While loop in java
It is an iteration looping control statement which has five parts such as.
- counter part
- condition part
- while body
- increment/decrement part
- end of program
The condition part is the main part of the while loop. It allows certain condition and that condition will enclosed within a pair of parenthesis.
In this case the control will first check the the condition of the while loop if the condition is true then it execute the body part of the while loop in a cycle manner up to the condition being satisfied if the the condition is unsatisfied then the control directly jump to the end of the loop.
syntax for while loop
ctr value;//counter part
while(condition)//condition part
{
statement....1;//while body
----------------
----------------
statement....n;
increment/decrement;//increment/decrement part
}//end of program
Q.Write a program then print numbers from 1 to 10;
class Sibani
{
public static void main(String args[])
{
int no=1;
while(no<=10)
{
System.out.println(+no);
no++;
}
}
}
Q.Write a program then print even numbers from 100 to 1(using command line arguments);
class Sibani
{
public static void main(String args[])
{
int no;
no=Integr.parseInt(args[0]);
while(no>=1)
{
if(no%2==0)
System.out.println(+no);
no--;
}
}
}
Q.Write a program then print all the factors of that number;
class Sibani
{
public static void main(String args[])
{
int no,f=1;
no=Integer.parseInt(args[0]);
while(f<=no)
{
if(no%f==0)
System.out.println(+f);
f++;
}
}
}
No comments:
Post a Comment