π 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