if... else
Definition
The if statement is used to execute a block of code when a condition is true. It is possible to add an else to the if statement to execute a different block of code when this condition is not true.
Syntax
if
A simple if statement has the following syntax:
if ( condition ) {
block1;
}if ( condition ) {
block1;
} if condition:
block1
if ... else
We can add an else and get a full if ... else statement:
if ( condition ) {
block1;
} else {
block2;
}if ( condition ) {
block1;
} else {
block2;
}if condition:
block1
else:
block2
- The condition is an expression that evaluates to either
trueorfalse. - The block1 is a block of code that gets executed when the condition evaluates to
true. - The block2 is a block of code that gets executed when the condition evaluates to
false.
if ... else if ... else
When the original condition is false and we reach the else, we can check for another condition using the else if:
if ( condition ) {
block1;
} else if ( condition2 ) {
block2;
} else {
block3;
}if ( condition ) {
block1;
} else if ( condition2 ) {
block2;
} else {
block3;
}
if condition:
block1
elif condition2:
block2
else:
block3
- The condition is an expression that evaluates to either
trueorfalse. - The block1 is a block of code that gets executed when the condition evaluates to
true. - The block2 is a block of code that gets executed when the condition evaluates to
falseand condition2 evaluates totrue. - The block3 is a block of code that gets executed when both condition and condition2 evaluate to
false.
The if ... else if ... else is a useful construct when there are more than 2 cases to the outcome. For example, we can use a simple if ... else to check the result of flipping a coin, because the result will either be heads or tails so 2 possibilities. This would result in the following code:
if ( coin == heads ) {
message = "You got heads";
} else {
message = "You got tails";
}
if ( coin == heads ) {
message = "You got heads";
} else {
message = "You got tails";
}
if coin == heads:
message = 'You got heads'
else:
message = 'You got tails'
However, if you wanted to check the results of rolling a dice, which has 6 possibilities, a simple if .. else statement will not be sufficient. In this case, the if ... else if ... else construct will be handy:
if ( side == one ) {
message = "You scored 1 point";
} else if ( side == two ) {
message = "You scored 2 points";
} else if ( side == three ) {
message = "You scored 3 points";
} else if ( side == four ) {
message = "You scored 4 points";
} else if ( side == five ) {
message = "You scored 5 points";
} else {
message = "You scored 6 points";
}
if ( side == one ) {
message = "You scored 1 point";
} else if ( side == two ) {
message = "You scored 2 points";
} else if ( side == three ) {
message = "You scored 3 points";
} else if ( side == four ) {
message = "You scored 4 points";
} else if ( side == five ) {
message = "You scored 5 points";
} else {
message = "You scored 6 points";
}if side == one:
message = 'You scored 1 point'
elif side == two:
message = 'You scored 2 points'
elif side == three:
message = 'You scored 3 points'
elif side == four:
message = 'You scored 4 points'
elif side == five:
message = 'You scored 5 points'
else:
message = 'You scored 6 points'
side == six since this is the only possibility if none of the previous conditions are trueExamples
Example 1: Ticket Price
You want to check the ticket price the user should pay. The rule to determine the price is as follows:
- Free for anyone up to the age of 12
- £10 for above 12, up to the age of 16
- £20 for above 16
Let's create the code for this, one step at a time.
- Entry is Free for anyone up to the age of 12
agewill be the variable representing the user's age, and will contain a number.entrywill be the variable representing the entry price, and will contain a string. - If the user is above 12, then the entry will be £10 if they are up to the age of 16.
- Then if they are above the age of 16 (i.e. neither 12 or under nor 16 or under), we know that they are above 16 and must pay £20.
- To pass the module, a student needs a minimum module average mark of 40% and also meet the minimum qualifying mark for the various module components that have one.
- Anyone that gets below 40% but above 30% or above 30% and doesn't meet any of the qualifying mark will get a referral in the module components for which they have not achieved a mark of at least 40.
- Anyone with an average mark below 40% has failed the module.
- All students with an average mark above 40% may have passed the module.
averagewill be the variable representing the module average, and will contain a number.outcomewill be the variable representing the module outcome, and will contain a string. - Let's consider that the module has a coursework and an exam, and that only the exam has a qualifying mark of 30%. The
examvariable will represent the exam mark, and will contain a number. To pass the module, the student needs an average mark of at least 40% (already in place) but also an exam mark above 30. If not, they get a referral. We can do this inside anotherif...elseblock: - We'll now add an
elseto the originalifso that anyone with an average above 30% will get a referral: - Finally, anyone students that don't get an average above 40% or an average above 30% get a fail in the module:
var age, entry;
if ( age <= 12 ) {
entry = "Free";
}
int age;
String entry;
if ( age <= 12 ) {
entry = "Free";
}
age = 2
if age <= 12:
entry = 'Free'
var age, entry;
if ( age <= 12 ) {
entry = "Free";
} else if ( age <= 16 ) {
entry = "£10";
}
int age;
String entry;
if ( age <= 12 ) {
entry = "Free";
} else if ( age <= 16 ) {
entry = "£10";
}
age = 2
if age <= 12:
entry = 'Free'
elif age <= 16:
entry = '£10'
var age, entry;
if ( age <= 12 ) {
entry = "Free";
} else if ( age <= 16 ) {
entry = "£10";
} else {
entry = "£20";
}
int age;
String entry;
if ( age <= 12 ) {
entry = "Free";
} else if ( age <= 16 ) {
entry = "£10";
} else {
entry = "£20";
}
age = 2
if age <= 12:
entry = 'Free'
elif age <= 16:
entry = '£10'
else:
entry = '£20'
View this example in the Code Visualiser. You may want to animate this code also using the control panel on the right handside.
Example 2: Module Outcome
You want to check whether someone has passed a module (in this case, a module is a course component, as university students would know it). The rule to pass a module is as follows:var average = 0,
outcome = "";
if ( average >= 40 ) {
outcome = "Pass";
}
double average = 0;
String outcome = "";
if ( average >= 40 ) {
outcome = "Pass";
}
average = 0
outcome = ''
if average >= 40:
outcome = 'Pass'
var average = 0,
exam = 0,
outcome = "";
if ( average >= 40 ) {
if ( exam >= 30 ) {
outcome = "Pass";
} else {
outcome = "Referral";
}
}double average = 0;
int exam = 0;
String outcome = "";
if ( average >= 40 ) {
if ( exam >= 30 ) {
outcome = "Pass";
} else {
outcome = "Referral";
}
}
average = 0
exam = 0
outcome = ''
if average >= 40:
if exam >= 30:
outcome = 'Pass'
else:
outcome = 'Referral'
var average = 0,
exam = 0,
outcome = "";
if ( average >= 40 ) {
if ( exam >= 30 ) {
outcome = "Pass";
} else {
outcome = "Referral";
}
} else if ( average >= 30 ) {
outcome = "Referral";
}
double average = 0;
int exam = 0;
String outcome = "";
if ( average >= 40 ) {
if ( exam >= 30 ) {
outcome = "Pass";
} else {
outcome = "Referral";
}
} else if ( average >= 30 ) {
outcome = "Referral";
}
average = 0
exam = 0
outcome = ''
if average >= 40:
if exam >= 30:
outcome = 'Pass'
else:
outcome = 'Referral'
elif average >= 30:
outcome = 'Referral'
var average = 0,
exam = 0,
outcome = "";
if ( average >= 40 ) {
if ( exam >= 30 ) {
outcome = "Pass";
} else {
outcome = "Referral";
}
} else if ( average >= 30 ) {
outcome = "Referral";
} else {
outcome = "Fail";
}
double average = 0;
int exam = 0;
String outcome = "";
if ( average >= 40 ) {
if ( exam >= 30 ) {
outcome = "Pass";
} else {
outcome = "Referral";
}
} else if ( average >= 30 ) {
outcome = "Referral";
} else {
outcome = "Fail";
}
average = 0
exam = 0
outcome = ''
if average >= 40:
if exam >= 30:
outcome = 'Pass'
else:
outcome = 'Referral'
elif average >= 30:
outcome = 'Referral'
else:
outcome = 'Fail'
if else statements structure, view this in the Code Visualiser, or play with the code below:
JS Bin on jsbin.com.