DEV Community

AmalaReegan
AmalaReegan

Posted on • Edited on

Day18; Control flow statement

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:

Image description
Image description

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
}

Image description

==> 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;
    }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

1
2
3
4
5
6
7
8
9
Enter fullscreen mode Exit fullscreen mode

Task1:

Image description

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);
    }


}
Enter fullscreen mode Exit fullscreen mode

}

Output:

count:8
count:11
count:14
count:17
count:20
count:23

Task2:

Image description

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/

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs