Is lesson me hum seekhenge:
- Loops kya hote hain
- Java me loops ke types
- Nested loops
- Pattern printing
- Break statement
- Continue statement
Loop ek control structure hai jo kisi code ko multiple times execute karne ke liye use hota hai.
Example:
Agar hume 1 se 100 tak number print karna ho
Without loop:
System.out.println(1);
System.out.println(2);
System.out.println(3);
...
Loop use karne se code short aur efficient ho jata hai.
Java me 3 types ke loops hote hain:
Loops
│
├── for loop
├── while loop
└── do-while loop
for loop tab use hota hai jab iterations ka exact number pata ho.
for(initialization; condition; update){
// code
}public class ForLoopExample {
public static void main(String[] args) {
for(int i = 1; i <= 5; i++){
System.out.println(i);
}
}
}Output:
1
2
3
4
5
while loop tab use hota hai jab iterations ka number fix na ho.
while(condition){
// code
}public class WhileLoopExample {
public static void main(String[] args) {
int i = 1;
while(i <= 5){
System.out.println(i);
i++;
}
}
}Is loop me code kam se kam ek baar execute hota hi hota hai.
do{
// code
}
while(condition);public class DoWhileExample {
public static void main(String[] args) {
int i = 1;
do{
System.out.println(i);
i++;
}
while(i <= 5);
}
}Jab ek loop ke andar dusra loop hota hai use Nested Loop kehte hain.
Example structure:
Outer Loop
└── Inner Loop
for(){
for(){
// code
}
}public class NestedLoopExample {
public static void main(String[] args) {
for(int i = 1; i <= 3; i++){
for(int j = 1; j <= 3; j++){
System.out.print("* ");
}
System.out.println();
}
}
}Output:
* * *
* * *
* * *
Pattern printing me nested loops use hote hain.
public class SquarePattern {
public static void main(String[] args) {
for(int i = 1; i <= 4; i++){
for(int j = 1; j <= 4; j++){
System.out.print("* ");
}
System.out.println();
}
}
}Output:
* * * *
* * * *
* * * *
* * * *
public class TrianglePattern {
public static void main(String[] args) {
for(int i = 1; i <= 5; i++){
for(int j = 1; j <= i; j++){
System.out.print("* ");
}
System.out.println();
}
}
}Output:
*
* *
* * *
* * * *
* * * * *
break statement ka use loop ko immediately terminate karne ke liye hota hai.
public class BreakExample {
public static void main(String[] args) {
for(int i = 1; i <= 10; i++){
if(i == 5){
break;
}
System.out.println(i);
}
}
}Output:
1
2
3
4
Explanation:
Jab i = 5 hua
loop break ho gaya
continue statement current iteration ko skip karta hai aur next iteration start karta hai.
public class ContinueExample {
public static void main(String[] args) {
for(int i = 1; i <= 5; i++){
if(i == 3){
continue;
}
System.out.println(i);
}
}
}Output:
1
2
4
5
Explanation:
i = 3 skip ho gaya
| Statement | Work |
|---|---|
| break | loop ko completely stop karta hai |
| continue | current iteration skip karta hai |
- Loop repetitive tasks ke liye use hota hai
- Nested loops pattern printing me use hote hain
breakloop terminate karta haicontinueiteration skip karta hai- Pattern printing programming practice ke liye important hai
1️⃣ Loop kya hota hai?
2️⃣ Java me kitne types ke loops hote hain?
3️⃣ Nested loop kya hota hai?
4️⃣ Break aur Continue me kya difference hai?
5️⃣ Pattern printing me nested loop kyu use hota hai?
Is lesson me humne seekha:
✔ Loops
✔ Nested loops
✔ Pattern printing
✔ Break statement
✔ Continue statement
Ye concepts DSA aur logic building ke liye bahut important hain.
Next lesson me hum seekhenge:
- Arrays
- Array traversal
- Multi dimensional arrays