๐ Day 24 of My Automation Journey โ Looping Statements (While Loop)
Today I explored Looping Statements in Java, especially the while loop. Loops are very important because they help us repeat a task multiple times without writing duplicate code. ๐น What is a Loop?...

Source: DEV Community
Today I explored Looping Statements in Java, especially the while loop. Loops are very important because they help us repeat a task multiple times without writing duplicate code. ๐น What is a Loop? ๐ A loop is used to execute a block of code repeatedly based on a condition. ๐ Types of Loops in Java โ While Loop โ For Loop โ Do-While Loop ๐ Today focus: While Loop ๐น While Loop Syntax while(condition) { // code } โ Condition is checked first โ If true โ executes โ If false โ stops ๐ Scenario 1: Print same number multiple times โ Question Print 1 five times int i = 1; while(i <= 5) { System.out.print(1 + " "); i++; } ๐ค Output 1 1 1 1 1 โ
Explanation โ Loop runs 5 times โ Always prints 1 ๐ Scenario 2: Print numbers from 1 to 5 โ Question Print numbers from 1 to 5 int i = 1; while(i <= 5) { System.out.print(i + " "); i++; } ๐ค Output 1 2 3 4 5 โ
Explanation โ i increases each time โ Prints updated value ๐ Scenario 3: Print odd numbers โ Question Print odd numbers from 1 to 10