Control structure in java

Control Structures in Java

Java provides special blocks of statements that allow great control over the execution of statements in a program. These are referred to as the control structures in Java.

Types of Control Structures

Java supports 5 types of control structures to control the flow of a program.

Sequential Control Structure:

  • If the programmer uses no control structure within a Java program, the control moves forward to the next statements sequentially. This scenario is known as Sequential Control Structure.
  • By default, all the statements execute sequentially one by one.

Conditional Control Structure:

  • We can also call these statements as decision making control structure.
  • They let us make a decision based upon the result of a condition.
  • These statements will execute a block of code conditionally depending on whether the condition is true or false.
  • If the condition is true, then the block of code will execute otherwise it will be skipped.
  • In some cases, there are 2 statement blocks one for the true value and other for the false value.
  • Moreover, the control will not go to the next statement until and unless the condition is satisfied.
  • For example,
    • if
    • if..else
    • if..else ladder (multiple if)
    • nested if

    Repetition Control Structure:

    • The looping statements help to execute a set of instructions repeatedly as long as the condition is true.
    • It repeats some portion of the program either a specified number of times or until a particular condition is satisfied.
    • This process of repeating some portion of the program is called looping or branching.
    • Sometimes we will provide the number of repetitions whereas in some cases we may not be aware of the number of repetitions.
    • For example:
      • for loop
      • while loop
      • do..while
      • for each

      Selection Control Structure:

      • This control structure allows us to make a decision from a number of choices.
      • Mostly, menu-driven programs use switch cases, where we have to choose a relevant option out of multiple options.
      • Each option has a different function to perform.
      • For example:
        • the switch case

        Jump Control Structure:

        • There are 2 jump control statements i.e. break and continue.
        • These statements directly transfer control of the program without depending upon any conditions.
        • They cause a sudden jump or control transfer from the current statement to another statement somewhere else in the code.
        • Thus they can interrupt the normal flow of the program according to the need.

        In the next article, I will discuss in details all the types of decision making statements and conditional control structures available in Java. The article will cover all the types of if..else statements and simple programs based on if..else statements.

        Источник

        Control structures in Java

        A program is a list of instructions or blocks of instructions. Java provides Control structures that can change the path of execution and control the execution of instructions. In this post, we will discuss the Control structures in programming language.

        Three kinds of control structures in Java

        1. Control statements in java / Conditional Branches in java

        Java control statements use to choose the path for execution. There are some types of control statements:

        if statement

        It is a simple decision-making statement. It uses to decide whether the statement or block of statements should be executed or not. A Block of statements executes only if the given condition returns true otherwise the block of the statement skips. The if statement always accepts the boolean value and performs the action accordingly. Here we will see the simplest example of an if statement. We will discuss it in detail in a separate post. Visit here for more details.

        public class IfStatementExample < public static void main(String arg[]) < int a = 5; int b = 4; // Evaluating the expression that will return true or false if (a >b) < System.out.println("a is greater than b"); >> >

        Output: a is greater than b

        nested if statement

        The nested if statements mean an if statement inside another if statement. The inner block of the if statement executes only if the outer block condition is true. This statement is useful when you want to test a dependent condition. Here we will see the example of nested if with a common example and see how it works. Visit here for more details.

        public class NestedIfStatementExample < public static void main(String arg[]) < int age = 20; boolean hasVoterCard = true; // Evaluating the expression that will return true or false if (age >18) < // If outer condition is true then this condition will be check if (hasVoterCard) < System.out.println("You are Eligible"); >> > >

        Output: You are Eligible

        if-else statement

        In the if statement, the block of statements executes when the given condition returns true otherwise block of the statement skips. Suppose when we want to execute the same block of code if the condition is false. Here we come on the if-else statement. In an if-else statement, there are two blocks one is an if block and another is an else block. If a certain condition is true, then if block executes otherwise else block executes. Let’s see the example and understand it. Visit here for more details.

        public class IfElseStatementExample < public static void main(String arg[]) < int a = 10; int b = 50; // Evaluating the expression that will return true or false if (a >b) < System.out.println("a is greater than b"); >else < System.out.println("b is greater than a"); >> >

        Output: b is greater than a

        if-else if statement/ ladder if statements

        If we want to execute the different codes based on different conditions then we can use if-else-if. It is known as if-else if ladder. This statement executes from the top down. During the execution of conditions if any condition is found true, then the statement associated with that if statement is executed, and the rest of the code will be skipped. If none of the conditions returns true, then the final else statement executes. Let’s see the example of the ladder if and you can visit here for more details.

        public class IfElseIfStatementExample < public static void main(String arg[]) < int a = 10; // Evaluating the expression that will return true or false if (a == 1) < System.out.println("Value of a: "+a); >// Evaluating the expression that will return true or false else if(a == 5) < System.out.println("Value of a: "+a); >// Evaluating the expression that will return true or false else if(a == 10) < System.out.println("Value of a: "+a); >else < System.out.println("else block"); >> >

        Output: Value of a: 10

        Switch statement

        The switch statement in java is like the if-else-if ladder statement. To reduce the code complexity of the if-else-if ladder switch statement comes.
        In a switch, the statement executes one from multiple statements based on condition. In the switch statements, we have a number of choices and we can perform a different task for each choice. You can read it in detail, for more details.

        public class SwitchStatementExample < public static void main(String arg[]) < int a = 10; // Evaluating the expression that will return true or false switch(a) < case 1: System.out.println("Value of a: 1"); break; case 5: System.out.println("Value of a: 5"); break; case 10: System.out.println("Value of a: 10"); break; default: System.out.println("else block"); break; >> >

        Output: Value of a: 10

        2. Loops in Java / Looping statements in java

        The loop in java uses to iterate the instruction multiple times. When we want to execute the statement a number of times then we use loops in java.

        for loop

        A for loop executes the block of code several times. It means the number of iterations is fixed. JAVA provides a concise way of writing the loop structure. When we want to execute the block of code serval times, let’s see the example and visit here for more details.

        public class ForLoopExample < public static void main(String arg[]) < // Creating a for loop for(int i = 1; i > >

        Output: Iteration number: 1
        Iteration number: 2
        Iteration number: 3

        while loop

        A while loop allows code to execute repeatedly until the given condition returns true. If the number of iterations doesn’t fix, it recommends using a while loop. It is also known as an entry control loop. For more details.

        public class WhileLoopExample < public static void main(String arg[]) < // Creating a while loop int i = 1; while(i > >

        Output: Iteration number: 1
        Iteration number: 2
        Iteration number: 3

        do-while loop

        It is like a while loop with the only difference being that it checks for the condition after the execution of the body of the loop. It is also known as Exit Control Loop. For more details.

        public class DoWhileLoopExample < public static void main(String arg[]) < // Creating a while loop int i = 1; do < System.out.println("Iteration number: "+i); i++; >while(i >

        Output: Iteration number: 1
        Iteration number: 2
        Iteration number: 3

        3. Branching Statements in Java

        These use to alter the flow of control in loops.

        If a break statement encounters inside a loop, the loop immediately terminates, and the control of the program goes to the next statement following the loop. It is used along with the if statement in loops. If the condition is true, then the break statement terminates the loop immediately.

        public class BreakStatementExample < public static void main(String arg[]) < for(int i = 0; i > >

        Output: Iteration number:0
        Iteration number:1
        Iteration number:2

        continue

        The continue statement uses in a loop control structure. If you want to skip some code and need to jump to the next iteration of the loop immediately. Then you can use a continue statement. It can be used in for loop or a while loop. Read it in detail

        public class ContinueStatementExample < public static void main(String arg[]) < for(int i = 0; i > >

        Output: Iteration number:0
        Iteration number:2
        Iteration number:3

        Control Structure of Java

        Leave a Comment Cancel reply

        You must be logged in to post a comment.

Оцените статью