View
226
Download
1
Category
Preview:
DESCRIPTION
control statements of java
Citation preview
CONTROL FLOW STATEMENTS IN JAVA
Java – Control Flow Statements
Control Statements
Selection
Iteration
Jump
Java – Control Flow Statements
Selection
If Statement
Switch Statement
Selection Statements are also called Decision Making Statements
Java – Control Flow StatementsIf Statements
Simple if
If else
If else if (if else Ladder)
Nested If
Java – Control Flow Statements If Statement
It is used to take decision based on a single condition
Syntax: if(Condition){
Statements
}
True
False
If condition is True; control will enter the if block
If condition is False; control will execute statement followed by if block
Java – Control Flow StatementsIf Statement
Start
End
Conditi on
Statements
FalseTrue
Flow Chart:
Java – Control Flow Statements If Statement
Example: Read marks from user and state only if user is pass.
Java – Control Flow Statements if-else Statement
It is used to take decision based on a single condition
Syntax: if(Condition){ Statements}Else{ Statements}
TrueFalse
Java – Control Flow StatementsIf-else Statement
FalseTrue
Flow Chart: Start
End
Conditi on
True Block False Block
Java – Control Flow StatementsIf-else Statement
If condition is True; control will execute the statement in the true block
If condition is False; control will execute the statement in false block
The if else statement has one condition and two statement blocks- True block and False block
Java – Control Statements if-else Statement
Example: Read marks from user and state whether student is Pass or Fail
if(Condition1){ Statements}else{ if(Condition2) { Statements }}
Java – Control Statements if-else-if Statement (Ladder if)
It is used to take decision based on two conditions.
Syntax: else part of if contains another if statement
Java – Control StatementsIf-else-if Statement (Ladder if)
FalseTrue
Flow Chart: Start
End
Conditi on1
True Block
S t a r t
E n d
C o nd i ti
o n
T r u e B l o c k
F a l s e B l o c k
If within else
Java – Control Statements
Example:
if-else-if Statement (Ladder if)
if(Condition1){ Statements}else{ if(Condition2) { Statements }}
Java – Control Statements Nested-if Statement
The Nested if can be inside the if-part or else-part
Syntax: if(Condition1){ if(Condition2) { Statements }
}else{
Statements}
Java – Control Statements
FalseTrue
Flow Chart: Start
End
Conditi on1
True Block
S t a r t
E n d
C o nd i ti
o n
T r u e B l o c k
F a l s e B l o c k
If within else
Nested-if Statement
Java – Control Statements
FalseTrue
Flow Chart:
If within else
Nested-if Statement
Start
End
Conditi on
False Block
S t a r t
E n d
C o nd i ti o n
T r u e B l o c k
F a l s e B l o c k
Java – Control Statements
Example:
Nested-if Statement
Java – Control Statements
Example:
Nested-if Statement
switch(expression/variable){Case value: case statements break // optionalCase value:
case statements break// optional
.
.
.
default://optional default statements
}
Java – Control Statements switch Statement
A switch statement is used to test many conditions
Syntax:
Java – Control Statements
Flow Chart:
switch Statement
Switch Statement without “break”
Case A
Case B
…
default
False
False
False
Case A Statements
Case B Statements
Case C Statements
Default Statements
Start
Variable or Expression
True
True
True
End
Java – Control Statements
Flow Chart:
switch Statement
Switch Statement with “break”
Case A
Case B
…
default
False
False
False
Case A Statementsbreak;
Case B Statementsbreak;
Case C Statementsbreak;
Default Statements
Start
Variable or Expression
True
True
True
End
Java – Control Statements
Example:
switch Statement
Java – Control Statements switch Statement
·The variable used in a switch statement can only be a byte, short, int, or char
·You can have any number of case statements within a switch
·Each case is followed by the value to be compared to and a colon
·The value for a case must be the same data type as the variable in the switch and it must be a constant or a literal
·When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached
Java – Control Statements switch Statement
·When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement
·Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached
·A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case
Java – Control Statements
Iterations / Loops
Java – Control StatementsThe iterati ve statements/ loop executes a block of statements when a parti cular conditi on is true
Each loop has four types of statements :-Initi alizati on-Conditi on checking-Executi on-Increment / Decrement
Java – Control Statements
Iterations/ Loops
while
do while
for
Types of Loops
Java – Control Statements
Loops can be categorized on the basis of place where conditi on checking is being done:
Loops
Entry Controlled Exit Controlled
For
While
Do-While
Java – Control StatementsDiff erence Between Entry Controlled and Exit Controlled Loops
Entry Controlled Exit Controlled
Condition is checked at the entry of the loop
Condition is checked at the exit of the loop
If condition is initially false, the loop never executes
i=1;while(i==0){ System.out.println(“In While loop”); }System.out.println(“out of the loop”);
Output:Out of the loop
If condition is initially false, then also the loop executes at least once
i=1;do{ System.out.println(“In While loop”); } while(i==0);System.out.println(“out of the loop”);
Output:In while loopOut of the loop
Example- for, while Example – do-while
Java – Control Statements while Loop
Syntax
while(condition){
Statements
}
True
False
Java – Control Statements while Loop
Example: print values from 1 to ten
Java – Control Statements while Loop
Example: print values from 1 to ten
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i); i=i+1;
}
Initializationstatement
Initialization Statement is used to initialize a variable/ counter.
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i); i=i+1;
}
Condition Statement
The condition statement controls the execution of loop
The loop executes till the condition statement is true
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
Execution StatementThe execution statements are the main body of a loop
All action statements of loop are written here
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
Increment/ Decrement
This section is used to increment or decrement the variable value
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
i
1
Variable i in memory
Output
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
i
1
Variable i in memory
Output Condition is True
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
i
1
Variable i in memory
1
Output
Check it out here
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
i
2
Variable i in memory
1
Output
Check it out here
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
i
2
Variable i in memory
1
Output Condition is checked again
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
i
2
Variable i in memory
1
Output Condition is True
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
i
2
Variable i in memory
12
Output
Check it out here
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
i
3
Variable i in memory
12
Output
Check it out here
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
i
3
Variable i in memory
12
Output Condition is checked again
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
i
3
Variable i in memory
12
Output Condition is True
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
i
3
Variable i in memory
123
Output
Check it out here
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
i
4
Variable i in memory
123
Output
Check it out here
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
i
4
Variable i in memory
123
Output Condition is checked again
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
i
4
Variable i in memory
123
Output Condition is True
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
i
4
Variable i in memory
1234
Output
Check it out here
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
i
5
Variable i in memory
1234
Output
Check it out here
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
i
5
Variable i in memory
1234
Output Condition is checked again
Java – Control Statements while Loop
This process will continue till the condition become false
Suppose value of i is 9 now
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
i
9
Variable i in memory
12345678
Output Condition is True
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
i
9
Variable i in memory
123456789
Output
Check it out here
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
i
10
Variable i in memory
123456789
Output
Check it out here
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
i
10
Variable i in memory
123456789
Output Condition is checked again
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
i
10
Variable i in memory
123456789
Output Condition is Still True
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
i
10
Variable i in memory
123456789
10
Output
Check it out here
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
i
11
Variable i in memory
123456789
10
Output
Check it out here
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
i
11
Variable i in memory
123456789
10
Output Condition is checked again
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
i
11
Variable i in memory
123456789
10
Output Condition is FALSE
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
i
11
Variable i in memory
123456789
10
Output
Skip the body of the lop and
executes the statement jast after the loop
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
i
11
Variable i in memory
123456789
10
Output
Final value of i after the
complete loop
Java – Control Statements while Loop
Functi onality
i=1;
while(i<=10){
System.out.println(i);
i=i+1;
}
i
11
Variable i in memory
123456789
10
Output Final output of the loop
Java – Control Statementsfor Loop
Syntax
for( initi alizati on ; conditi on ; increment){
Statements
}
TrueFalse
Java – Control Statements for Loop
Example: print values from 1 to ten
Java – Control Statementsfor Loop
Example: print values from 1 to ten
Java – Control Statementsfor Loop
Functi onality
for(i=1; i<=10; i=i+1){
System.out.println(i);
}
Initializationstatement
Initialization Statement is used to initialize a variable/ counter.
Java – Control StatementsFor Loop
Functi onality The condition statement controls the execution of loopThe loop executes till the condition statement is true
for(i=1; i<=10; i=i+1){
System.out.println(i);
}
Condition Statement
for(i=1; i<=10; i=i+1){
System.out.println(i);
}
Java – Control Statements for Loop
Functi onality
Increment/ Decrement
This section is used to increment or decrement the variable value
for(i=1; i<=10; i=i+1){
System.out.println(i);
}
Java – Control Statements for Loop
Functi onality
Execution Statement
The execution statements are the main body of a loopAll action statements of loop are written here
for(i=1; i<=10; i=i+1){System.out.println(i);
}
Java – Control Statements for Loop
Functi onality
i
1
Variable i in memory
Output Initialization
for(i=1; i<=10; i=i+1){System.out.println(i);
}
Java – Control Statements for Loop
Functi onality
i
1
Variable i in memory
OutputCondition
is True
for(i=1; i<=10; i=i+1){System.out.println(i);
}
Java – Control Statements for Loop
Functi onality
i
1
Variable i in memory
1
Output
Check it out here
Java – Control Statements for Loop
Functi onality
i
2
Variable i in memory
1
Output
Check it out here
for(i=1; i<=10; i=i+1){System.out.println(i);
}
increment
for(i=1; i<=10; i=i+1){System.out.println(i);
}
Java – Control Statements for Loop
Functi onality
i
2
Variable i in memory
1
OutputCondition is
checked again
for(i=1; i<=10; i=i+1){System.out.println(i);
}
Java – Control Statements for Loop
Functi onality
i
2
Variable i in memory
1
OutputCondition
is True
for(i=1; i<=10; i=i+1){System.out.println(i);
}
Java – Control Statements for Loop
Functi onality
i
2
Variable i in memory
12
Output
Check it out here
Java – Control Statements for Loop
Functi onality
i
3
Variable i in memory
12
Output
Check it out here
for(i=1; i<=10; i=i+1){System.out.println(i);
}
for(i=1; i<=10; i=i+1){System.out.println(i);
}
Java – Control Statements for Loop
Functi onality
i
3
Variable i in memory
12
OutputCondition is
checked again
for(i=1; i<=10; i=i+1){System.out.println(i);
}
Java – Control Statements for Loop
Functi onality
i
3
Variable i in memory
12
OutputCondition
is True
for(i=1; i<=10; i=i+1){System.out.println(i);
}
Java – Control Statements for Loop
Functi onality
i
3
Variable i in memory
123
Output
Check it out here
for(i=1; i<=10; i=i+1){System.out.println(i);
}
Java – Control Statements for Loop
Functi onality
i
4
Variable i in memory
123
Output
Check it out here
for(i=1; i<=10; i=i+1){System.out.println(i);
}
Java – Control Statements for Loop
Functi onality
i
4
Variable i in memory
123
OutputCondition is
checked again
for(i=1; i<=10; i=i+1){System.out.println(i);
}
Java – Control Statements for Loop
Functi onality
i
4
Variable i in memory
123
OutputCondition
is True
for(i=1; i<=10; i=i+1){System.out.println(i);
}
Java – Control Statements for Loop
Functi onality
i
4
Variable i in memory
1234
Output
Check it out here
Java – Control Statements for Loop
Functi onality
i
5
Variable i in memory
1234
Output
Check it out here
for(i=1; i<=10; i=i+1){System.out.println(i);
}
for(i=1; i<=10; i=i+1){System.out.println(i);
}
Java – Control Statements for Loop
Functi onality
i
5
Variable i in memory
1234
Output Condition is checked again
Java – Control Statements for Loop
This process will continue till the condition become false
Suppose value of i is 9 now
for(i=1; i<=10; i=i+1){System.out.println(i);
}
Java – Control Statements for Loop
Functi onality
i
9
Variable i in memory
12345678
OutputCondition
is True
for(i=1; i<=10; i=i+1){System.out.println(i);
}
Java – Control Statements for Loop
Functi onality
i
9
Variable i in memory
123456789
Output
Check it out here
Java – Control Statements for Loop
Functi onality
i
10
Variable i in memory
123456789
Output
Check it out here
for(i=1; i<=10; i=i+1){System.out.println(i);
}
for(i=1; i<=10; i=i+1){System.out.println(i);
}
Java – Control Statements for Loop
Functi onality
i
10
Variable i in memory
123456789
OutputCondition is
checked again
for(i=1; i<=10; i=i+1){System.out.println(i);
}
Java – Control Statements for Loop
Functi onality
i
10
Variable i in memory
123456789
Output
Condition is Still True
for(i=1; i<=10; i=i+1){System.out.println(i);
}
Java – Control Statements for Loop
Functi onality
i
10
Variable i in memory
123456789
10
Output
Check it out here
Java – Control Statements for Loop
Functi onality
i
11
Variable i in memory
123456789
10
Output
Check it out here
for(i=1; i<=10; i=i+1){System.out.println(i);
}
for(i=1; i<=10; i=i+1){System.out.println(i);
}
Java – Control Statements for Loop
Functi onality
i
11
Variable i in memory
123456789
10
OutputCondition is
checked again
for(i=1; i<=10; i=i+1){System.out.println(i);
}
Java – Control Statementsfor Loop
Functi onality
i
11
Variable i in memory
123456789
10
Output Condition is FALSE
for(i=1; i<=10; i=i+1){System.out.println(i);
}
Java – Control Statements for Loop
Functi onality
i
11
Variable i in memory
123456789
10
Output
Skip the body of the lop and
executes the statement just after the loop
Java – Control Statements for Loop
Functi onality
i
11
Variable i in memory
123456789
10
Output
Final value of i after the
complete loop
for(i=1; i<=10; i=i+1){System.out.println(i);
}
for(i=1; i<=10; i=i+1){System.out.println(i);
}
Java – Control Statements for Loop
Functi onality
i
11
Variable i in memory
123456789
10
Output Final output of the loop
Java – Control Statements do-while Loop
Syntax
while(condition){
Statements
}
True
False
Java – Control Statements do-while Loop
Example: print values from 1 to ten
Java – Control Statements do-while Loop
Example: print values from 1 to ten
Java – Control Statements do-while Loop
Functi onality
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
InitializationstatementInitialization Statement is used to initialize a variable/ counter.
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
Java – Control Statements do-while Loop
Functi onality
Condition Statement
The condition statement controls the execution of loop
The loop executes till the condition statement is true
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
Java – Control Statements do-while Loop
Functi onality
Execution StatementThe execution statements are the main body of a loop
All action statements of loop are written here
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
Java – Control Statements do-while Loop
Functi onality
Increment/ Decrement
This section is used to increment or decrement the variable value
Java – Control Statements do-while Loop
Functi onality
i
1
Variable i in memory
Output
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
Java – Control Statements do-while Loop
Functi onality
i
1
Variable i in memory
OutputNo condition
checking at entry of the loop
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
Java – Control Statements do-while Loop
Functi onality
i
1
Variable i in memory
1
Output
Check it out here
Java – Control Statements do-while Loop
Functi onality
i
2
Variable i in memory
1
Output
Check it out here
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
Java – Control Statements do-while Loop
Functi onality
i
2
Variable i in memory
1
Output
Condition is checked again
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
Java – Control Statements do-while Loop
Functi onality
i
2
Variable i in memory
1
Output
Condition is True
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
Java – Control Statements do-while Loop
Functi onality
i
2
Variable i in memory
12
Output
Check it out here
Java – Control Statements do-while Loop
Functi onality
i
3
Variable i in memory
12
Output
Check it out here
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
Java – Control Statements do-while Loop
Functi onality
i
3
Variable i in memory
12
Output
Condition is checked again
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
Java – Control Statements do-while Loop
Functi onality
i
3
Variable i in memory
12
Output
Condition is True
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
Java – Control Statements do-while Loop
Functi onality
i
3
Variable i in memory
123
Output
Check it out here
Java – Control Statements do-while Loop
Functi onality
i
4
Variable i in memory
123
Output
Check it out here
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
Java – Control Statements do-while Loop
Functi onality
i
4
Variable i in memory
123
Output
Condition is checked again
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
Java – Control Statements do-while Loop
Functi onality
i
4
Variable i in memory
123
Output
Condition is True
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
Java – Control Statements do-while Loop
Functi onality
i
4
Variable i in memory
1234
Output
Check it out here
Java – Control Statements do-while Loop
Functi onality
i
5
Variable i in memory
1234
Output
Check it out here
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
Java – Control Statements do-while Loop
Functi onality
i
5
Variable i in memory
1234
Output
Condition is checked again
Java – Control Statements do-while Loop
This process will continue till the condition become false
Suppose value of i is 9 now
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
Java – Control Statements do-while Loop
Functi onality
i
9
Variable i in memory
12345678
Output
Condition is True
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
Java – Control Statements do-while Loop
Functi onality
i
9
Variable i in memory
123456789
Output
Check it out here
Java – Control Statements do-while Loop
Functi onality
i
10
Variable i in memory
123456789
Output
Check it out here
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
Java – Control Statements do-while Loop
Functi onality
i
10
Variable i in memory
123456789
Output
Condition is checked again
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
Java – Control Statements do-while Loop
Functi onality
i
10
Variable i in memory
123456789
Output
Condition is Still True
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
Java – Control Statements do-while Loop
Functi onality
i
10
Variable i in memory
123456789
10
Output
Check it out here
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
Java – Control Statements do-while Loop
Functi onality
i
11
Variable i in memory
123456789
10
Output
Check it out here
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
Java – Control Statements do-while Loop
Functi onality
i
11
Variable i in memory
123456789
10
Output Condition is checked again
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
Java – Control Statements do-while Loop
Functi onality
i
11
Variable i in memory
123456789
10
Output
Condition is FALSE
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
Java – Control Statements do-while Loop
Functi onality
i
11
Variable i in memory
123456789
10
Output
Stop the execution of the
loop and executes the
statement just after the loop
Java – Control Statements do-while Loop
Functi onality
i
11
Variable i in memory
123456789
10
Output
Final value of i after the
complete loop
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
Java – Control Statements do-while Loop
Functi onality
i
11
Variable i in memory
123456789
10
Output
i=1;do{
System.out.println(i);i=i+1;
}while(i<=10);
Final output of the loop
Recommended