Conditional statements, or "if statements", let you run a block of code only if a certain condition is true.
Lesson created by Richard Yi
This content is archived!
For the 2018-2019 school year, we have switched to using the WLMOJ judge for all MCPT related content. This is an archive of our old website and will not be updated.
Conditions can check if 2 values are equal, if a value is less than another, if a value is more than another, or a mixture of conditions.
If the condition is true, it will execute the code in the block below
For example, you can ask for the user’s age and see if they are an adult.
Turing
1
2
3
4
5
6
7
8
9
var age : int
put "Please enter your age: "
get age
if age < 18 then
put "You are not an adult."
% If age is less than 18, every line until "end if" will be excecuted
end if
Python
1
2
3
4
5
age=int(input('Please enter your age: '))ifage<18:print('You are not an adult.')# If age is less than 18, every line on this indent level will be excecuted
Java
1
2
3
4
5
6
7
8
9
BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));System.out.print("Please enter your age: ");intage=Integer.parseInt(in.readLine());if(age<18){System.out.println("You are not an adult.");// If age is less than 18, every line until the } will be excecuted}
If you want a block of code to run if a condition is true, and want another block of code to run if that condition is not true, you can use an else along with the if statement
Turing
1
2
3
4
5
6
7
8
9
10
11
12
var age : int
put "Please enter your age: "
get age
if age < 18 then
put "You are not an adult."
% If age is less than 18, every line until "else" will be excecuted
else
put "You are an adult."
% If age is not less than 18, every line until "end if" will be excecuted
end if
Python
1
2
3
4
5
6
7
8
age=int(input('Please enter your age: '))ifage<18:print('You are not an adult.')# If age is less than 18, every line on this indent level will be excecutedelse:print('You are an adult.')# If age is not less than 18, every line on this indent level will be excecuted
Java
1
2
3
4
5
6
7
8
9
10
11
12
BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));System.out.print("Please enter your age: ");intage=Integer.parseInt(in.readLine());if(age<18){System.out.println("You are not an adult.");// If age is less than 18, every line until the } will be excecuted}else{System.out.println("You are an adult.");// If age is not less than 18, every line until the } will be excecuted}
You can use else if (in some languages shortened to elif or elsif) to chain if statements together.
If the condition in the if statement is not true, then it checks for the condition in the next else if statement, then the next, and so on.
If none of the if or else if statements are true, it goes to the else block (if there is one).
Turing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
var age : int
put "Please enter your age: "
get age
if age < 14 then
put "You have not started high school yet."
elsif age < 18 then
put "You are not an adult."
elsif age > 200 then
put "Uh..."
else
put "You are an adult."
end if
Python
1
2
3
4
5
6
7
8
9
10
age=int(input('Please enter your age: '))ifage<14:print('You have not started high school yet.')elifage<18:print('You are not an adult.')elifage>200:print('Uh...')elseprint('You are an adult.')
Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));System.out.print("Please enter your age: ");intage=Integer.parseInt(in.readLine());if(age<14){System.out.println("You have not started high school yet.");}elseif(age<18){System.out.println("You are not an adult.");}elseif(age>200){System.out.println("Uh...");}else{System.out.println("You are an adult.");}
Practice
Pick three random numbers from 1 to 10 and ask the user to enter a number between 1 and 10.
If the user guesses one of the three numbers you picked, tell they won, otherwise, tell them they lost.