Java control flow statements
-
Upload
future-programming -
Category
Education
-
view
172 -
download
0
description
Transcript of Java control flow statements
![Page 1: Java control flow statements](https://reader033.fdocuments.net/reader033/viewer/2022052322/557cc59ed8b42a43438b4947/html5/thumbnails/1.jpg)
JAVA JAVA CONTROL CONTROL
STATEMENTSSTATEMENTS
ByBy
Hiren K. VaghasiyaHiren K. Vaghasiya
![Page 2: Java control flow statements](https://reader033.fdocuments.net/reader033/viewer/2022052322/557cc59ed8b42a43438b4947/html5/thumbnails/2.jpg)
CONTROL STATEMENTSCONTROL STATEMENTS
• if elseif else
• switchswitch
• whilewhile
• do whiledo while
• forfor
• breakbreak
• continuecontinue
• returnreturn
• Labeled break, continueLabeled break, continue
![Page 3: Java control flow statements](https://reader033.fdocuments.net/reader033/viewer/2022052322/557cc59ed8b42a43438b4947/html5/thumbnails/3.jpg)
IF-ELSEIF-ELSE
if(conditional_statement){if(conditional_statement){
statement to be executed if conditions becomes statement to be executed if conditions becomes
truetrue
}else{}else{
statements to be executed if the above condition statements to be executed if the above condition
becomes falsebecomes false
}}
![Page 4: Java control flow statements](https://reader033.fdocuments.net/reader033/viewer/2022052322/557cc59ed8b42a43438b4947/html5/thumbnails/4.jpg)
SWITCHSWITCH
switch(byte/short/int){switch(byte/short/int){
case expression:case expression:
statementsstatements
case expression:case expression:
statementsstatements
default:default:
statementstatement
}}
![Page 5: Java control flow statements](https://reader033.fdocuments.net/reader033/viewer/2022052322/557cc59ed8b42a43438b4947/html5/thumbnails/5.jpg)
WHILE - LOOPWHILE - LOOP
while(condition_statementwhile(condition_statementtrue){true){
Statements to be executed when the condition becomes Statements to be executed when the condition becomes
true and execute them repeatedly until condition true and execute them repeatedly until condition
becomes false.becomes false.
}}
E.g.E.g.
int x =2;int x =2;
while(x>5){while(x>5){
system.out.println(“value of x:”+x);system.out.println(“value of x:”+x);
x++;x++;
}}
![Page 6: Java control flow statements](https://reader033.fdocuments.net/reader033/viewer/2022052322/557cc59ed8b42a43438b4947/html5/thumbnails/6.jpg)
DO WHILE - LOOPDO WHILE - LOOP
do{do{
statements to be executed at least once without statements to be executed at least once without
looking at the condition. looking at the condition.
The statements will be exeucted until the condition The statements will be exeucted until the condition
becomes true.becomes true.
}while(condition_statement);}while(condition_statement);
![Page 7: Java control flow statements](https://reader033.fdocuments.net/reader033/viewer/2022052322/557cc59ed8b42a43438b4947/html5/thumbnails/7.jpg)
FOR - LOOPFOR - LOOP
for(for(initializationinitialization; condition; ; condition; increment/decrementincrement/decrement){){
statements to be executed until the condition becomes statements to be executed until the condition becomes
falsefalse
}}
E.g:E.g:
for(int x=0; x<10;x++){for(int x=0; x<10;x++){
System.out.println(“value of x:”+x);System.out.println(“value of x:”+x);
}}
![Page 8: Java control flow statements](https://reader033.fdocuments.net/reader033/viewer/2022052322/557cc59ed8b42a43438b4947/html5/thumbnails/8.jpg)
BREAKBREAK
• Break is used in the loops and when executed, the Break is used in the loops and when executed, the
control of the execution will come out of the loop.control of the execution will come out of the loop.
for(int i=0;i<50;i++){if(i%13==0){break;}System.out.println(“Value of i:”+i);}
![Page 9: Java control flow statements](https://reader033.fdocuments.net/reader033/viewer/2022052322/557cc59ed8b42a43438b4947/html5/thumbnails/9.jpg)
CONTINUECONTINUE
• Continue makes the loop to skip the current Continue makes the loop to skip the current
execution and continues with the next iteration.execution and continues with the next iteration.
for(int i=0;i<50;i++){for(int i=0;i<50;i++){
if(i%13==0){if(i%13==0){
continue;continue;
}}
System.out.println(“Value of i:”+i);System.out.println(“Value of i:”+i);
}}
![Page 10: Java control flow statements](https://reader033.fdocuments.net/reader033/viewer/2022052322/557cc59ed8b42a43438b4947/html5/thumbnails/10.jpg)
RETURNRETURN
• returnreturn statement can be used to cause execution statement can be used to cause execution
to branch back to the caller of the method.to branch back to the caller of the method.
![Page 11: Java control flow statements](https://reader033.fdocuments.net/reader033/viewer/2022052322/557cc59ed8b42a43438b4947/html5/thumbnails/11.jpg)
LABELED BREAK,CONTINUELABELED BREAK,CONTINUE
• Labeled break and continue statements will break Labeled break and continue statements will break
or continue from the loop that is mentioned.or continue from the loop that is mentioned.
• Used in nested loops.Used in nested loops.
![Page 12: Java control flow statements](https://reader033.fdocuments.net/reader033/viewer/2022052322/557cc59ed8b42a43438b4947/html5/thumbnails/12.jpg)
WWW.FUTUREPROGRAMMING.INWWW.FUTUREPROGRAMMING.IN
The End
The End