Control Structures (Python and JavaScript)
Subtopics: If–Else, Loops, Functions
1. Introduction to Control Structures
In digital processing and programming, control structures
determine how a program executes instructions.
A program normally runs from top to bottom (sequential execution),
but control structures allow us to:
Make decisions
Repeat actions
Organize reusable blocks of code
The three major control structures are:
1. Conditional statements (If–Else)
2. Loops (Repetition)
3. Functions (Reusable blocks of code)
2. Conditional Statements (If–Else)
Conditional statements allow a program to make decisions.
They help a program choose what to do based on a condition (True
or False).
A. IF Statement
The if statement executes a block of code only if a condition is true.
🔹 Python Example
age = 18
if age >= 18:
print("You are an adult")
Explanation:
If age is 18 or more, it prints the message.
If the condition is false, nothing happens.
🔹 JavaScript Example
let age = 18;
if (age >= 18) {
[Link]("You are an adult");
}
B. IF–ELSE Statement
Used when there are two possible outcomes.
🔹 Python Example
score = 45
if score >= 50:
print("Pass")
else:
print("Fail")
🔹 JavaScript Example
let score = 45;
if (score >= 50) {
[Link]("Pass");
} else {
[Link]("Fail");
}
C. IF–ELIF–ELSE (Multiple Conditions)
🔹 Python Example
score = 75
if score >= 70:
print("Excellent")
elif score >= 50:
print("Good")
else:
print("Fail")
🔹 JavaScript Example
let score = 75;
if (score >= 70) {
[Link]("Excellent");
} else if (score >= 50) {
[Link]("Good");
} else {
[Link]("Fail");
}
3. Loops (Repetition Structures)
Loops are used to repeat a block of code several times.
They help automate repetitive tasks.
A. FOR Loop
Used when the number of repetitions is known.
🔹 Python Example
for i in range(5):
print("Hello", i)
Output:
Hello 0
Hello 1
Hello 2
Hello 3
Hello 4
🔹 JavaScript Example
for (let i = 0; i < 5; i++) {
[Link]("Hello", i);
}
B. WHILE Loop
Used when the number of repetitions is unknown.
🔹 Python Example
count = 1
while count <= 5:
print(count)
count += 1
🔹 JavaScript Example
let count = 1;
while (count <= 5) {
[Link](count);
count++;
}
C. Break and Continue
Break
Stops the loop immediately.
Continue
Skips the current iteration.
🔹 Python Example
for i in range(5):
if i == 3:
break
print(i)
🔹 JavaScript Example
for (let i = 0; i < 5; i++) {
if (i === 3) {
break;
}
[Link](i);
}
4. Functions
A function is a reusable block of code designed to perform a specific
task.
Functions help:
Avoid repetition
Improve readability
Organize programs
A. Defining a Function
🔹 Python Example
def greet(name):
print("Hello", name)
greet("Peter")
Explanation:
def is used to define a function.
name is a parameter.
🔹 JavaScript Example
function greet(name) {
[Link]("Hello " + name);
}
greet("Peter");
B. Function with Return Value
A function can return a value.
🔹 Python Example
def add(a, b):
return a + b
result = add(5, 3)
print(result)
🔹 JavaScript Example
function add(a, b) {
return a + b;
}
let result = add(5, 3);
[Link](result);
C. Arrow Function (JavaScript Modern Method)
const add = (a, b) => {
return a + b;
};
5. Comparison Between Python and JavaScript
Feature Python JavaScript
Block Structure Uses indentation Uses curly braces {}
Variable Keyword No keyword required let, var, const
Feature Python JavaScript
Function Keyword def function
Output print() [Link]()
6. Practical Application in Digital Processing
Control structures are used in:
Form validation systems
Login authentication
Grade processing
Calculator applications
Data filtering
Automation systems
Game development
Web development
Example:
A school grading system uses if–else.
ATM software uses loops and functions.
Websites use JavaScript functions for interaction.