Java For Loop

For loop:

  • For loop in java is used to iterate a block of code multiple times.
  • Use for loop only when the exact number of iterations needed is already known to you. Syntax :/* for (initialize; check_bool_expression; update){ //code; } */Copy
  • Initializer: Initializes the value of a variable. This part is executed only once.
  • check_bool_expression: The code inside the for loop is executed only when this condition returns true.
  • update: Updates the value of the initial variable.Example :for (i=7; i!=0; i--){ System.out.println(i); }CopyThe above for loop initializes the value of i=7 and keeps printing as well as decrementing the value of i till i do not get equals to 0.

Flow control of for loop : 

Java_For_Loop

Exercise 1: Write a program to print first n odd numbers using a for loop.

Exercise 2: Write a program to print first n natural numbers in reverse order.

Example

package com.company;

public class cwh_23_for_loop {
    public static void main(String[] args) {
//        for (int i=1; i<=10; i++){
//            System.out.println(i);
//        }
        // 2i = Even Numbers = 0, 2, 4, 6, 8
        // 2i+1 = Odd Numbers = 1, 3, 5, 7, 9
        //int n = 3;
        //for (int i =0; i<n; i++){
        //    System.out.println(2*i+1);
        //}

        for(int i=5; i!=0; i--){
            System.out.println(i);
        }
    }
}
Was this article helpful?
YesNo

Leave a Comment

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