Conditional and Loop Control Statements

Conditional and Loop Control Statements

Conditional and Loop Control statements in Java

Conditional statements

  1. The if Statement

Use the if statement to specify a block of Java code to be executed if a condition is true.

Syntax

    if (condition) {
      // block of code to be executed if the condition is true
    }

Note

if is in lowercase letters. Uppercase letters (If or IF) will generate an error.

In the example below, we test two values to find out if 20 is greater than 18. If the condition is true, print some text:

Example

    if (20 > 18) {
      System.out.println("20 is greater than 18");
    }
  1. The else Statement

Use the else statement to specify a block of code to be executed if the condition is false.

Syntax

    if (condition) {
      // block of code to be executed if the condition is true
    } else {
      // block of code to be executed if the condition is false
    }

Example 1

    int time = 20;
    if (time < 18) {
      System.out.println("Good day.");
    } else {
      System.out.println("Good evening.");
    }

Output

Good evening

Example 2

int n = 5;

if(n%2==0) {
  System.out.println("n is even");
}
else {
  System.out.println("n is odd");
}

Output

n is odd
  1. The else if Statement

Use the else if statement to specify a new condition if the first condition is false.

Syntax

    if (condition1) {
      // block of code to be executed if condition1 is true
    } else if (condition2) {
     // block of code to be executed if the condition1 is false and condition2 is true
    } else {
     // block of code to be executed if the condition1 is false and condition2 is false
    }

Example 1

    int time = 22;
    if (time < 10) {
      System.out.println("Good morning.");
    } else if (time < 20) {
      System.out.println("Good day.");
    } else {
      System.out.println("Good evening.");
    }

Output

Good evening.

Example 2

public class Main{
    public static void main(String[] args) {
        int a = 10;
        int b = 3;
        if (a > 5 && b > 5) {
            System.out.println(" a>5, b>5");
        } else if (a > 5 && b < 5) {
            System.out.println(" a>5, b<5");
        } else if (a < 5 && b > 5) {
            System.out.println(" a<5, b>5");
        } else if (a < 5 && b < 5) {
            System.out.println(" a<5, b<5");
        }
    }
}

Output

a>5, b<5

Ternary operator

Syntax:

condition ? true : false

Example:

string result;

result = (6>5) ? "6 is greater than 5" : " 6 is not greater than 5" ;
System.out.println(result);

Output

6 is greater than 5

Nested if Conditions

Syntax:

if(condition1)
{
   if(condition2) 
   {
      //Statements
   } else 
   {
      //Statements
   }
} else 
{
   // Statements
}

Example

public class Main{
    public static void main(String[] args) {
        int a = 10;
        int b = 3;
        if (a > 5) {

            if (b > 5) {
                System.out.println("a>5 , b>5");
            } else {
                System.out.println("a>5 , b<5");
            }
        } else {
            if (b > 5) {
                System.out.println("a<5 , b>5");
            } else {
                System.out.println("a<5 , b<5");
            }
        }
    }
}

Output

a>5 , b<5

Switch Statement

The switch statement evaluates an value and matches the value to various cases.

It then executes the code associated with each statement until it encounters a break statement.

Syntax:

switch (expression) {

     case value1:

    //Statements executed when the

    //result of expression matches value1

        break;

     case value2:

    //Statements executed when the

    //result of expression matches value2

        break;

       ...


      case valueN:

    //Statements executed when the

    //result of expression matches valueN

       break;

      default:

    //Statements executed when none of

    //the values match the value of the expression

       break;
     }

Example:

int day = 6

switch (day) {

  case 0:

    day = "Sunday";

    break;

  case 1:

    day = "Monday";

    break;

  case 2:

    day = "Tuesday";

    break;

  case 3:

    day = "Wednesday";

    break;

  case 4:

    day = "Thursday";

    break;

  case 5:

    day = "Friday";

    break;

  case 6:

    day = "Saturday";

    break;

  default:

    day = "Invalid Input";
}

System.out.println(day);

Output

Saturday

Loop Control statements

Loops can execute a block of code as long as a specified condition is reached.

Loops are handy because they save time, reduce errors, and they make code more readable.

While Loop

The while loop loops through a block of code as long as a specified condition is true:

Syntax

 while (condition) {
   // code block to be executed
 }

Example

int i = 0;
while (i < 5) {
   System.out.println(i);
   i++;
}

Output

0 1 2 3 4

Note:

Do not forget to increase the variable used in the condition, otherwise the loop will never end!

Do/While Loop

The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

Syntax

do {
  // code block to be executed
  }
while (condition);

The loop will always be executed at least once, even if the condition is false, because the code block is executed before the condition is tested:

Example

int i = 10;
do {
   System.out.println(i);
   i++;
  }
while (i < 5);

Output

10

For Loop:

When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:

Syntax

for (statement 1; statement 2; statement 3) {
  // code block to be executed
}

Statement 1 is executed (one time) before the execution of the code block.

Statement 2 defines the condition for executing the code block.

Statement 3 is executed (every time) after the code block has been executed.

Example 1

for (int i = 0; i < 5; i++) {
  System.out.println(i);
}

Output

0 1 2 3 4

Example2

for (int i = 0; i <= 10; i = i + 2) {
  System.out.println(i);
}

Output

0 2 4 6 8 10

Break Statement

The break statement can also be used to jump out of a loop.

This example jumps out of the loop when i is equal to 4:

Example

for (int i = 0; i < 10; i++) {
  if (i == 4) {
    break;
}
  System.out.println(i);
}

Output

0 1 2 3

Continue Example

Example

int i = 0;
while (i < 10) {
  if (i == 4) {
    i++;
    continue;
  }
  System.out.println(i);
  i++;
 }

Output

0 1 2 3 5 6 7 8 9