Skip to content

Commit ecfef82

Browse files
committed
feat: Demonstrate control flow variations using while, do-while, infinite loop, and if-else
🧠 Logic: - Example 1 (commented): `while` loop that never executes since initial condition `i < 100` is false. - Example 2 (commented): `do-while` loop that executes once even though `i < 100` is false initially. - Example 3 (commented): `while(true)` loop using `byte` type which overflows after 127, causing infinite printing. - Example 4 (active): `if(true)` block runs unconditionally, prints current `i`, increments it, and skips the `else`. Signed-off-by: Somesh diwan <[email protected]>
1 parent 413acc9 commit ecfef82

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed
Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
11
public class DoWhileLoop1 {
22
public static void main(String[] args) {
3-
/*int i = 100;*/
4-
/*while(i < 100)
3+
// Example 1: A 'while' loop that never executes because the condition is false at the start
4+
/*int i = 100;
5+
while(i < 100)
56
{
6-
System.out.println(i);
7-
i=i*2;
7+
System.out.println(i); // This line will never be reached
8+
i = i * 2;
89
}*/
9-
/* do
10+
11+
// Example 2: A 'do-while' loop that executes at least once even if the condition is false
12+
/*int i = 100;
13+
do
1014
{
11-
System.out.println(i);
12-
i=i*2;
15+
System.out.println(i); // This will print once even though i is not < 100
16+
i = i * 2;
1317
}
14-
while(i<100);*/
18+
while(i < 100);*/
1519

16-
byte i=1;
20+
// Example 3: Infinite loop using while(true) and byte data type overflow
21+
byte i = 1; // byte range is -128 to 127
1722
/*while(true)
1823
{
19-
System.out.println(i);
24+
System.out.println(i); // Will eventually overflow after reaching 127 to -128
2025
i++;
2126
}*/
2227

28+
// Example 4: Simple 'if-else' structure where condition is always true
2329
if(true)
2430
{
25-
System.out.println(i);
26-
i++;
31+
System.out.println(i); // Prints 1
32+
i++; // i becomes 2 (not printed)
2733
}
2834
else
29-
System.out.println("Bye");
35+
System.out.println("Bye"); // This will never execute due to 'if(true)'
3036
}
3137
}

0 commit comments

Comments
 (0)