JavaScript · Lesson 10 / 32 · தமிழ்
Control Flow
கருத்தைப் புரிந்துகொள்ளுங்கள். Code-ஐ மாற்றி இயக்குங்கள். ஒரு சிறிய பயிற்சியால் கற்றதை உறுதி செய்யுங்கள்.
மூலப் பாட விளக்கங்கள் தமிழில் உள்ளன. கூடுதல் concept, practice மற்றும் answer notes ஆங்கிலத்தில் உள்ளன.
முக்கியக் கருத்து
Conditions choose a path and loops repeat work. Trace each iteration and the condition it evaluates before predicting the result.
Learning goal: Explain what the example does, predict its result, and change it to solve the practice task.
விரிவான விளக்கங்கள்
JavaScript-ல Control Flow
Control flow என்பது statements எந்த order-ல execute ஆகும்னு decide பண்றது. JavaScript-ல conditional statements மற்றும் loops மூலம் program flow-ஐ control பண்ணலாம்.
if / else if / else
let score = 85;
if (score >= 90) {
console.log("Grade: A");
} else if (score >= 80) {
console.log("Grade: B"); // ← இது run ஆகும்
} else if (score >= 70) {
console.log("Grade: C");
} else {
console.log("Grade: F");
}Ternary Operator
// condition ? valueIfTrue : valueIfFalse
let age = 20;
let status = age >= 18 ? "Adult" : "Minor";
console.log(status); // "Adult"switch Statement
let day = "Monday";
switch (day) {
case "Monday":
case "Tuesday":
case "Wednesday":
case "Thursday":
case "Friday":
console.log("Weekday");
break;
case "Saturday":
case "Sunday":
console.log("Weekend");
break;
default:
console.log("Unknown day");
}break மறக்காதே! இல்லன்னா next case-க்கும் execution போயிடும் (fall-through).
Loops
// for loop
for (let i = 0; i < 5; i++) {
console.log(i); // 0, 1, 2, 3, 4
}
// while loop
let count = 0;
while (count < 3) {
console.log(count++);
}
// for...of — values-ஐ iterate பண்ண
const fruits = ["apple", "banana", "cherry"];
for (const fruit of fruits) {
console.log(fruit);
}
// for...in — keys/indices-ஐ iterate பண்ண
const user = { name: "Alice", age: 25 };
for (const key in user) {
console.log(key, user[key]);
}break மற்றும் continue
// break — loop-ஐ முழுவதும் exit பண்ணும்
for (let i = 0; i < 10; i++) {
if (i === 5) break;
console.log(i); // 0, 1, 2, 3, 4
}
// continue — current iteration skip பண்ணும்
for (let i = 0; i < 5; i++) {
if (i === 2) continue;
console.log(i); // 0, 1, 3, 4 (2 skip ஆகும்)
}முழுமையான உதாரணம்
Run this complete index.js program in the JavaScript console editor below. The earlier reference fragments may need a browser page or other surrounding setup.
for (let lesson = 1; lesson <= 3; lesson++) {
if (lesson === 2) {
console.log("Review lesson " + lesson);
} else {
console.log("Study lesson " + lesson);
}
}எதிர்பார்க்கும் முடிவைப் பாருங்கள்
Study lesson 1 Review lesson 2 Study lesson 3
மாற்றி இயக்கிப் பாருங்கள்
Load the example, change a value, predict the output, and run it again. Use the console output to check your reasoning.
Editor load ஆனதும் code-ஐ ஏற்றி Run அழுத்துங்கள்.
OneCompiler என்ற இலவச வெளிப்புற சேவை code-ஐ இயக்குகிறது. Run செய்யும் code அங்கு அனுப்பப்படும். தனிப்பட்ட தகவல்கள் அல்லது secret keys உள்ளிட வேண்டாம்.
சிறிய பயிற்சி
Practice challenge
Extend the loop to lesson 4.
விடை மற்றும் விளக்கம்
One additional line appears: Study lesson 4.
தவிர்க்க வேண்டிய தவறு: A loop must make progress toward stopping. Forgetting an increment can create an infinite loop.
அடுத்த பாடத்திற்குத் தயாரா?
- Explain the example in your own words.
- Complete the challenge before revealing its answer.
- Describe the common mistake and how to avoid it.