12
JAVA CONTROL JAVA CONTROL STATEMENTS STATEMENTS By By Hiren K. Vaghasiya Hiren K. Vaghasiya

Java control flow statements

Embed Size (px)

DESCRIPTION

Java Control Flow Statement Description. Learn Java.

Citation preview

Page 1: Java control flow statements

JAVA JAVA CONTROL CONTROL

STATEMENTSSTATEMENTS

ByBy

Hiren K. VaghasiyaHiren K. Vaghasiya

Page 2: Java control flow statements

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

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

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

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

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

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

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

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

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

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

WWW.FUTUREPROGRAMMING.INWWW.FUTUREPROGRAMMING.IN

The End

The End