FmD4FRX3FmXvDZXvGZT3FRFgNBP1w326w3z1NBMhNV5=

Video

items

Java Do While Loop with Simple Example - Simple Way to Learn

Java Do While Loop with Simple Example - Simple Way to Learn

Friends, I wanted to teach pattern programs from Java Loops. But most people don't know much about loops. They just remember the loops and after a while forget the steps of the loops in them. You should read this post in its entirety, of course, do-while loops will solve 100% of the problems, and you will become an expert.

Even if you don't know about this do-while loop, you can get complete information by reading the post. Here you will learn how to do Dry-Run.

Java Loop Overview

The concept of loops is very old in programming. If you have learned c and c ++, you have seen different loops in them too. The only difference is that, with the slightest change in the concept of loops, new loops came in the latest programming languages.

In programming, the loops concept is included in the control statement. This includes more if-else statements, break statements, continue statements. The goto statement has been removed from java. Control statements are used to change the flow of a program or to achieve something.

What is a loop in java?

Looping in programming languages is a feature that facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true.

Types of loops in java?

In Java, there are four kinds of loops which are – the for loop, the while loop,  the do-while loop, and the for-each loop. All these four-loop constructs of Java execute a set of repeated statements as long as a specified condition remains true. This particular condition is generally known as loop control.

In this post, you will only see the do-while loop with a simple example. The next post will contain information about the remaining 3 loops with examples.

4 Easy Ways to Become an Expert in Java Loops?

  1. To know the syntax of loops.
  2. Understanding the flow of the loop.
  3. To do Dry Run.
  4. To practice.
I am going to teach these four options below. Read everything carefully and practice. Comment if you have any problems.

Java Do While Loop Overview

This loop is used when there is a need to repeatedly execute a group of statements as long as a condition is true. If the condition is false, the repetition will be stopped and the flow of execution comes out of the do-while loop. 

Syntax of a do-while loop in java

do{
    statements;
}while(condition);

What are the 4 steps in the  Java do-while loop?

Loops have 4 steps, the loops are made up of steps. The structure of the steps in each loop is different. Next, we will see the design of the do-while loop with an example.

1. Variable Initialization:
From where you want to start your program. Take the datatype and initialize that value.

2. Statements:
When your loop starts, all the operations you want to do have to be written in the statement block. We will see that in the next example.

3. Increment / Decrement:
The value given to the starting point is taken from the end to the end by the Increment and Decrement operators. Suppose starting number is given as 1 and it has to be increased to 10, then it can be done with the help of the increment operator.

4. Condition:
You will need to set boundaries for where your program will end. These boundaries are written with the help of the relational operator in JAVA.

public class DoWhileDemo{ 
    
    public static void main(String[] args) {    
    
        int i=1;                                   //Step 1 - Initialization
        do{    
            System.out.println(i);      //Step 2 - Statements
            i++;                                 //Step 3 - Increment - Decrement    
        }while(i<=5);                    //Step 4 - Condtion
    }    

 

Java Do While Loop with Simple Example

I am now going to write a program using a do-while loop, with the help of which I am going to print the name of my website 5 times. If you want these examples, there is a download button below, download from there.

public class DoWhileDemo{ 
    
    public static void main(String[] args) {    
    
        int i=1;                                   //Step 1 - Initialization
        do{    
            System.out.println("www.technologyandfun.com");      //Step 2 - Statements
            i++;                                 //Step 3 - Increment - Decrement    
        }while(i<=5);                    //Step 4 - Condtion
    }    



 Java Do While Loop with Simple Example - Simple Way to Learn

Java Do While Loop Program Dry Run

If the program in the loop does Dry Run, then the concept of your loop will become more solid. Dry running is considered to be a good habit of programmers.

First I will write the steps and show how the number changed. Then I will add a photo from which you will know how to dry run on the book.

Step 1:
Already the value of i is 1, so it is displayed website name the first time. Then i++ will increment the value of i by 1, hence the value of i becomes 2.

Step 2:
Then the condition i<=5 is tested. As the condition is true, then the flow of execution will go back. and the of i, i.e. 2  so it is displayed website name the second time. Then the i value is incremented and becomes 3.

Step 3:
Then the condition i<=5 is tested. As the condition is true, then the flow of execution will go back. and the of i, i.e. 3  so it is displayed website name the third time. Then the i value is incremented and becomes 4.

Step 4:
Then the condition i<=5 is tested. As the condition is true, then the flow of execution will go back. and the of i, i.e. 4  so it is displayed website name the fourth time. Then the i value is incremented and becomes 5.

Step 5:
Then the condition i<=5 is tested. As the condition is true, then the flow of execution will go back. and the of i, i.e. 5  so it is displayed website name the fifth time. Then the i value is incremented and becomes 6.

Step 6:
Then the condition i<=5 is tested. As the condition is false, then the flow of execution will break it. 

Program Dry Run:

NOTE - 
  • Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop executes once before the condition is tested. If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop executes again.
  • If you pass true in the do-while loop, it will be an infinitive do-while loop.

Java Do While Loop Examples for Practice

Now your Java Do While Loop problem will be solved. If there is still any problem, read the post carefully.

Now you want to practice Do While Loop. Here are some Do While Loop examples, you want to solve. Many of these examples also ask the interviewer. Download the following file for its solution.

  1. Write a program to print numbers from 1 to 10.
  2. Write a program to calculate the sum of the first 10 natural numbers.
  3. Write a program to print numbers from 10 to 1.
  4. Write a program to find the given no is even or odd.
  5. Write a program to print the table of 5.



1/Post a Comment/Comments

73745675015091643