Creating Branches
Last updated
Last updated
if statement
In this situation we created a single branch. The event that belongs to our if statement only executes if our given variable num
is greater than 100.
Any code that belongs to a coding construct is called a "code block"
if ... else statement
To create a program with two pathways where we can have an event occurs when our if statement
evaluates to false is using an else
statement.
An else
statement is not a requirement for all if clauses; however, you can use it if you want something to happen when the condition it is attached to is evaluated to false
.
To create a program with multiple, BUT RELATED pathways, we can incorporate an else if
statement.
The program above would work as follows
It outputs Nice!
only if num was equal to 69
It outputs Wow, that is a large number!
, only if num
is not equal to 69 and greater than 100
It outputs Darn, that is a small number!
, only if num
is not equal to 69 and num
is less than 100
Conditional Branching (Link)