Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

Are you tall enough for a ride at an amusement park?

Ride height

Try

Watch this video to learn about the new concepts shown in the program:

Knowledge organiser

The new commands used in this program and others that may be useful. Select them below to learn more:

if, else if, else

The if command is used to branch a program execution in one of two directions depending on a condition being true or false.

if (x < y)

If the value of x is less than the value of y do the commands inside the braces underneath this command.

if (x > y)

If the value of x is greater than the value of y do the commands inside the braces underneath this command.

else

If the condition is not met do the commands inside the braces underneath this command instead.

Code must be enclosed in braces { } inside if and else statements. Only one part of the code will be run, the other part will be ignored.

Investigate

Questions to think about with this program to check your understanding:

Item question

Identify a condition in the program.

Reveal answer

height > 104 is known as a condition. The outcome of a condition is either true or false.

Structure question

State the two essential parts of the if selection statement and two optional parts.

Reveal answer

Two essential parts of the if command:

  1. A condition inside parentheses
  2. Code inside braces to execute if the condition is true.

Two optional parts:

  1. An else
  2. Code inside braces to execute if the condition is false.

Make

Change the program so that it works for a toddler’s merry-go-round:

  1. The child must be under 91cm to go on the ride.

Typical inputs and outputs from the program would be:

Enter the height of the person in cm:
110
Sorry, you are too tall.
Enter the height of the person in cm:
78
Height OK.
🆘 If you're really stuck, use this Parsons code sorting exercise
Complete program
// Ride height program

using System;

class Submission
{
---
    // -------------------------
    // Subprograms
    // -------------------------
    // Check if the child is under 90cm
---
    static bool valid_height(double height)
    {
---
        if (height < 91)
        {
---
            return true;
---
        }
---
        else
        {
---
            return false;
---
        }
---
    }
---

    // -------------------------
    // Main program
    // -------------------------
    public static void Main(string[] args)
    {
---
        Console.WriteLine("Enter the height of the person in cm:");
        double height = Convert.ToDouble(Console.ReadLine());
---
        if (valid_height(height))
        {
---
            Console.WriteLine("Height OK.");
---
        }
---
        else
        {
---
            Console.WriteLine("Sorry, you are too tall.");
---
        }
---
    }
---
}