Control flow statement:
==> Control flow statements are fundamental components of programming languages that allow developers to control the order in which instructions are executed in a program.
==> They enable execution of a block of code multiple times, execute a block of code based on conditions, terminate or skip the execution of certain lines of code, etc.
Types of Control Flow statements:
Looping Statements:
==> Looping statements, also known as iteration or repetition statements, are used in programming to repeatedly execute a block of code.
==> They are essential for performing tasks such as iterating over elements in a list, reading data from a file, or executing a set of instructions a specific number of times. Here are some common types of looping statements:
1.For loop
2.While loop
3.Do while loop
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.
Syntax:
while (condition) {
// Code to be executed
}
==> The condition is evaluated before each iteration.
==> If the condition is true, the loop executes the block of
code.
==> If the condition is false, the loop stops.
Example:Printing number from 1 to 10:
package javaprogram;
public class Whileloop {
public static void main(String []args)
{
int num=1;
while(num<10)
{
System.out.println(num);
num=num+1;
}
}
}
Output:
1
2
3
4
5
6
7
8
9
Task1:
Example Program:
package javaprogram;
public class task {
public static void main(String[] args) {
// TODO Auto-generated method stub
int count=5;
while(count<=20)
{
count=count+3;
System.out.println(" count:"+count);
}
}
}
Output:
count:8
count:11
count:14
count:17
count:20
count:23
Task2:
Example program:
package javaprogram;
public class welcome {
public static void main(String[] args) {
int count=1;
while(count<=10) {
System.out.println("welcome to java");
count=count+1;
}
}
}
Output:
welcome to java
welcome to java
welcome to java
welcome to java
welcome to java
welcome to java
welcome to java
welcome to java
welcome to java
welcome to java
Refernce:
https://www.w3schools.com/java/java_while_loop.asp
https://www.geeksforgeeks.org/java-while-loop-with-examples/
https://www.geeksforgeeks.org/control-flow-statements-in-programming/
https://www.codingem.com/flowchart-loop/
Top comments (0)