Small Steps I — Creating with Text and Numbers
Make Decisions
Make Decisions
Explore one topic in small steps. Try the examples one at a time and take a break whenever you need. Choose how many extra exercises you want to try.
Running only when a condition holds
What you will learn
A condition is an expression that can be true or false. if is a conditional statement that runs the code inside its braces only when the condition is true.
Try it
void small_main()
{
int score = 80;
if (score >= 60)
{
print("Pass");
}
print("Done");
}
score >= 60 asks whether the score is at least 60. With 80, it is true and prints Pass. Change it to 50: Pass is skipped and only Done is printed.
> means greater than, < less than, >= at least, and <= at most. Do not put a ; after the closing brace of if.
Expected output:
Pass
Done
Doing something else when a condition is false
What you will learn
else holds the instructions to run when the if condition is false. It chooses one of two branches.
Try it
void small_main()
{
int answer = 4;
if (answer == 4)
{
print("Correct");
}
else
{
print("Try again");
}
}
Use == to compare for equality. It is different from =, which assigns a value. != compares whether values differ. Changing answer to 5 runs only the else branch.
Expected output:
Correct
Choosing one of three branches
What you will learn
For three or more choices, connect conditions with else if. They are checked from top to bottom, and only the first matching branch runs.
Try it
void small_main()
{
int number = 0;
if (number > 0)
{
print("Positive");
}
else if (number < 0)
{
print("Negative");
}
else
{
print("Zero");
}
}
0 is neither positive nor negative, so it reaches the final else. Several independent if statements check their conditions separately, so more than one branch may run.
Expected output:
Zero
Remembering true and false
What you will learn
bool is a type that stores true or false. It remembers states such as whether you are ready or whether a game is over.
Try it
void small_main()
{
bool is_ready = true;
if (is_ready)
{
print("Start");
}
print(is_ready);
}
You can put a bool variable directly in if. Small's print displays true as 1 and false as 0. You can also store a comparison result, as in bool passed = score >= 60;.
Expected output:
Start
1
Combining two conditions
What you will learn
&& means both conditions are true, || means at least one is true, and ! reverses true and false.
Try it
void small_main()
{
int age = 15;
if (age >= 13 && age <= 19)
{
print("Teen");
}
}
Check whether the age is at least 13 and at most 19. Try 12, 13, 19, and 20 to check the boundaries.
!ready asks whether you are not ready. && skips the right side if the left is false; || skips it if the left is true.
Expected output:
Teen
Exercise
Print Positive only when number is greater than 0. Test with 3 first.
void small_main()
{
int score = 80;
if (score >= 60)
{
print("Pass");
}
print("Done");
}
Hint
Also change 3 to 0 and -1. Those cases should produce no output.
show solution
void small_main()
{
int number = 3;
if (number > 0)
{
print("Positive");
}
}
Try More (Optional)
Exercise
Print Open when the password equals 1234 and Locked otherwise. Test with 1111.
void small_main()
{
int answer = 4;
if (answer == 4)
{
print("Correct");
}
else
{
print("Try again");
}
}
Hint
Change the parts of the example that you need.
show solution
void small_main()
{
int password = 1111;
if (password == 1234)
{
print("Open");
}
else
{
print("Locked");
}
}
Exercise
Change number to -2 and check which branch runs.
void small_main()
{
int number = 0;
if (number > 0)
{
print("Positive");
}
else if (number < 0)
{
print("Negative");
}
else
{
print("Zero");
}
}
Hint
Change the parts of the example that you need.
show solution
void small_main()
{
int number = -2;
if (number > 0)
{
print("Positive");
}
else if (number < 0)
{
print("Negative");
}
else
{
print("Zero");
}
}
Exercise
Change is_ready to false and check how the output changes.
void small_main()
{
bool is_ready = true;
if (is_ready)
{
print("Start");
}
print(is_ready);
}
Hint
Change the parts of the example that you need.
show solution
void small_main()
{
bool is_ready = false;
if (is_ready)
{
print("Start");
}
print(is_ready);
}
Exercise
Print Outside when the age is below 13 or above 19. Test with 20.
void small_main()
{
int age = 15;
if (age >= 13 && age <= 19)
{
print("Teen");
}
}
Hint
Use || between the two conditions.
show solution
void small_main()
{
int age = 20;
if (age < 13 || age > 19)
{
print("Outside");
}
}