Mada za sehemu hiiDemonstrate an understanding of basic principles of computer programming (using an appropriate structured programming language such as C, Python, etc.)Mada 7
- Describe the concept of programming language (categories, paradigm, generic structures) and programming tools (compiler/interpreter, text editor, IDE, debugger)
- Describe programming tools (compiler/interpreter, text editor, IDE, debugger)
- Install and configure the selected programming language (compiler/interpreter, text editor, IDE, debugger)
- Use programming tools of selected programming language to write a program (compile/run and debug a simple program)
- Use variables, constants, and data types of a selected programming language in a program (operators and expressions)
- Use syntax and constructs of the selected programming language to write programs with branching
- Debug computer programs
Branching in Programming
Branching allows a program to make decisions based on conditions. Instead of running the same instructions every time, the program checks certain conditions and chooses which code to run. This is essential for creating programs that respond differently to different situations.
In programming, we use conditional statements to implement branching. The main statements are:
- if – checks a single condition
- if-else – chooses between two options
- else-if ladder – checks multiple conditions
The if statement executes code only when a condition is true.
Syntax (C and Python)
In C:
if (condition) {
// code to execute if condition is true
}
In Python:
if condition:
# code to execute if condition is true
Example
A program that checks if a student passed an exam:
Python:
score = 75
if score >= 50:
print("You passed the exam!")
C:
int score = 75;
if (score >= 50) {
printf("You passed the exam!\n");
}
In both examples, the message prints only when the score is 50 or more.
The if-else statement provides two paths: one when the condition is true, another when it is false.
Syntax (C and Python)
In C:
if (condition) {
// code if true
} else {
// code if false
}
In Python:
if condition:
# code if true
else:
# code if false
Example
Checking if a number is positive or negative:
Python:
number = -5
if number >= 0:
print("The number is positive or zero.")
else:
print("The number is negative.")
Output: The number is negative.
C:
int number = -5;
if (number >= 0) {
printf("The number is positive or zero.\n");
} else {
printf("The number is negative.\n");
}
When there are more than two possible outcomes, we use the else-if ladder (or elif in Python).
Syntax (C and Python)
In C:
if (condition1) {
// code for condition1
} else if (condition2) {
// code for condition2
} else {
// code if none are true
}
In Python:
if condition1:
# code for condition1
elif condition2:
# code for condition2
else:
# code if none are true
Example: Grading System
A program that assigns grades based on marks:
Python:
marks = 72
if marks >= 80:
print("Grade: A")
elif marks >= 60:
print("Grade: B")
elif marks >= 40:
print("Grade: C")
else:
print("Grade: F")
Output: Grade: B
C:
int marks = 72;
if (marks >= 80) {
printf("Grade: A\n");
} else if (marks >= 60) {
printf("Grade: B\n");
} else if (marks >= 40) {
printf("Grade: C\n");
} else {
printf("Grade: F\n");
}
We often need to check multiple conditions together. The logical operators are:
| Operator | Meaning | Example |
|---|---|---|
and (Python) / && (C) | Both must be true | age >= 18 and has ID |
or (Python) / ` | ` (C) | |
not (Python) / ! (C) | Reverses the condition | not (age < 18) |
Example: Voting Eligibility
A person can vote if they are 18 or older and are a citizen.
Python:
age = 20
is_citizen = True
if age >= 18 and is_citizen:
print("You are eligible to vote.")
else:
print("You cannot vote.")
Output: You are eligible to vote.
C:
int age = 20;
int is_citizen = 1; // 1 means true, 0 means false
if (age >= 18 && is_citizen) {
printf("You are eligible to vote.\n");
} else {
printf("You cannot vote.\n");
}
A shop in Dar es Salaam gives discounts based on purchase amount:
- TZS 10,000 or more: 10% discount
- TZS 5,000 to 9,999: 5% discount
- Below TZS 5,000: no discount
Python:
amount = int(input("Enter purchase amount (TZS): "))
if amount >= 10000:
discount = amount * 0.10
final_price = amount - discount
print(f"Discount: TZS {discount}")
print(f"Final price: TZS {final_price}")
elif amount >= 5000:
discount = amount * 0.05
final_price = amount - discount
print(f"Discount: TZS {discount}")
print(f"Final price: TZS {final_price}")
else:
print("No discount applied.")
print(f"Final price: TZS {amount}")
If the user enters 8000, the output is:
Discount: TZS 400.0
Final price: TZS 7600.0
- Forgetting to indent in Python – indentation defines which code belongs to which branch
- Using single
=instead of==–=assigns a value,==compares values - Forgetting parentheses in C – conditions must be inside
( ) - Not handling all cases – always consider what happens when no condition is true
In Tanzania, branching is used in mobile money systems like M-Pesa. When you buy airtime, the system checks if you have enough balance; if yes, it processes the transaction and deducts the amount, otherwise it displays an error message. Similarly, when applying for a loan through mobile banking, the system uses branching to check your age, income, and existing balance before deciding whether to approve or reject your request.
Swali
Which relational operator is used to check if two values are NOT equal in C?
Ingia ili kuwasilisha jibu lako na lihesabiwe katika umahiri wako.
Ingia ili kufanya mazoeziMwalimu
Umekwama? Niulize chochote kuhusu mada hii.
Ingia ili kumuuliza Mwalimu wa AI wa Sonza kuhusu swali hili.
Ingia ili kuuliza