Control flow Statement:
-->Java compiler executes the code from top to bottom. The statements in the code are executed according to the order in which they appear.
-->However, Java provides statements that can be used to control the flow of Java code. Such statements are called control flow statements.
--> It is one of the fundamental features of Java, which provides a smooth flow of program.
Loop statement
-->In programming, sometimes we need to execute the block of code repeatedly while some condition evaluates to true.
-->However, loop statements are used to execute the set of instructions in a repeated order.
-->The execution of the set of instructions depends upon a particular condition.
1.do while loop
2.while loop
3.for loop
While loop:
-->The while loop is also used to iterate over the number of statements multiple times. However, if we don't know the number of iterations in advance, it is recommended to use a while loop.
-->Unlike for loop, the initialization and increment/decrement doesn't take place inside the loop statement in while loop.
-->Java while loop is a control flow statement used to execute the block of statements repeatedly until the given condition evaluates to false.
-->Once the condition becomes false, the line immediately after the loop in the program is executed.
Note: If we do not provide the curly braces ‘{‘ and ‘}’ after while( condition ), then by default while statement will consider the immediate one statement to be inside its block.
Syntax:
while (test_expression) {
// statements
update_expression;
}
Flowchart:
Example program:
package day1if;
public class WhileLoop {
public static void main(String[] args)
{
int c = 1;
while (c <= 5)
{
System.out.println(c);
c++;
}
}
}
Output:
1
2
3
4
5
2.
package day1if;
class whileLoop {
public static void main(String args[])
{
int i = 1;
while (i < 6) {
System.out.println("Welcome to java");
i++;
}
}
}
Output:
Welcome to java
Welcome to java
Welcome to java
Welcome to java
Welcome to java
Task1:
program:
public class Count
{
public static void main (String args[])
{
int count = 1;
while(count<=5){
count=count+1;
System.out.println("count: "+ count);
}}}
Output:
count: 2
count: 3
count: 4
count: 5
count: 6
Task 2:
Program:
public class Count1
{
public static void main (String args[])
{
int count = 5;
while(count>=1)
{
count=count-1;
System.out.println("count: "+ count);
}
}}
Output:
count: 4
count: 3
count: 2
count: 1
count: 0
Task 3:
program:
public class Count2
{
public static void main (String args[])
{
int count = 1;
while(count<=10){
count=count+2;
System.out.println("count: "+ count);
}}}
Output:
count: 3
count: 5
count: 7
count: 9
count: 11
Task 4:
Program:
public class Count3
{
public static void main (String args[])
{
int count = 0;
while(count<10)
{
count=count+2;
System.out.println("count: "+ count);
}
}}
Output:
count: 2
count: 4
count: 6
count: 8
count: 10
Refernce:
https://www.w3schools.com/java/java_while_loop.asp
https://www.codingem.com/flowchart-loop/
https://www.geeksforgeeks.org/java-while-loop-with-examples/
https://www.geeksforgeeks.org/control-flow-statements-in-programming/
Top comments (0)