DEV Community

Kaviya.k
Kaviya.k

Posted on

Do and while loop:

while loop

Java while loop is a control flow statement used to execute the block of statements repeatedly until the given condition evaluates to false. Once the condition becomes false, the line immediately after the loop in the program is executed.

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

Syntax of while loop in Java:

while (test_expression) {   

 // statements        

update_expression; 


}
Enter fullscreen mode Exit fullscreen mode

Execution of While Loop in Java:

Image description

1.Control enters the while loop.
2.The condition is tested.
3.If true, execute the body of the loop.
4.If false, exit the loop.
5.After executing the body, update the loop variable.
6.Repeat from step-2 until the condition is false.

simple example:

public class Main {
  public static void main(String[] args) {
    int i = 0;
    while (i < 5) {
      System.out.println(i);
      i++;
    }  
  }
}

output:
0
1
2
3
4 

Enter fullscreen mode Exit fullscreen mode

The Do-While Loop:

This do-while loop is an exit controlled loop that checks the condition at the end of the code. This loop ensures that the code is executed at least once.

Image description

**simple example:**

package B10;

public class loop {

    public static void main(String[] args) {
        int no=1;
        do
        {
            System.out.println(no);
            no=no+1;
        }
        while(no<=5);
    }

}
output:
1
2
3
4
5



Enter fullscreen mode Exit fullscreen mode

Differnce between while and do while loop:

Image description

Image description

Entry Control Loops

These control the entry to the loop. They check for a condition and the control is transferred inside the loop if, and only if, the condition is true. These loops are used when checking for a condition before execution is mandatory. Example include for and while loops.

Exit Control Loops

An exit control loop controls the exit. The exit control loop checks the condition for an exit. If the given condition for exit is true, control will exit from the loop body, or else control will enter again into the loop. Example is a do-while loop.

Increment and Decrement Operators:

Increment and Decrement Operators are Unary Operators commonly used in programming to increase or decrease the value of a variable by one, respectively.

Increment Operators:

Increment operators are used to increase the value of a variable by one. There are two types of increment operators:
The prefix increment operator (++x) and
The postfix increment operator (x++)

Prefix Increment Operator (++x):

The prefix increment operator increases the value of the variable by 1 before the value is used in the expression.
Syntax: ++x
Example: If x is initially 5, ++x will increment x to 6 and return the new value (6).

Postfix Increment Operator (x++):

The postfix increment operator increases the value of the variable by 1 after the value is used in the expression.
Syntax: x++
Example: If x is initially 5, x++ will return the current value of x (5) and then increment x to 6.

**Example program:**(Increment)

public class Main {
    public static void main(String[] args)
    {
        int x = 5;
        // Prefix increment: increment x by 1 and then print
        // the value (6)
        System.out.println(++x);
        // Postfix increment: print the value of x (6) and
        // then increment x by 1
        System.out.println(x++);
        // Output: 7
        System.out.println(x);
    }
}
output:
6
6
7
Enter fullscreen mode Exit fullscreen mode

Decrement Operators:

Decrement operators are used in programming languages to decrease the value of a variable by one. Similar to increment operators, there are two types of decrement operators:
The prefix decrement operator (--x)
The postfix decrement operator(x--)

Prefix Decrement Operator (--x):
The prefix decrement operator decreases the value of the variable by 1 before the value is used in the expression.
Syntax: --x
Example: If x is initially 5, --x will decrement x to 4 and return the new value (4).

Postfix Decrement Operator (x--):

The postfix decrement operator decreases the value of the variable by 1 after the value is used in the expression.
Syntax: x--
Example: If x is initially 5, x-- will return the current value of x (5) and then decrement x to 4.

Example program:(Decrement)

public class Main {
    public static void main(String[] args)
    {
        int x = 5;
        // Prefix decrement: decrement x by 1 and then print
        // the value (4)
        System.out.println(--x);
        // Postfix decrement: print the value of x (4) and
        // then decrement x by 1
        System.out.println(x--);
        // Output: 3
        System.out.println(x);
    }
}
output:
4
4
3

Enter fullscreen mode Exit fullscreen mode

References:
https://www.simplilearn.com/tutorials/javascript-tutorial/javascript-loops
https://www.geeksforgeeks.org/java-while-loop-with-examples/
https://www.w3schools.com/java/tryjava.asp?filename=demo_while_loop
https://studyopedia.com/java/loops-in-java/

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

đź‘Ą Ideal for solo developers, teams, and cross-company projects

Learn more