Loops & Conditional Statements

Conditional Statements

if statement

: An if statement tells the program that it must carry out a specific piece of code if a condition test evaluates to true

if( condition ) {
    statement;
} else{
    statement
}

switch statement

  • a multi-way branch statement.

  • provides an easy way to dispatch execution to different parts of code based on the value of the expression

switch(argument value) { // Not a conditional expression, but an argument value!

 // -> Only data types that can be promoted to int can be used as the argument value!

  // ex) Integer types (byte, short, int), character type (char)



 case condition value 1:     // => It's a colon(:), not a semicolon(;)!!!!!

  statement; // -> Statement to be executed when the conditional expression result matches the condition value

break;  // => break must be included for proper operation

 case condition value 2:

  statement;

break;

case condition value 3:

  statement;

break;

case condition value 4:

  statement;

break;

 Default:  // => default is for exiting when none of the conditions match!

  statement;

Loops

for loop

  • A for loop is divided into three parts, an initialization part, a conditional part and an increment part

  • You should sett all initial values in the initialization part of the loop.

  • A true from the condition part will execute subsequent statements bounded by {} brackets. A false from the condition part will end the loop.

  • For each loop the increment part will be executed.

Nested for loop

Enhanced for loop

: The enhanced for loop can be used to loop over arrays of any type as well as any kind of Java object that implements the java.lang.Iterable interface.

while loop

: Compare first, then process

-> The conditional expression cannot be omitted! To make the condition always true, use true

do ~ while loop

: Executes at least once even if the condition is not met

break statement

: A control statement that exits the loop containing the break

-> The break statement exits the nearest loop!

continue statement

: Used when you want to skip certain statements

-> When continue is encountered, the statements below continue are not processed, and it moves to the increment expression for the next iteration (if there is no increment expression, it moves to the condition!)

Difference between break and continue statements

: Whether or not you exit the loop!

-> The continue statement does not exit the loop; instead, it moves to the loop's conditional expression for the next iteration!

The return statement terminates the function itself

Last updated