Hi guys. Today we are gonna look at while loop in java. In while loop again we have a counter and a stop condition like for loop. The code shown below explains this.
public class WhileLoopTest {
public static void main(String[] Args)
{
int counter = 0;
while(counter < 5)
{
System.out.println("Value of the counter is " + counter);
counter++;
}
}
}
in this code we have a counter variable again. But the counter is incremented in {} or body of the while loop. every time while loop check the condition inside the (). If the condition is true, it stops the loop and execute the next statement after "}" of the loop. An if condition is false it keeps executing the code inside the {}. Here is the output of the our program.
Value of the counter is 0
Value of the counter is 1
Value of the counter is 2
Value of the counter is 3
Value of the counter is 4
Hope you have enjoyed today's lesson. Please feel free to ask questions and problems. I'll be happy to answer. So see in next class.
No comments:
Post a Comment