DMCA.com Protection Status Java Programming Lesson 5 | Loops powerful features in Java - ZAREEN TECH WAVE -Largest Tutorials Website For Freelancer ZAREEN TECH WAVE -Largest Tutorials Website For Freelancer

Header Ads

Java Programming Lesson 5 | Loops powerful features in Java

LESSON-5: LOOP 

🔥 Let's jump into Lesson-5: Loops — one of the most powerful features in Java.




📘 Lesson 5 — Loops (for, while, do-while)

Loops help us repeat code multiple times without rewriting it.


✅ 1️⃣ for Loop

We know how many times repetition is needed.

Structure:

for(initialization; condition; increment/decrement){ // code }

Example:

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

✅ Output:

Number: 1 Number: 2 Number: 3 Number: 4 Number: 5

✅ 2️⃣ while Loop

We don't always know the number of repetition beforehand — loop continues while condition is true.

Structure:

while(condition){ // code }

Example:

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

✅ 3️⃣ do-while Loop

Executes at least once, even if condition is false.

Structure:

do{ // code } while(condition);

Example:

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

✅ Extra: break & continue

KeywordPurpose
break             Stops loop immediately
continue             Skips current iteration

Example:

for(int i=1; i<=5; i++){ if(i == 3){ continue; // Skip 3 } System.out.println(i); }

Output:

1 2 4 5

✅ Full Practical Example

public class LoopExample { public static void main(String[] args) { // For Loop System.out.println("For Loop:"); for(int i = 1; i <= 5; i++){ System.out.println(i); } // While Loop System.out.println("\nWhile Loop:"); int j = 1; while(j <= 5){ System.out.println(j); j++; } // Do-While Loop System.out.println("\nDo-While Loop:"); int k = 1; do{ System.out.println(k); k++; }while(k <= 5); } }

📝 Exercise for You ✅

Write programs using:

1️⃣ for loop → print numbers 1 to 10
2️⃣ while loop → print even numbers 2 to 20
3️⃣ do-while loop → print numbers decreasing from 5 to 1

Reply with your:

✅ Code
✅ Output

I will check and give feedback ✅🔥


🎯 Quick Quiz

1️⃣ Which loop must run at least once?
A) for
B) while
C) do-while

2️⃣ Which keyword exits loop immediately?
A) break
B) stop
C) quit

Reply format:
1 → ?, 2 → ?


✅ Lesson-5 Completed!

Ready for next lessons?

Lesson-6: Arrays (1D & 2D)
or
Lesson-6: Strings (String class & methods)

Which one should we learn first? 😃


▶ NEXT PART - Lesson-5: Arrays (1D & 2D)

No comments

Thank You For Visit My Website.
I Will Contact As Soon As Possible.

Theme images by 5ugarless. Powered by Blogger.