Conditions & Loops

if statement structure

if (Logical argument) {Do something}
else if (Logical arument) {Do something}
else {Do something}

ex: 
if (x < y)
  {System.out.println("x is less than y!");}
else if (y < x)
  {System.out.println("y is less than x!");}
else
  {System.out.pringln("x = y");}

while statement structure (Uses the argument to determine if it needs to run)

while (logical argument) {Do something}

do statements structure (Will run once regardless of the argument)

do {Do somethin} while (logical argument);

for loops

for ( int n = 1 /* starting # */ ; n <= 10 /* ending # */; n = n+2 /* increment by # */)
  {Do something;}

switch statement structure

  • controlling expression enclosed in parentheses after the keyword switch
    • The controlling expression must evaluate to a char, int, short, or byte
  • Each branch statement in a switch statement starts with the reserved word case, followed by a constant called a case label, followed by a colon, and then a sequence of statements
    • Each case label must be of the same type as the controlling expression
    • Case labels need not be listed in order or span a complete interval, but each one may appear only once
    • Each sequence of statements may be followed by a break statement ( break;)
  • There can also be a section labeled default:
    • The default section is optional, and is usually last
    • Even if the case labels cover all possible outcomes in a given switch statement, it is still a good practice to include a default section
    • It can be used to output an error message, for example
  • When the controlling expression is evaluated, the code for the case label whose value matches the controlling expression is executed
    • If no case label matches, then the only statements executed are those following the default label (if there is one)
  • The switch statement ends when it executes a break statement, or when the end of the switch statement is reached
    • When the computer executes the statements after a case label, it continues until a break statement is reached
    • If the break statement is omitted, then after executing the code for one case, the computer will go on to execute the code for the next case
    • If the break statement is omitted inadvertently, the compiler will not issue an error message
switch (Controlling_Expression)
{
  case Case_Label_1:
           Statement_Sequence_1
           break;
  case Case_Label_2:
           Statement_Sequence_2
           break;
 
  case Case_Label_n:
           Statement_Sequence_n
           break;
  default:
           Default_Statement Sequence
           break;
}

break; // ends nearest enclosing switch or loop

  • break someIdentifier; // ¬†end any containing loop, not just the innermost loop
    • To label a loop, simply precede it with an Identifier and a colon:
      someIdentifier:

continue; // statement ends the current loop body iteration of the nearest enclosing loop statement

exit; // statement will immediately end the program as soon as it is invoked