What are expression, blocks, conditional and branching statements in java

0
4852

In this tutorial we will learn about expression, blocks, conditional and branching statements along with their implementation in java.

  • Detailed explanation about what are expression,blocks, conditional and branching statements in java is given below :

  1. Expressions:- For building the blocks of any java program, expressions are used. It is built using operators, variables, values and method calls. In java, regular expression defines the pattern of a string. It can be used to edit, manipulate and search text. It differ’s slightly for each language but it is not language specific.
  2. Statement: – It is nothing but more than one expression followed by semicolon. The group of statement is called as statement block. It may also include variable declarations.
    1. Control Statements: –
      1. If Statement: – Conditionally execute single statement.
      2. If-Else Statement: – Conditionally select one of the two statements and execute it.
      3. Switch Statement: – Conditionally choose one of the several statements and execute it.
    2. Repetitive Statements: – It has three types of loop statements: for loop, while loop, do while loop. These statements are allowed as nested statement to be executed repetitively. Each execution of nested statement is called iteration of the loop. These iterations are controlled by a boolean expression. In while loops and for loops boolean expression is evaluated before each iteration. In do-while loop boolean expression is evaluated after each iteration. If the boolean expression becomes false the loop is terminated.
      1. While Loop: – In this case boolean expression, can be true or false. It is a pretest loop statement. The boolean expression is tested start of the each iteration of the loop. The loop terminates when it is false.
      2. While Loop
        Example : This example shows how while loop is used in a class.

        Output :

      3. For Loop: – It is a pretest loop statement. In this case initialization and increment is an expression. A boolean expression also is an expression that can be true or false. In this case when loop is executed then first initialization expression is evaluated. This expression assigns the initial value to loop control variable (eg. j=1).
        The boolean expression is tested start of the each iteration of the loop. The loop terminates when it is false and expression is frequently a comparison (j<=10). After that increment expression is evaluated, that can increment the control variable (eg. j++).
      4. For Loop
        Example : This example shows how to use for loop in a class.

        Output :

      5. Do-While Loop:- It is a post-test loop statement. In this case boolean expression is tested after each iteration of the loop. If it is false that means loop is terminated.
      6. do while loop

        Example : This Java Example shows how to use do- while loop to iterate in Java program.

        Output :

    3. Special Control Statements: –
      1. Return Statement: – It is used to terminate execution of the method and in the definition of a method to initialize return value.
        It can be used in two ways:

        • Method with returned type void.
        • Method with returned type non-void.
      2. return statement
        Example : This example shows how to write return in a class.

        Output :

      3. Continue Statement: – This statement is used to terminate an iteration of the loop in for loop, while and do-while loop.
        The boolean expression and its increment are evaluated after the continue statement is executed in a for loop. In this case nested statements are executed again if and only if the boolean expression is true.
        The boolean expression is evaluated after the continue statement is executed in a while loop and do-while loop. In this case nested statements are executed again if and only if the boolean expression is true.
      4. Continue Statement

        Example : This example shows how to use java continue statement to skip the iteration of the loop in a class

        Output :

      5. Break Statement : – It is used to terminate execution of the statement in loop statements like for loop, while loop, do-while loop and also in switch statements.
      6. Break Statement

        Example : This Java Example shows how to break the loop or condition in a class.

        Output :

  3. Blocks :- A block is nothing but the group of zero or more than zero statements between balanced braces.
  4. Example :

    Output :

  5. Loops and Branching : –
    • Loops: –
      1. do : – In this case, do loop body is always executed at least once. Semicolon is always required after the conditional expression.
      2. Example : This Java Example shows how to use do while loop to iterate in Java program.

        Output :

      3. while : – This keyword indicates that the loop is executed continuously upto condition is true.
      4. While

        Example :

        Output :

      5. for : – It is a pretest loop statement. In this case initialization and increment is an expression. A boolean expression also is an expression that can be true or false. In this case when loop is executed then first initialization expression is evaluated. This expression assigns the initial value to loop control variable (eg. j=1). The boolean expression is tested start of the each iteration of the loop. The loop terminates when it is false and expression is frequently a comparison (j<=10). After that increment expression is evaluated, that can increment the control variable (eg. j++).
      6. For Loop
        Example : This example shows how to use for loop in a class.

        Output :

    • Branching : –
      1. break : – The break keyword is used to exit a for loop, while loop, do loop or to the end of a case block in a switch statement.
      2. Break Statement

        Example : This Java Example shows how to break the loop or condition in a class.

        Output :

      3. continue : – Continue is helpful for skipping the next iteration of a do, while or for loop statement.
      4. continue statement

        Example : This example shows how to use java continue statement to skip the iteration of the loop in a class

        Output :

  6. Hence, we now have sound knowledge about what are expression, blocks, conditional and branching statements in java

LEAVE A REPLY

Please enter your comment!
Please enter your name here