Skip to content

Commit 97efc34

Browse files
committed
feat: Demonstrate for, while, and do-while loops with user input and output
🧠 Logic & Purpose: - Added examples for printing numbers and messages using: - `for` loop (e.g., 1 to 5 with custom step) - `while` loop to print numbers from 1 to 5 - `do-while` loop to ensure the body runs at least once - Included user input using `Scanner` to repeat "Hello World" n times - Preserved original comments and code for learning reference - Structured the output and comments to make loop behavior easily understandable 📁 Files: - `Loop.java`: Includes commented learning examples, restored basic while/do-while logic - `Loop2.java`: Active implementation with user input, step-wise for-loop, and all loop variants. Signed-off-by: Somesh diwan <[email protected]>
1 parent 2c0a6f2 commit 97efc34

File tree

3 files changed

+115
-60
lines changed

3 files changed

+115
-60
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import java.util.Scanner;
2+
3+
public class Loop {
4+
public static void main(String[] args) {
5+
6+
/*
7+
Syntax of for loop:
8+
for (initialisation; condition; increment/decrement) {
9+
// loop body
10+
}
11+
*/
12+
13+
// Q: Print numbers from 1 to 5 with step size of 2
14+
// for (int num = 1; num <= 5; num += 2) {
15+
// System.out.println(num); // prints: 1, 3, 5
16+
// }
17+
18+
// Print "Hello World" n times using for loop
19+
Scanner in = new Scanner(System.in);
20+
// int n = in.nextInt(); // Input n from user
21+
22+
// for (int num = 1; num <= n; num++) {
23+
//// System.out.print(num + " "); // Uncomment to print numbers from 1 to n
24+
// System.out.println("Hello World"); // Prints "Hello World" n times
25+
// }
26+
27+
// Syntax of while loop:
28+
/*
29+
while (condition) {
30+
// loop body
31+
}
32+
*/
33+
34+
// Example of while loop printing numbers from 1 to 5
35+
// for (int num = 1; num <= 5; num += 2) {
36+
// System.out.println(num); // Prints: 1, 3, 5
37+
// }
38+
39+
int num = 1;
40+
while (num <= 5) {
41+
System.out.println(num); // Uncommented to actually print numbers from 1 to 5
42+
num += 1;
43+
}
44+
45+
// Syntax of do-while loop:
46+
/*
47+
do {
48+
// loop body
49+
} while (condition);
50+
*/
51+
52+
int n = 1;
53+
do {
54+
System.out.println("Hello World"); // This will execute at least once even if condition is false
55+
} while (n != 1); // Since n == 1, this runs only once
56+
57+
}
58+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.util.Scanner;
2+
3+
public class Loop2 {
4+
public static void main(String[] args) {
5+
6+
/*
7+
Syntax of for loops:
8+
9+
for (initialisation; condition; increment/decrement) {
10+
// body
11+
}
12+
*/
13+
14+
// Q: Print numbers from 1 to 5
15+
for (int num = 1; num <= 5; num += 2) {
16+
System.out.println(num); // prints 1, 3, 5 (incremented by 2)
17+
}
18+
19+
// print numbers from 1 to n
20+
Scanner in = new Scanner(System.in);
21+
int n = in.nextInt(); // take input from user
22+
23+
for (int num = 1; num <= n; num++) {
24+
// System.out.print(num + " "); // prints numbers from 1 to n on same line
25+
System.out.println("Hello World"); // prints "Hello World" n times
26+
}
27+
28+
// while loops
29+
/*
30+
Syntax:
31+
while (condition) {
32+
// body
33+
}
34+
*/
35+
36+
// Same logic as first for-loop: print numbers from 1 to 5 using while loop
37+
int num = 1;
38+
while (num <= 5) {
39+
System.out.println(num); // prints 1 2 3 4 5
40+
num += 1;
41+
}
42+
43+
// do while
44+
/*
45+
Syntax:
46+
do {
47+
// body
48+
} while (condition);
49+
*/
50+
51+
int m = 1;
52+
do {
53+
System.out.println("Hello World"); // This will run at least once
54+
} while (m != 1); // Since condition is false, loop runs only once
55+
56+
}
57+
}

Section7ConditionalStatements/Practice/src/Loops.java

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)