L3 - Decision Making and Control Structures
L3 - Decision Making and Control Structures
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
}
public class IfExample { output
public static void main(String[] args) {
int age=20;
Age is greater than 18
if(age>18){
System.out.print("Age is greater than 18");
}
}
}
if else
It is used for two way decision making.
if (condition)
{
// Executes this block if condition is true
}
else
{
// Executes this block if condition is false
}
class IfElseDemo output
{
public static void main(String args[])
{
int i = 10; i is smaller than 15
if (i < 15)
System.out.println("i is smaller than 15");
else
System.out.println("i is greater than 15");
}
}
Nested if
An if else statement can contain any sort of statement within it.
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
class NestedIfDemo
{
output
public static void main(String args[])
{
int i = 10;
if (i == 10) i is smaller than 15
{ i is smaller than 12 too
if (i < 15)
System.out.println("i is smaller than 15");
if (i < 12)
System.out.println("i is smaller than 12
too");
else
System.out.println("i is greater than 15");
}
}
}
if else if
If else if statement is a multiway branch statement.
if (condition1)
statement1;
else if (condition2)
statement2;
.
.
else
statement n;
class ifelseifDemo
{
public static void main(String args[])
{
int i = 20;
if (i == 10)
output
System.out.println("i is 10");
else if (i == 15) i is 20
System.out.println("i is 15");
else if (i == 20)
System.out.println("i is 20");
else
System.out.println("i is not present");
}
}
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;
}
class SwitchCaseDemo
{
public static void main(String args[])
{
int i = 9;
switch (i)
{
output
case 0:
System.out.println("i is zero.");
i is greater than 2.
break;
case 1:
System.out.println("i is one.");
break;
case 2:
System.out.println("i is two.");
break;
default:
System.out.println("i is greater than 2.");
}
}
}
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
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[]
4 args)
{
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