OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 1 : Looping Statements

Loops in Java – Syntax, Explanation & Examples

In Java, loops are used to execute a block of code repeatedly until a given condition is satisfied. They are essential when the number of iterations is known or unknown.

Types of Loops in Java

  1. for loop
  2. while loop
  3. do-while loop
  4. Enhanced for loop (for-each)

1. for Loop

Syntax:
for (initialization; condition; update) {
    // code to be executed
}
Example:
public class ForLoopDemo {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            System.out.println("Count: " + i);
        }
    }
}
Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

2. while Loop

Syntax:
while (condition) {
    // code to be executed
}
Example:
public class WhileLoopDemo {
    public static void main(String[] args) {
        int i = 1;
        while (i <= 5) {
            System.out.println("Number: " + i);
            i++;
        }
    }
}
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

3. do-while Loop

Syntax:
do {
    // code to be executed
} while (condition);
Example:
public class DoWhileDemo {
    public static void main(String[] args) {
        int i = 1;
        do {
            System.out.println("Value: " + i);
            i++;
        } while (i <= 5);
    }
}
Output:
Value: 1
Value: 2
Value: 3
Value: 4
Value: 5

4. Enhanced for Loop (for-each)

Syntax:
for (datatype var : array) {
    // code using var
}
Example:
public class ForEachDemo {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30, 40};

        for (int num : numbers) {
            System.out.println("Element: " + num);
        }
    }
}
Output:
Element: 10
Element: 20
Element: 30
Element: 40

📋 Comparison Table

Loop Type Condition Check Executes at Least Once Use Case
for Before No When number of iterations is known
while Before No Condition-driven loop
do-while After Yes Run at least once
for-each Internal Yes Array or collection traversal

✅ Summary

  • for – Best when count is known
  • while – Best when condition controls iteration
  • do-while – Executes at least once
  • for-each – Simplified loop for arrays/collections
  • Use break to exit loop early
  • Use continue to skip current iteration
=========================================================================

A. for Loop – Assignment Programs

  1. Print numbers from 1 to 10.

  2. Print even numbers between 1 and 100.

  3. Calculate the sum of first 10 natural numbers.

  4. Print multiplication table of a given number.

  5. Calculate the factorial of a number.

  6. Print reverse of a given number.

  7. Check if a number is prime or not.

  8. Find the sum of digits of a number.

  9. Generate Fibonacci series up to n terms.

  10. Count the number of digits in a given number.


B. while Loop – Assignment Programs

  1. Print numbers from 1 to 10 using while loop.

  2. Print digits of a number one by one.

  3. Reverse a number using while loop.

  4. Count number of vowels in a given string.

  5. Calculate the power of a number using loop.

  6. Check whether a number is palindrome.

  7. Find the sum of even and odd digits of a number.

  8. Display digits in reverse order.

  9. Check whether a number is an Armstrong number.

  10. Keep reading user input until 0 is entered.


C. do-while Loop – Assignment Programs

  1. Print menu and perform arithmetic operations until user exits.

  2. Accept numbers and count how many are positive and how many are negative (stop on 0).

  3. Print characters from 'A' to 'Z' using do-while.

  4. Accept password until correct one is entered.

  5. Display the sum of entered numbers until total exceeds 100.

  6. Print even numbers between 1 and 20 using do-while.

  7. Find LCM of two numbers using loops.

  8. Calculate average of numbers entered by user until user enters 'n' to stop.

  9. Simulate ATM-like transaction with do-while repeating options.

  10. Print pattern of stars in decreasing order (e.g., 5 rows to 1 row).


✅ Bonus Tasks

  • Rewrite any 3 programs using all 3 types of loops.

  • Compare execution of while vs. do-while for a false condition.

  • Build a simple console menu using do-while and switch.



Comments

Popular posts from this blog

How to Get a Job in Top IT MNCs (TCS, Infosys, Wipro, Google, etc.) – Step-by-Step Guide for B.Tech Final Year Students

Common HR Interview Questions

How to Get an Internship in a MNC