Java control flow statements

Preview:

DESCRIPTION

Java Control Flow Statement Description. Learn Java.

Citation preview

JAVA JAVA CONTROL CONTROL

STATEMENTSSTATEMENTS

ByBy

Hiren K. VaghasiyaHiren K. Vaghasiya

CONTROL STATEMENTSCONTROL STATEMENTS

• if elseif else

• switchswitch

• whilewhile

• do whiledo while

• forfor

• breakbreak

• continuecontinue

• returnreturn

• Labeled break, continueLabeled break, continue

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

}}

SWITCHSWITCH

switch(byte/short/int){switch(byte/short/int){

case expression:case expression:

statementsstatements

case expression:case expression:

statementsstatements

default:default:

statementstatement

}}

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++;

}}

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);

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);

}}

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);}

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);

}}

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.

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.

WWW.FUTUREPROGRAMMING.INWWW.FUTUREPROGRAMMING.IN

The End

The End

Recommended