Looping in java (class 8)

 A for loop is a control structure that allows you to repeat a block of code a specific number of times.

In Java, the syntax is:

for(initialization; condition; update) { // code to be executed }

🧩 Explanation of each part

  1. Initialization → executed once before the loop starts.
    (Usually used to declare and set a loop variable.)

  2. Condition → checked before each iteration. If it’s true, the loop runs.

  3. Update → executed after every iteration (to change the loop variable).


⚙️ Real-time Example

Imagine you are a teacher and want to display the names of 5 students in order.
Instead of writing 5 print statements, you can use a for loop.


💻 Example 1 – Simple counting

class ForLoopExample { public static void main(String[] args) { for(int i = 1; i <= 5; i++) { System.out.println("Student number: " + i); } } }

Output:

Student number: 1 Student number: 2 Student number: 3 Student number: 4 Student number: 5

💼 Example 2 – Real-time situation (Printing bills)

Suppose you run a store and you want to print 10 bills in a day.

class BillingSystem { public static void main(String[] args) { for(int bill = 1; bill <= 10; bill++) { System.out.println("Printing Bill #" + bill); } } }

Output:

Printing Bill #1 Printing Bill #2 ... Printing Bill #10

🧮 Example 3 – Calculate sum of first 10 natural numbers

class SumExample { public static void main(String[] args) { int sum = 0; for(int i = 1; i <= 10; i++) { sum = sum + i; } System.out.println("Sum of first 10 numbers = " + sum); } }

Output:

Sum of first 10 numbers = 55

🧱 1. break statement

Definition:
The break statement is used to stop a loop immediately, even if the condition is still true.
It breaks out of the loop and moves to the next part of the program.

Example:

for(int i = 1; i <= 5; i++) { if(i == 3) { break; // loop stops when i = 3 } System.out.println(i); }

Output:

1 2

👉 The loop stopped when i became 3.


🔁 2. continue statement

Definition:
The continue statement is used to skip one loop iteration and move to the next one.
It does not stop the loop, only skips that particular step.

Example:

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

Output:

1 2 4 5

👉 The loop skipped printing 3 but continued with the rest.


🔁 1. while loop

🧠 Definition:

A while loop repeats a block of code as long as the condition is true.
The condition is checked before running the loop.

💻 Example:

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

Output:

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

👉 Here, the loop checks the condition i <= 5 before each repetition.


🔁 2. do-while loop

🧠 Definition:

A do-while loop is similar to a while loop, but it executes the code first and checks the condition later.
That means the loop will run at least once, even if the condition is false.

💻 Example:

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

Output:

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

👉 Even if the condition was false initially, the loop would still run once.


📊 Difference Among for, while, and do-while Loops

Feature / Loop Type for loop while loop do-while loop
When condition is checked Before each loop Before each loop After each loop
Initialization Done inside the loop header Done outside the loop Done outside the loop
Minimum number of executions 0 times (if condition false) 0 times (if condition false) At least 1 time
Best used when Number of repetitions is known Number of repetitions is unknown Code must run at least once
Example for(int i=1;i<=5;i++) while(i<=5) do{ }while(i<=5)




Comments

Popular posts from this blog

class 8 computer chapter 5

CLASS 8 COMPUTER

Class 5 computer chapter 4