Go Back

Loops in Java

Loops in Java

It is common in programming to use loops to keep going back and forth over something or a certain task provided a condition is given and the condition evaluate to true. In Java, we have three types of loops that all work the same way. However, there are some differences in syntax and how long it takes to check for conditions.

1.while loop

2.do-while loop

3.for loop

 

Java While Loop

The while loop, loops through a block of code as long as a specified condition is true .

 

The while loop can also be used to iterate over a set of statements repeatedly. If we don't know how many iterations we'll need ahead of time, we should use a while loop.

 

It's also known as the entry-controlled loop because the condition is tested at the beginning of the loop. If the condition is true, the loop body will be executed; otherwise, the statements following the loop will be executed.

The while loop's syntax is seen below.

while (boolean condition)
{
   loop statements...
}

 

while loop in java

 

public class Calculation {    
public static void main(String[] args) {       
int i = 0;    
System.out.println("Printing the list of first 10 even numbers \n");    
while(i<=10) {    
System.out.println(i);    
i = i + 2;    
}    
 }    
}

&nbsp;

output:

Printing the list of first 10 even numbers 

0
2
4
6
8
10

 

Example

As long as the variable (i) is less than 5, the code in the loop will run, over and over again

int i = 0;
while (i < 5) {
  System.out.println(i);
  i++;
}

output:


1
2
3
4

 

 

Quick Quiz  Write a program to print natural number from 100 to 200.

 

Java Do/While Loop

Do while Loops are a subset of While loops, with the only difference being that your code runs at least once before checking the condition, and then if the condition is true, your code begins iterating until the condition is false, as opposed to While loops which check condition first before running.

 

Syntax

do {
  // code block to be executed
}
while (condition);

Example:

Even if the condition is false, the loop will be executed at least once because the code block is executed prior to the condition being tested:

int i = 0;
do {
  System.out.println(i);
  i++;
}
while (i > 5);

output:

0

 

Quick Quiz  Write a program to print first n natural number using do while loop.

 

For loops

The Syntax of a for loop look like this: 

For( initialize; check Boolean expression; update ){ 
//code; 
} 

 

A for loop is usually used to execute a piece of code for specific number of time

    Quick Quiz  Write a program to print first n Odd number using for loop. 

 

Decrementing For loop 

For ( i = 7; , i!=0 , i-- ){ 
System.out.println( i ); 
} 

 

Quick Quiz  Write a program to print first n natural number using for loop in reverse order

output:

O 7, 6, 5, 4, 3, 2, 1 

 

Break and Continue

 

Break Statement The break statement is used exit the loop irrespective of whether the condition is true or false Whenever a “break” is encountered inside the loop, the control is sent outside the loop Example –

For ( int i = 1; i>0 ; i++){
If( i ==2){
System.out.println(“This is break”);
Break;

 

Continue Statement The continue statement is used to immediately move to the next iteration of the loop. The control is taken to the next iteration thus skipping everything below “continue” inside the loop for that iteration.

Example –

For ( int i = 1; i>0 ; i++){
If( i ==2){
System.out.println(“This is break”);
Continue;
  }
System.out.println( i );
System.out.println( “best” );
 }
In While
Int i =0;
While( i<10 ){
i++;
if( i== 2 ){
System.out.println(“This is break/continue”);
Break/continue;
 }
System.out.println( i );
}

 

Practice Set

  1. Write a program to print the following pattern 

**** 

*** 

** 

  1. Write a program to sum first n even number using while loop. 

  2. Write a JAVA program to print multiplication table of a given number n. 

  3. Write a JAVA program to print multiplication table of 10 in reverse order. 

  4. Write a program to calculate the sum of the number occurring in the multiplication table of 8

 

Next → Methods in JAVA

 

All Concepts

1.Variable and Data Types and Strings in JAVA 

2.Operators in JAVA

3.Condition in JAVA 

4.Loop Control in JAVA 

5.Arrays in JAVA (coming soon) 6.Methods in JAVA

7.Introduction to OOPS ( important ),(coming soon)

8.Access Modifier & Constructor (coming soon)

9.Inheritance in JAVA (coming soon)

10.Abstract Classes & Interface (coming soon)

11.Package in JAVA (coming soon)

12.Multithreading in JAVA (coming soon)

13.Error & Exception (coming soon)

14.Advance JAVA – I (coming soon)

15.Advance JAVA – II(coming soon)