What is an example of a while loop?

What is an example of a while loop?

A “While” Loop is used to repeat a specific block of code an unknown number of times, until a condition is met. For example, if we want to ask a user for a number between 1 and 10, we don’t know how many times the user may enter a larger number, so we keep asking “while the number is not between 1 and 10”.

What is while loop in Java with example?

Java while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. Otherwise, we will exit from the while loop. Example: i <= 10. Update Expression: After executing the loop body, this expression increments/decrements the loop variable by some value.

How do you do a while loop in Java?


  1. public class WhileExample {
  2. public static void main(String[] args) {
  3. int i=1;
  4. while(i<=10){
  5. System.out.println(i);
  6. i++;
  7. }
  8. }

Do while loop simple examples?

To understand how to implement a do while loop, let’s implement a simple example to print values from 10-19.

  • #include
  • int main () {
  • int a = 10;
  • do {
  • printf(‘value of a: %d\n’, a);
  • a = a + 1;
  • } while( a < 20 );{
  • return 0;

What is an example of while loop?

A while loop is a control flow structure which repeatedly executes a block of code indefinite no. of times until the given condition becomes false. For example, say, you want to count the occurrence of odd numbers in a range. Some technical references call it a pre-test loop as it checks the condition before every iteration.

What is an example of a loop in programming?

The exact looping process and initial structuring varies between programming languages. In SQL, for example, a programmer may script these structures and execute them either as a server nested loop that is called remotely or as one on a client machine that is executed locally.

What is loop structure in Java?

for loop in java. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to be executed a specific number of times. A for loop is useful when you know how many times a task is to be repeated. Syntax. The initialization step is executed first, and only once.

What is a loop statement in Java?

Looping Statement in Java. Looping statement are the statements execute one or more statement repeatedly several number of times. In java programming language there are three types of loops; while, for and do-while.