Learn with Yasir

Share Your Feedback

Java Loops Explained for Beginners | While and For Loop Tutorial


Learn Java loops step by step. Understand while loops, for loops, loop rules, syntax, and examples with beginner-friendly explanations and clear code samples.

πŸ” Java Loops

Loops are one of the most powerful concepts in programming. They allow a computer to repeat instructions many times without rewriting code.

Think of loops like telling a student:

β€œWrite your name 10 times.”

Instead of repeating the instruction 10 times, you give one instruction + a rule.


🎯 Why Loops Are Important

Without loops:

  • Programs would be very long.
  • Repetitive tasks would require duplicate code.
  • Maintenance would be difficult.

With loops:

  • Code becomes shorter.
  • Tasks become automated.
  • Programs become efficient.

πŸ“Œ The 3 Essential Parts of Every Loop

Every loop must have these three parts:

Step Name Purpose Example
1 Initialization Starting value int i = 0;
2 Condition When to stop i < 5
3 Update Change value i++

⚠ If update is missing β†’ Infinite loop


πŸ”Ή WHILE Loop (Condition-Based Loop)

πŸ‘‰ Use a while loop when you don’t know how many times the loop should run.

Syntax

while(condition){
    // code
}

Example 1 β€” Print Numbers 1–5

int i = 1;

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

Output

1
2
3
4
5

Example 2 β€” Password Attempts

int attempts = 1;

while(attempts <= 3){
    System.out.println("Try password");
    attempts++;
}

Real-life logic:

Keep asking until attempts reach limit.


Example 3 β€” Infinite Loop (Common Mistake)

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

❌ Problem: No i++ β†’ condition never changes.



πŸ”Ή FOR Loop (Counting Loop)

πŸ‘‰ Use a for loop when you know exactly how many times you want to repeat something.

Syntax

for(initialization; condition; update){
    // code
}

Example 1 β€” Print 1–5

for(int i = 1; i <= 5; i++){
    System.out.println(i);
}

Example 2 β€” Print Even Numbers

for(int i = 2; i <= 10; i += 2){
    System.out.println(i);
}

Example 3 β€” Countdown Timer

for(int i = 5; i >= 1; i--){
    System.out.println(i);
}
System.out.println("Go!");


πŸ”„ WHILE vs FOR (When to Use Which?)

Situation Best Loop
Number of repetitions known for
Unknown repetitions while
Condition-based repetition while
Counting numbers for

πŸ‘‰ Rule of thumb for beginners:

If counting β†’ use for If waiting for condition β†’ use while


🧠 Practice Tasks


🟒 Easy Tasks

Task 1 β€” While Loop Print numbers from 10 to 1.


Task 2 β€” For Loop Print your name 5 times.


Task 3 β€” For Loop Print all odd numbers from 1–15.



🟑 Medium Tasks

Task 4 β€” While Loop Calculate sum of numbers 1–10.

Expected Output:

Sum = 55

Task 5 β€” For Loop Print multiplication table of 7.


Task 6 β€” While Loop Print numbers divisible by 3 between 1 and 20.



πŸ”΄ Challenging Tasks

Task 7 β€” Pattern Printing

*
**
***
****
*****

Task 8 β€” Reverse Counting Print numbers from 50 to 0 with step 5.


Task 9 β€” Guessing Game Logic Keep asking user for number until they enter 7.

(Hint: use while loop)