Control flow defines how a program executes based on conditions and repetition across different programming languages.
1๏ธโฃ Conditional Statements (if / else)
Used to make decisions.
Python
age = 20
if age >= 18:
print("Eligible")
else:
print("Not Eligible")
JavaScript
let age = 20;
if (age >= 18) {
console.log("Eligible");
} else {
console.log("Not Eligible");
}
Java
int age = 20;
if (age >= 18) {
System.out.println("Eligible");
} else {
System.out.println("Not Eligible");
}
๐ Same concept, different syntax.
2๏ธโฃ Multiple Conditions (if-elif / else if)
Python
marks = 75
if marks >= 90:
print("A")
elif marks >= 60:
print("B")
else:
print("C")
JavaScript / Java
if (marks >= 90) {
console.log("A");
} else if (marks >= 60) {
console.log("B");
} else {
console.log("C");
}
3๏ธโฃ Loops
๐น For Loop (fixed iterations)
Python
for i in range(1, 6):
print(i)
JavaScript
for (let i = 1; i <= 5; i++) {
console.log(i);
}
Java
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
๐ Output: 1 to 5
๐ Used when number of iterations is fixed.
๐น While Loop (condition-based)
Python
i = 1
while i <= 5:
print(i)
i += 1
JavaScript / Java
let i = 1;
while (i <= 5) {
console.log(i);
i++;
}
๐ Runs until condition becomes false.
4๏ธโฃ Break Statement
Stops loop immediately.
Python
for i in range(1, 6):
if i == 3:
break
print(i)
JavaScript / Java
for (let i = 1; i <= 5; i++) {
if (i == 3) break;
console.log(i);
}
๐ Output: 1, 2
๐ Loop stops when condition is met.
5๏ธโฃ Continue Statement
Used to skip the current iteration and continue the loop.
Python
for i in range(1, 6):
if i == 3:
continue
print(i)
JavaScript / Java
for (let i = 1; i <= 5; i++) {
if (i == 3) continue;
console.log(i);
}
๐ Output: 1, 2, 4, 5
๐ Skips 3.
6๏ธโฃ Switch Case
Used for multiple conditions.
JavaScript
let day = 2;
switch(day) {
case 1: console.log("Monday"); break;
case 2: console.log("Tuesday"); break;
default: console.log("Other day");
}
Java
int day = 2;
switch(day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
default: System.out.println("Other day");
}
๐ Python uses if-elif instead.
โญ Key Insight (Important for Interviews)
โข Logic is same across all languages
โข Only syntax changes
โข Interviewers focus on: how you think not which language you use
Double Tap โฅ๏ธ For More