Java For Loop
Keep counted repetition in one place.
முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.
இந்தப் பாடத்தில்…
- Explain the key idea behind For Loop.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
அடிப்படையைப் புரிந்துகொள்ளுங்கள்
A conventional for loop keeps initialization, continuation condition, and update together. Trace the condition before each iteration, including the final failed check. That makes off-by-one mistakes easier to spot.
விரிவான விளக்கங்கள்
Java For Loop
Loop எத்தனை தடவை ஓடணும்னு தெரியும்போது for loop use பண்றோம்.
For loop-ல மூணு parts இருக்கு:
- Statement 1 — loop ஆரம்பிக்கும்போது ஒரு தடவை execute ஆகும்
- Statement 2 — loop execute ஆகணும்னா இந்த condition true-ஆ இருக்கணும்
- Statement 3 — ஒவ்வொரு iteration-க்கும் கடைசில execute ஆகும்
For-Each Loop
Arrays-ல உள்ள elements-ஐ iterate பண்றதுக்கு for-each loop மிகவும் convenient:
// Count from 1 to 5
for (int i = 1; i <= 5; i++) {
System.out.print(i + " "); // 1 2 3 4 5
}
// Enhanced for-each
int[] nums = {10, 20, 30, 40, 50};
for (int n : nums) {
System.out.print(n + " "); // 10 20 30 40 50
}
// Nested loops
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
System.out.printf("%3d", i * j);
}
System.out.println();
}Enhanced for loop (`for-each`) எதற்கு சிறந்தது?
- Index-based access-க்கு
- Array/Collection-ன் எல்லா elements-ஐயும் iterate செய்ய
- Reverse order iteration-க்கு
- Nested loops-க்கு
Reveal answer / விடை
Array/Collection-ன் எல்லா elements-ஐயும் iterate செய்ய
Enhanced for loop (`for (String item : list)`) collection-ன் elements-ஐ clean-ஆக iterate செய்யும். ஆனால் index தேவைப்பட்டால் அல்லது modify செய்ய வேண்டுமென்றால் traditional for loop பயன்படுத்தவும்.
முழுமையான உதாரணம்
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 total = 0;
for (int n = 1; n <= 4; n++) {
total += n;
}
System.out.println(total);
}
}Predict first. Then reveal the expected output / வெளியீடு
10
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
Change the upper bound to 5 and predict the new total.
Reveal the answer and reasoning / விடை
The result is 15, because 1 + 2 + 3 + 4 + 5 = 15.
தவிர்க்க வேண்டிய தவறு: n < 4 and n <= 4 describe different sets of values. Decide whether the upper bound is inclusive.
பாடத்தின் சுருக்கம்
- 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.