3-Decision Making and Control Structures-28!04!2023
3-Decision Making and Control Structures-28!04!2023
Topic/Course
Sub-Topic (Example: name of college)
Decision/Selection statements
• if
• if else
• nested if
• if else if
• switch case
Simple if
• It is used to decide whether certain statement or block of statements
will be executed or not.
Syntax:
if (condition)
{
// Executes this block if condition is true
}
1 public class IfExample { output
2 public static void main(String[] args) {
3 int age=20;
4 if(age>18){ Age is greater than 18
5 System.out.print("Age is greater than 18");
6 }
7 }
8 }
if else
• It is used for two way decision making.
• If the condition is true then the if part is executed and if the condition is
false the else part will be executed.
Syntax:
if (condition)
{
// Executes this block if condition is true
}
else
{
// Executes this block if condition is false
}
1 class IfElseDemo output
2 {
3 public static void main(String args[])
4 {
5 int i = 10; i is smaller than 15
6 if (i < 15)
7 System.out.println("i is smaller than 15");
8 else
9 System.out.println("i is greater than 15");
10 }
11 }
Nested if
• An if else statement can contain any sort of statement within it.
• It can contain another if-else statement
• An if-else may be nested within the if part.
• An if-else may be nested within the else part.
• An if-else may be nested within both parts.
Syntax:
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
1 class NestedIfDemo output
2 {
3 public static void main(String args[])
4 {
5 int i = 10; i is smaller than 15
6 if (i == 10) i is smaller than 12 too
7 {
8 if (i < 15)
9 System.out.println("i is smaller than 15");
10 if (i < 12)
11 System.out.println("i is smaller than 12 too");
12 else
13 System.out.println("i is greater than 15");
14 }
15 }
16 }
if else if
• If else if statement is a multiway branch statement.
• In if-else-if statement, as soon as the condition is met, the
corresponding set of statements get executed, rest gets ignored.
Syntax:
if (condition1)
statement1;
else if (condition2)
statement2;
.
.
else
statement n;
1 class ifelseifDemo output
2 {
3 public static void main(String args[])
4 {
5 int i = 20;
6 i is 20
7 if (i == 10)
8 System.out.println("i is 10");
9 else if (i == 15)
10 System.out.println("i is 15");
11 else if (i == 20)
12 System.out.println("i is 20");
13 else
14 System.out.println("i is not present");
15 }
16 }
switch case
• The switch statement is a multiway branch statement.
• Expression can be of type byte, short, int char or an enumeration,
String.
• The break statement is optional. If omitted, execution will continue on
into the next case.
Syntax:
switch (expression)
{
case value1: statement1;
break;
case value2: statement2;
break;
.
.
case valueN: statementN;
break;
default: statementDefault;
}
1 class SwitchCaseDemo output
2 {
3 public static void main(String args[])
4 {
5 int i = 9;
6 switch (i) i is greater than 2.
7 {
8 case 0:
9 System.out.println("i is zero.");
10 break;
11 case 1:
12 System.out.println("i is one.");
13 break;
14 case 2:
15 System.out.println("i is two.");
16 break;
17 default:
18 System.out.println("i is greater than 2.");
19 }
20 }
21 }
1 // Predict the output
2 class Test {
3 public static void main(String[] args)
4 {
5 int x = 10;
6 if (x) {
7 System.out.println("HELLO");
8 } else {
9 System.out.println("BYE");
10 }
11 }
12 }
13
14
15
16
17
18
19
20
21
22
OUTPUT
1. HELLO
2. Compile time error
3. Runtime Error
4. BYE
1 // Predict the output
2 class Test {
3 public static void main(String[] args)
4 {
5 int x = 10;
6 if (x)
7 System.out.println("HELLO");
8 System.out.println("WELCOME");
9
10 else
11 {
12 System.out.println("BYE");
13 }
14 }
15 }
OUTPUT
1. HELLO WELCOME
2. HELLO
3. BYE
4. Compile time error
1 // Predict the output
2 class Test {
3 public static void main(String[] args)
4 {
5 if (true)
6 ;
7 }
8 }
OUTPUT
1. No Output
2. Compile time error
3. Runtime error
4. Runtime Exception
1 // Predict the output
2 class MainClass {
3 public static void main(String[] args)
4 {
5 int x = 10;
6 switch (x + 1 + 1) {
7 case 10:
8 System.out.println("HELLO");
9 break;
10 case 10 + 1 + 1:
11 System.out.println(“BYE");
12 break;
13 }
14 }
15 }
OUTPUT
1. Nothing
2. Error
THANK YOU