Lesson Plan - Conditionals PDF
Lesson Plan - Conditionals PDF
Conditionals
Pre-Requisites:
Basic Java syntax
Data types, variables, keywords
Operators
In real life we often encounter situations where our actions are governed by some sort of conditions. For
instance, if the weather is rainy, we carry an umbrella. Here carrying an umbrella is an action that is performed
only when the condition of the weather being rainy is fulfilled.
In programming, this kind of scenario is handled with the help of conditional statements.
Topic: If statement
An if statement is the most common conditional statement. It executes when the condition being checked
evaluates to true.
Syntax
if (condition)
statement;
System.out.print(“Pass”);
Case - 1: marks = 85
Output - Pass
Explanation - Since the marks is greater than 80 i.e. the condition inside ‘if’ parentheses is true, we get “Pass”
printed in the output.
Output - No output
Explanation - Since the marks is not greater than 80 i.e. the condition inside if parentheses is false, the
statement inside the if block is skipped/not executed i.e. we get nothing printed in the output.
An if-else statement is designed to give us this functionality in our code. It executes statements if some
condition is true or false. If the condition is true, the if part is executed, otherwise the else part of the statement
is executed.
Syntax
if (condition)
statement - 1
} else
statement - 2
To illustrate the working of an if-else statement, we can create a grading system. We’ll assume that the score
ranges between 0 to 100(both inclusive). A score above 33 gets a “Pass” verdict, otherwise, it’s “Fail”.
To solve this problem, we can use an if-else statement. Let's see how!
System.out.println(“Pass”);
} else
System.out.println(“Fail”);
Case - 1: grade = 60
Output - Pass
Explanation - Since the score is more than 33 i.e. the condition inside ‘if’ parentheses is true, we get “Pass”
printed.
Case - 2: grade = 20
Output - Fail
Explanation - Since the score is not more than 33 i.e. the condition inside ’if’ parentheses is false, the compiler
jumps to the else part of the code and we get “Fail” printed.
1. Find if the input value is odd or even. If it’s odd print “Odd”, otherwise print “Even”.
Syntax
if (condition - 1)
statement - 1
} else if (condition - 2)
statement - 2
} else
statement - 3
To implement if else-if, we can further expand our grading system problem. Apart from pass and fail now it will
give grades based on the score.
System.out.println(“A”);
System.out.println(“B”);
System.out.println(“C”);
} else
System.out.println(“D”);
Case - 1: grade = 81
Output - A
Explanation - Since the score is more than 80 i.e. the condition inside if parentheses is true, “A” gets printed.
Case - 2: grade = 61
Output - B
Explanation - Since the score is less than 80, the first block is skipped and since it is more than 60 i.e. the
condition inside else-if parentheses is true, “B” gets printed.
Case - 3: grade = 41
Output - C
Explanation - Since the score is 41, the first 2 blocks are skipped and then the condition for the third block is
checked. It turns out that it is true, so “C” gets printed and the rest is skipped.
Case - 4: grade = 21
Output - D
Explanation - Since all the conditions are false, the else-block will execute and we get “D” as output.
Try these :
Write a program to identify people as “Child” (age < 12), “Teenager” (12 <= age < 18) or “Adult” (age >= 18).
Print the maximum of 3 numbers a, b, c taken as input.
Let us now increase the dimension and abilities of if and explore the concept of nested if-else statements.
Syntax
if (condition - 1)
if (condition - 2)
statement - 1
} else
statement - 2
} else
statement - 3
System.out.print(“Gracefully ”);
System.out.println(“Pass”);
} else
System.out.println(“Fail”);
Input :
Score = 30
Output :
Fail
Input :
Score = 90
Output :
Gracefully Pass
Input :
Score = 60
Output:
Pass
Operators that assist in the functioning of conditional statements by proper and accurate condition checks are
known as conditional operators. We have already seen a few in the previous lecture but it deserves a mention
here too. So, let us learn more about them.
It is used when we want the condition to be true iff both the expressions are true.
Syntax
statement;
Print the number if the input value is greater than 5 and less than 10.
System.out.print(val);
Case - 1: val = 3
Output- No output
Explanation- The input value is less than 10 but it is not greater than 5.
Case - 2: val = 7
Output- 7
Explanation- The input value is both less than 10 and greater than 5.
Case - 3: val = 13
Output- No output
Explanation- The input value is greater than 5 but it is not less than 10.
The first one (&) parses through all the conditions, despite their evaluation as true or false. However, the latter
traverses through the second condition only if the first condition is evaluated as true, otherwise it negates the
entire condition. For instance,
It gives us a runtime error since 5 is divided by 0, which isn't a valid operation. However, if we write-
we get “false” as the output. This is because our first condition is false, which is enough to make the entire
condition false here.
Try this
This operator is used when any one of the boolean expressions is evaluated as true.
Syntax
if(condition - 1 || condition - 2)
statement;
Print the number if the input value is greater than 10 or less than 5.
Code
System.out.print(val);
Case - 1: val = 3
Output- 3
Explanation- The input value is less than 5. It is enough to satisfy the condition so the second condition won't
be tested and the val will be printed.
Case - 2: val = 7
Output- No output
Case - 3: val = 13
Output- 13
Explanation- The input value is not smaller than 5 but it is greater than 10. So overall it will be evaluated as
true.
Try this
|| -> logical or
System.out.println(true | 5/0==2);
It gives a runtime error since 5 is being divided by 0, which isn't a valid operation. However, if we write-
System.out.println(true || 5/0==2);
We get “true” as the output. This is because our first condition is true, which is enough to make the entire
condition true in case of logical or.
It is a smaller version for the if-else statement. If the condition is true then the statement - 1 is executed else the
statement - 2 is executed.
Syntax
if (val % 2 == 1)
} else
Case - 1: val = 1
Case - 2: val = 2
For numbers which are multiples of both 3 and 5, print "FizzBuzz" instead of the number.
2. Write a short program that prints each number from 1 to 100 on a new line, except if the number is a
multiple of 5 or 7.
We have now reached to the final conditional statement viz switch case. This renders the program to look
extremely neat and organized. Let us learn more about it.
It is like an if-else ladder with multiple conditions, where we check for equality of a variable with different
values.
It works with byte, short, int, long, enum types, String and some wrapper types like Byte, Short, Int, and Long.
Since Java 7, you can use strings in the switch statement.
Syntax
switch (expression)
case x:
// code
break;
case y:
// code
break;
default:
// code
Note: The case value must be literal or constant, and must be unique.
Write a program using switch statements to check if the input lowercase character is vowel or consonant.
switch (ch) {
case ‘a’:
System.out.println(“Vowel”);
break;
case ‘e’:
System.out.println(“Vowel”);
break;
case ‘i’:
System.out.println(“Vowel”);
break;
case ‘o’:
System.out.println(“Vowel”);
break;
case ‘u’:
System.out.println(“Vowel”);
break;
default:
System.out.println(“Consonant”);
Case - 1: ch = ‘e’
Output - Vowel
Case - 2: ch = ‘w’
Output - Consonant
‘Break’ is a very important keyword when we are implementing switch statement (we will explore it in detail
in the forthcoming lectures). Lets look at an example where we implement a logic with and without break
statement.
int val = 5;
switch (val)
case (5):
System.out.println(“Five”);
case (6):
System.out.println(“Six”);
case (7):
System.out.println(“Seven”);
Output:
Five
Six
Seven
Surprised? You did not expect this, did you? Let us see why ?
Here, even when the first case is true, it will keep on executing all the subsequent statements until it encounters
a break statement(which we have not used anywhere). After the break statement, it will break out of the switch
statement.
int val = 6;
switch (val)
case (6):
System.out.println(“Six”);
break;
case (7):
System.out.println(“Seven”);
break;
case (8):
System.out.println(“Eight”);
break;
Output:
Six
Explanation : Using the break keyword here, helped the program to stop executing and come out of the switch
statement.
Try this
Write a program to print the day name based upon the day number.