Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> Java >> Snippet

08 Conditional Flow


break may be used with for , while , do - while loops
continue exits the current loop and starts over at the first statement of the next loop
loop can have a name (name follow by colon)
break LoopName


if ( boolean expression )
   { statements }
else if
   { statements }
else
   { statements }


switch ( boolean expression )   {
   case 'A' :
      Statement(s);
      Break
   case 'B' :
      Statement(s);
      Break
   case 'C' :
      Statement(s);
      Break
   default :
      Statement(s)
}


ternary operator
( boolean expression ) ?     < if - true - expression >   :   < if - false - expression >


while ( boolean expression )
      // executes 0 or more times
      { statements }
}

do {
      //executes at least one time
      { statements }
while ( boolean expression ) }


for ( < init - stmts > ;    < boolean expression > ;   < exprs > )
   { statements }

Example :
for ( int number = 0 ; number < 1000 ; number ++ ) {
   if (number % 12 == 0) System.out.println( "#: " + number) ;
   // prints all numbers evenly divisible by 12
}

Example 2 :
for ( i = 0 , j = 0 ; i * j < 1000 ; i ++ , j += 2) {
   //allowed to use more than one variable in the loop
  System.outlprintln(i + " * " + j   + " = " + i * j) ;

Example 3 :
for (   ;  Count < MaxCount ; Count ++ )
   // section of the loop can be missing
}