❮ Previous Next ❯

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 condition is specified.
This loop is useful where programmer doesn't know in advance that 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 ❮ Previous Next ❯



  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved