Java While Loop
Repeat while the condition remains true.
முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.
இந்தப் பாடத்தில்…
- Explain the key idea behind While Loop.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
அடிப்படையைப் புரிந்துகொள்ளுங்கள்
A while loop checks its condition before each iteration. Identify the starting value, the condition, and the statement that moves you toward stopping. A loop that never changes a condition-related value may run forever.
விரிவான விளக்கங்கள்
Java Loops
Loops-ஐ use பண்ணி ஒரே code-ஐ பல தடவை execute பண்ணலாம் — நிறைய time மிச்சமாகும்.
While Loop
Condition true-ஆ இருக்குற வரைக்கும் code block execute ஆகும்:
Loop-க்கு உள்ளே இருக்குற code-ஐ ஒவ்வொரு தடவையுங்கள்யும் update பண்றதை மறக்காத — இல்லன்னா infinite loop-ஆ ஆகிடும்!
Do/While Loop
While loop-ஓட variant — condition check பண்றதுக்கு முன்னாடி code ஒரு தடவை execute ஆகும்:
// while loop
int i = 1;
while (i <= 5) {
System.out.print(i + " "); // 1 2 3 4 5
i++;
}
// do-while — executes at least once
int n = 0;
do {
System.out.println("Attempt: " + n);
n++;
} while (n < 3);
// Sum using while
int sum = 0, num = 1;
while (num <= 10) { sum += num; num++; }
System.out.println("Sum 1-10: " + sum); // 55`do-while` loop `while` loop-ல் இருந்து எப்படி வேறுபடுகிறது?
- do-while faster-ஆக இருக்கும்
- do-while loop body-ஐ குறைந்தது ஒரு முறை execute செய்யும்
- do-while infinite loop create செய்யாது
- வித்தியாசம் இல்லை
Reveal answer / விடை
do-while loop body-ஐ குறைந்தது ஒரு முறை execute செய்யும்
`do-while` loop condition-ஐ check செய்வதற்கு முன் body-ஐ execute செய்யும். அதனால் condition false-ஆக இருந்தாலும் ஒரு முறை run ஆகும். User input validation-க்கு ideal.
முழுமையான உதாரணம்
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
public class Main {
public static void main(String[] args) throws Exception {
int attempt = 1;
while (attempt <= 3) {
System.out.println("Attempt " + attempt);
attempt++;
}
}
}Predict first. Then reveal the expected output / வெளியீடு
Attempt 1 Attempt 2 Attempt 3
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
நீங்களே முயற்சி செய்யுங்கள்
Practice challenge
Start attempt at 4 without changing the condition. How many times does the body run?
Reveal the answer and reasoning / விடை
Zero times: the condition is false before the first iteration.
தவிர்க்க வேண்டிய தவறு: Forgetting attempt++ here creates a non-terminating loop. Stop a stuck run rather than repeatedly starting more executions.
பாடத்தின் சுருக்கம்
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.