0% found this document useful (0 votes)
7 views

L3 - Decision Making and Control Structures

The document discusses different types of decision making statements in Java including if, if-else, nested if, if-else-if, and switch-case statements. Examples of code are provided for each statement type to demonstrate their syntax and usage. Predictions of output are also included for sample code snippets.

Uploaded by

samarth soni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

L3 - Decision Making and Control Structures

The document discusses different types of decision making statements in Java including if, if-else, nested if, if-else-if, and switch-case statements. Examples of code are provided for each statement type to demonstrate their syntax and usage. Predictions of output are also included for sample code snippets.

Uploaded by

samarth soni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Decision Making

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 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
}
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.

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
}
}
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.

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;
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. Compile time error


2. BYE
3. HELLO
4. No Output
1 // Predict the output
2 public class A {
3 public static void main(String[] args)
4 {
5 if (true)
6 break;
7 }
8 }
OUTPUT

1. Nothing
2. Error
THANK YOU

You might also like