Home | | Object Oriented Programming | Java - Control Statements

Chapter: Object Oriented Programming(OOP) : Overview of Java

Java - Control Statements

Types • if else • switch • while • do while • for • break • continue • return • Labeled break, continue

CONTROL STATEMENTS

 

Types

•      if else

•      switch

•      while

•      do while

•      for 

•      break

•      continue 

•      return

•      Labeled break, continue

 

if-else if(conditional_statement){

 

statement to be executed if conditions becomes true }else{

 

statements to be executed if the above condition becomes false

}

 

Switch switch(byte/short/int){ case expression: statements

 

case expression: statements default: statement

}

 

while – loop while(condition_statementàtrue){

 

Statements to be executed when the condition becomes true and execute them repeatedly until condition becomes false.

 

}

E.g.

 

int x =2; while(x>5){

 

system.out.println(“value of x:”+x); x++;

 

}

 

do while – loop do{

 

statements to be executed at least once without looking at the condition. The statements will be exeucted until the condition becomes true. }while(condition_statement);

 

for – loop

 

for(initialization; condition; increment/decrement){ statements to be executed until the condition becomes false

}

E.g:

for(int x=0; x<10;x++){

System.out.println(“value of x:”+x);

}

Break

 

•      Break is used in the loops and when executed, the 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);

 

•      }

Continue

 

•      Continue makes the loop to skip the current execution and continues with the next iteration.

 

for(int i=0;i<50;i++){ if(i%13==0){ continue;

}

System.out.println(“Value of i:”+i);

}

Return

 

return statement can be used to cause execution to branch back to the caller of the method Labeled break,continue

 

•      Labeled break and continue statements will break or continue from the loop that is mentioned.

 

•      Used in nested loops.

•         Primitive data types

 

–   char, byte, short, int, long, float, double, boolean

–   Building blocks for more complicated types

•         All variables must have a type before being used

•         Strongly typed language

–   Primitive types portable, unlike C and C++

•         In C/C++, write different versions of programs

–   Data types not guaranteed to be identical

–   ints may be 2 or 4 bytes, depending on system

•         WORA - Write once, run anywhere

–   Default values

•         boolean gets false, all other types are 0

 

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Object Oriented Programming(OOP) : Overview of Java : Java - Control Statements |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2027 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.