maxtek IoT

    Control structures

As in most programming languages, Java handles flow of control with branching and looping statements. Java branching and looping statements are the same as in the C and C++ languages and are very similar to those in other programming languages. Most branching and looping statements are controlled by Boolean expressions. A Boolean expression is any expression that is either true or false.

     if-else Statements

An if-else statement chooses between two alternative statements based on the value of a Boolean expression.
The syntax for an if-else statement is given below.
If the Boolean expression in parentheses (after the if) evaluates to true, then the statement before the else is executed. If the Boolean expression evaluates to false, then the statement after the else is executed.

      Syntax


       if (Boolean_Expression)
       {
        Yes_Statement
        }
        else
        {
        No_Statement
        }
       
       
      Example:

        if (time < limit)
        {
        System.out.println("You made it.");
        }
        else
        {
        System.out.println("You missed the deadline.");
        }

      The switch Statement

The switch statement is the only other kind of Java statement that implements multiway branches.

When a switch statement is executed, one of a number of different branches is executed. The choice of which branch to execute is determined by a controlling expression given in parentheses after the keyword switch. Following this are a number of occurrences of the reserved word case followed by a constant and a colon. These constants are called case labels. The controlling expression for a switch statement must be one of the types char, int, short, byte, or String . The case labels must all be of the same type as the controlling expression. No case label can occur more than once, because that would be an ambiguous instruction. There may also be a section labeled default:, which is usually last.

When the switch statement is executed, the controlling expression is evaluated and the computer looks at the case labels. If it finds a case label that equals the value of the controlling expression, it executes the code for that case label.

The switch statement ends when either a break statement is executed or the end of the switch statement is reached. A break statement consists of the keyword break followed by a semicolon. When the computer executes the statements after a case label, it continues until it reaches a break statement. When the computer encounters a break statement, the switch statement ends. If you omit the break statements, then after executing the code for one case, the computer will go on to execute the code for the next case.

      Syntax



        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;

       Loop

Looping mechanisms in Java are similar to those in other high-level languages. The three Java loop statements are the while statement, the do-while statement, and the for statement. The same terminology is used with Java as with other languages. The code that is repeated in a loop is called the body of the loop. Each repetition of the loop body is called an iteration of the loop.

       while loop

The first thing that happens when a while loop is executed is that the controlling Boolean expression is evaluated. If the Boolean expression evaluates to false at that point, the body of the loop is never executed.

       syntax



      while (Boolean_Expression)
      {
      Statement
      }


Example
      int i= 0;
      while(i<3)
      {
      System.out.println("programming is fun");
      i++
      }

This program displays:
      programming is fun
      programming is fun
      programming is fun.

The program first check the while condition zero is it less than three? which is evaluated to true and it displays programming is fun. next the value of the variable i is incremented to 1 that's the meaning of i++ or i=i+1. So the new value of i becomes i=0+1=1 which is less than 3 and the body of the loop is executed again till the boolean expression is evaluated to false.

      do - while

The body of the loop is executed first, and the Boolean expression is checked after the loop body is executed. Thus, the do-while statement always executes the loop body at least once. After this start-up, the while loop and the do-while loop behave the same way. After each iteration of the loop body, the Boolean expression is again checked, and if it is true, the loop is iterated again. If it has changed from true to false, then the loop statement ends.

      Syntax

      do
      {
      Statements;
      } while (Boolean condition);

      for loop

The third and final loop statement in Java is the for statement. The for statement is most commonly used to step through some integer variable in equal increments.

A for statement begins with the keyword for followed by three expressions in parentheses that tell the computer what to do with the controlling variable(s). The beginning of a for statement looks like the following:

      for (Initialization; Boolean_Expression; Update)
       {
        statement;
        }
The first expression tells how the variable, variables, or other things are initialized, the second expression gives a Boolean expression that is used to check for when the loop should end, and the last expression tells how the loop control variable or variables are updated after each iteration of the loop body. The loop body is a single statement (typically a compound statement) that follows the heading we just described.

The Body may be any Java statement either a simple statement or a compound statement consisting of a list of statements enclosed in braces {}.
Notice that the three things in parentheses are separated by two semicolons not three. Example
        for (int n = 1; n < 10; n++)
       {
System.out.println(n);
       }
This program dispalys integer values from 1 to 9