In: Computer Science
explain what a while loop is and how you can use it. Give examples based on websites on how you can use a while loop to your advantage. This assignment should be a couple paragraphs long (5-8 sentences per paragraph).
while is an iteration statement ,that represents a statement or block of statements untill some condition is true.
General form
while(condition)
{
// body of the loop
}
Where condition may be any boolean expression.The body of loop will be executed as long as the condition is true.Once the condition becomes false, the control passes to the statement immediately after the while loop.
Example
1). Class Test
{
Public staic void main(string args[ ] )
{
int n=0;
while(n<10)
{
System.out.println("N:" +n);
n++;
}
}
}
.....................................................................................
2). Class ReverseLong
{
Public staic void main(string args[ ] )
{
Long 1=7562,reverse =0,temp,digits;
temp=1;
while(temp!=0)
{
digits=temp%10;
temp=temp/10;
reverse=reverse*10+digits;
}
System.out.println("The original number is:" +1);
System.out.println("Reverse of the original number is:" +reverse);
}
}