while Loop

A while loop is a control structure that allows you to repeat a task a certain number of times, I mean the while loop executes till the condition is specified.
This loop is useful where the programmer doesn't know in advance how many times the loop will be executed.

//Syntax
The syntax of a
while loop is:
    while (boolean_expression) {
        //Statements
    }

When executing, if the boolean_expression result is true, then the actions inside the loop will be executed. This will continue as long as the expression result is true.
Here, key point of the while loop is that the loop might not ever run. When the expression is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.

Example

public class Test {
    public static void main(String args[]) {
        int x = 10;
        while (x < 20) {
            System.out.print("value of x : " + x);
            x++;
            System.out.print("\n");
        }
    }
}

//Output value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19

//write a program to check whether the given number is palindrome or not.

import java.util.Scanner;
class Palindrome {
    public static void main(String args[]) {
        int n, i, num, rev = 0;
        Scanner s = new Scanner(System.in);
        System.out.println("enter the number");
        n = s.nextInt();
        i = n;
        while (n != 0) {
            num = n % 10;
            rev = rev * 10 + num;
            n = n / 10;
        }
        if (rev == i)
            System.out.println("the number is palindrome");
        else
            System.out.println("the number is not palindrome");
    }
}

Output
1st run
enter the number
123
the number is not palindrome
2nd run
enter the number
121
the number is palindrome

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology, BBSR
Call to: 9439202111  |  9658317757  |  0674-2361252

Comment