The do-while loop always executes its body at least once, because its conditional expression is at the bottom of the loop.The do-while loop first executes the body of the loop and then evaluates the conditional expression. If this expression is true, the loop will repeat. Otherwise, the loop terminates.
Syntax:
do
{
// body of loop;
// Increment (or) Decrement;
} while(Condition) ;
This Java program prompts the user to input a number and then prints out all the even integers from 2 up to that number using a do-while loop. Here is a breakdown of the code:
This program is a simple example of how to use a do-while loop in Java to iterate over a range of numbers. In this case, it is used to print out all even numbers between 2 and the user-specified limit. The do-while loop is used here because we want to ensure that at least one iteration of the loop is executed even if the condition is false, so that we can output the first even number (which is 2).
import java.util.Scanner; public class do_while { public static void main(String args[]) { System.out.println("Enter The Limit : "); Scanner in =new Scanner(System.in); int n=in.nextInt(); int i=2; do { System.out.println(i); i=i+2; }while (i<=n); } }
Enter The Limit : 20 2 4 6 8 10 12 14 16 18 20To download raw file Click Here
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions