+ 1
Can anyone xplain me while loop with giving simple n appropriate example???
how can i create table with while loop??? Helga Arun Tomar Fata1 Err0r HAWKEYE Gaurav Agrawal
5 ответов
+ 10
While and for loops keep on doing some task until they encounter a false condition.
This is a nested loop example.
Let's say you want to print the tables, we start with a variable 'num' initialized to 1 carrying the number for which you need the table and another variable 'i' to update the multiplier value (usually 1 to 10).
int num = 1;
while (num <= 10) {
int i = 1;
while (i <= 10) {
int result = num * i;
System.out.println(num + " * " + i + " = " + result);
i++;
}
System.out.println();
num++;
}
How the above code works:
The inner (2nd) while loop will start to execute initially, taking num as 1 every times it executes. When i becomes greater than 10 (or 11), it now moves on the outer 1st loop which runs and now makes num equal to 2. Then again the inner while loop executes, this time taking num as 2 on every run and i, as obvious, keeps on increasing until its 10. The same process continues until num is greater than 10, printing tables from number 1 to 10 ;)
+ 8
Harsh Agrawal Even or Odd could be checked without using any type of loop. Either by using an if and else statement, or ternary operators, and that's enough. You've made your program!
+ 2
class Check
{
public static void main(String args[])
{
int x=2;
int i=1;
while(i<6)
{
system.out.println(x*i);
i+=1;
}
}
}
0
Dev how to print even no or odd no with while loop
0
no Dev if i want to display even or odd no by using while loop like
0
2
4.....
or odd
1
3
5.....