Facebook

For Loops

It's easy and quick to print a name one time or two times. But what if we want to print same name 1000 times? What if we want to print a table of a number? What if we want to print a similar pattern again and again? In order to solve these questions and in order to repeat a set of instructions continuously until the condition is met, the role of loops come into play.

Loop is a set of condition repeated again and again until the required condition is met. For loop is a type of loop which is used when we know the exact condition and the number of times we want to execute that loop. Syntax of for loop is given below:

for(variable initialization; condition ; iteration)

{

// code to be executed

}
  • In initialization,variable is initialized i.e variable is assigned some value.Declaration can also be done in this step.
  • If the condition is true,body of the loop is executed otherwise flow of control jumps to next lines of code after the ending of loop.After this,control jumps back to increment statement.
  • The incrementation/ decrementation  is done and condition is checked again.If it is true then control goes to body of loop and executes it.
  • The same process happens again and again until the condition becomes false.
  • When the condition becomes false control exits the for loop.

Example 1

A class has 40 students. Say we want to print even roll numbers of the class.

public class EvenRollNo
{
 public static void main(String[] args)
 {
 for(int rollno = 2; rollno <= 40 ; rollno = rollno + 2)
 {
 System.out.println(rollno);
 }
}


Run

  • EvenRollNo is the class name.
  • rollno is a variable name which is initialized to 2 since the roll numbers start from 1 and we want to print only even numbers.
  • The for loop executes until rollno reaches to 40 since it is a class of 40 students.
  • Increment is done by 2 since we want to print even roll numbers only.

Example 2

Say we want to print multiples of 7 but in descending order and starting from 700.

public class Multiples
{
 public static void main(String[] args)
 {
 System.out.println("The multiples are:");
 for(int mul = 700; mul > 0 ; mul = mul -7)
 {
 System.out.println(mul);
 }
 }
}

Run

Example 3

Say we want to print a pattern of $ like below:

 

$

$$

$$$

$$$$

$$$$$

public class PatternDollar
{
 public static void main(String[] args)
 {
 for(int row = 1; row <=5; row++)
 {
 for(int col = 1; col <= row; col++ )
 {
 System.out.print("$");
 }
 System.out.println();
 }
}
}


Run

For understanding  the flow in nesting of loops, please refer to Nesting of loops article.

Example 4 

Let's print a table of user entered number.

import java.util.Scanner;
public class MathTable
{
public static void main(String[] args)
 {
 Scanner input = new Scanner(System.in);
 System.out.println("Enter a no of your choice");
 int number = input.nextInt();
 int result = 0;
 for(int i=1;i<=10;i++)
 {
 System.out.printf(number +  "*" +   i +  "="  + number * i);
 System.out.println();
 }
}
}


 

Rate this post

Comments

So empty here ... leave a comment!

Leave a Reply

Your email address will not be published. Required fields are marked *

Sidebar