CONTROL FLOW STRUCTURE IN JAVA LANG.pdf

riazahamed37 17 views 14 slides Jul 19, 2024
Slide 1
Slide 1 of 14
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14

About This Presentation

control flow in java


Slide Content

Programming
in Java
Control Flow

Flow of Control
Control flow statements help programmers make
decisionsabout which statements to execute and to
change the flow of execution in a program.
Four categories Four categories
conditional statements
loops
exception
branch

Conditional Statements
When a program makes a decision, it determines,
based on the state of the program data whether
certain lines of code should be executed.
The two conditional statements provided by Java are:The two conditional statements provided by Java are:
if
switch-case

Conditional Statements
These statements allows to control the flow of the
program’s execution based upon conditions known only
during run time.
Types of if statement
if
if else
Nested if
if else if

Conditional Statement -if
The if statement executes a block of code only if the
specified condition is true. If the value is false, then
the if block is skipped and execution continues with
the rest of the program
Syntax:Syntax:
if(condition)
statement1;
statement2;

Example for if statement
if((n % 2) == 0)
{
System.out.println(“Number is divisible by 2");
}}

Conditional Statement –if else statement
Definition
The if/else statement is an
extension of the if statement.
If the statements in the if
statement fails, the statements in
the else block are executed.
Syntax
if(condition)
statement1;
elseelse
statement2;

Example for if else statement
int n=50;
if((n % 2) == 0)
{
System.out.println("Given Number is Even!");
}}
else
{
System.out.println("Given Number is Odd!");
}

Conditional Statement –nested if statement
Definition
A nested if is an if statement
that is the target of another if
or else.
Nested if statements mean an
Syntax
if(condition1)
{
if(condition2)
{
Nested if statements mean an
if statement inside an if
statement.
{
statement1;
}
}

Example for Nested if statement
int num=50;
if (num < 100)
{
System.out.println("Given number is below 100");
if (num % 2 == 0)
System.out.println(“Number is EVEN");
elseelse
System.out.println(“Number is ODD");
}
else
System.out.println("Given number is not below 100");

Conditional Statement –if else if statement
Definition
if-else-if statement is a
sequence of if-else
statements.
Syntax
if(condition)
statement1;
else if(condition)
statement2;
else if(condition)else if(condition)
statement3;
. .
else
statement4;

Example for if-else if statement
int num1=50,num2=40,num3=60;
if( num1>=num2 && num1>=num3)
System.out.println("\nThe largest number is " + num1) ;
else if (num2>=num1 && num2>=num3)
System.out.println("\nThe largest number is " + num2) ;System.out.println("\nThe largest number is " + num2) ;
else
System.out.println("\nThe largest number is " + num3) ;

Conditional Statement –switch statement
Definition
It provides more than one
choice to choose. It’s a
better alternative to if-else-if
ladder.
Syntax
switch(expression)
{
case value1: statement1;
break;
case value2: statement2;case value2: statement2;
break;
case value3: statement3;
break; . . .
case valueN: statementN;
break;
default : statement;
}

Example for switch statement
char c='B';
switch(c)
{
case 'A': System.out.println("You entered Sunday"); break;
case 'B': System.out.println("You entered Monday"); break;
case 'C': System.out.println("You entered Tuesday"); break;
case 'D': System.out.println("You entered Wednesday");break;
case 'E': System.out.println("You entered Thursday"); break;
case 'F': System.out.println("You entered Friday");break;
case 'G': System.out.println("You entered Saturday"); break;
default: System.out.println("Wrong choice");
}
Tags