Mada za sehemu hiiImplementing control structures in programmingMada 3
- Implementing condition structures
- Implementing loop structures
- Implementing nested structures
Implementing Condition Structures
Condition structures (also called conditional statements) are programming tools that let your program make decisions. Based on whether a condition is true or false, the program executes different blocks of code. This is essential for solving problems where different actions are needed depending on the situation.
The if statement is the most basic conditional structure. It checks a condition and executes code only when that condition is true.
Single Alternative (if only)
This checks one condition and runs code only if it is true.
Syntax:
if (condition)
{
// code to execute when condition is true
}
Example: Checking for a positive number
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter an integer: ";
cin >> number;
if (number > 0) {
cout << "You entered a positive number: " << number << endl;
}
cout << "This statement is always executed.";
return 0;
}
Output (if user enters 4):
Enter an integer: 4
You entered a positive number: 4
This statement is always executed.
Two Alternatives (if-else)

When you have two possible outcomes, use if-else. One block runs if the condition is true, the other runs if it is false.
Syntax:
if (condition)
{
// code if condition is true
}
else
{
// code if condition is false
}
Example: Even or Odd Number
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a Number: ";
cin >> num;
if (num % 2 == 0)
{
cout << "It is an even number" << endl;
}
else
{
cout << "It is an odd number" << endl;
}
return 0;
}
Output (if user enters 4):
Enter a Number: 4
It is an even number
More Than Two Alternatives (if-else if-else)
When you have multiple conditions to check, use else if to test each condition in order.
Syntax:
if (condition 1)
{
// code for option 1
}
else if (condition 2)
{
// code for option 2
}
else if (condition 3)
{
// code for option 3
}
else
{
// default code (when none of the above conditions are true)
}
Example: Finding the Largest of Three Numbers
#include <iostream>
using namespace std;
int main() {
double n1, n2, n3;
cout << "Enter three numbers: ";
cin >> n1 >> n2 >> n3;
if (n1 >= n2 && n1 >= n3)
cout << "Largest number: " << n1;
else if (n2 >= n1 && n2 >= n3)
cout << "Largest number: " << n2;
else
cout << "Largest number: " << n3;
return 0;
}
Key Point: The conditions are checked from top to bottom. Once a true condition is found, its block executes and the rest are skipped.
The switch statement tests one variable against multiple values (called cases). It is useful when you have many discrete options to compare.
Syntax:
switch (expression)
{
case value1:
// code for value1
break;
case value2:
// code for value2
break;
// more cases as needed
default:
// code when no case matches
}
Important Notes:
- The
breakkeyword stops the program from checking remaining cases - The
defaultcase runs when no other case matches (optional but recommended)
Example: Simple Grading System
#include <iostream>
using namespace std;
int main() {
char grade;
cout << "Enter Your Grade (A-F): ";
cin >> grade;
switch (grade) {
case 'A':
cout << "Excellent!" << endl;
break;
case 'B':
cout << "Very Good!" << endl;
break;
case 'C':
cout << "Well done" << endl;
break;
case 'D':
cout << "You passed" << endl;
break;
case 'F':
cout << "Better try again" << endl;
break;
default:
cout << "Invalid grade" << endl;
}
cout << "Your grade is " << grade << endl;
return 0;
}
Output (if user enters A):
Enter Your Grade (A-F): A
Excellent!
Your grade is A
| if Statement | Switch Statement |
|---|---|
| Good for ranges (score > 80) | Good for exact values (grade == 'A') |
| Good for complex conditions | Good for comparing one variable to many constants |
| Can check multiple variables | Checks only one variable |
- Condition structures let programs make decisions
- Use if for single or multiple conditions with ranges
- Use switch when comparing one variable to many specific values
- Always test conditions in the right order
- The
elseordefaultblock handles all remaining cases
In Tanzania, condition structures are used in mobile money systems like M-Pesa and banking apps. For example, when a shop owner in Dar es Salaam uses a point-of-sale program to calculate the total price of items bought by a customer, the program uses if-else statements to check if the total exceeds a certain amount and applies different discount rates accordingly — just like the grading example above applies different remarks based on score ranges.
Jinsi utakavyotathminiwaTathmini ya mchakato · Tathmini ya matokeo · Tathmini ya maarifa
Tathmini ya mchakato
The student should be able to:
- Write syntactically correct conditional statements.
- Apply nesting and logical operators in condition structures.
- Debug andtest programs involving condition structures.
Tathmini ya matokeo
Programs that execute correctly with all possible condition outcomes handled are created.
Tathmini ya maarifa
Methods used: The trainee should explain how to:
- Use conditional statements (if, if-else, switchcase) effectively.
- Combine logical operators (e.g., AND, OR, NOT) for complex conditions.
Principles: The student should explain the principle of:
- Logical decision-making in programming.
- Efficiency and clarity in designing condition structures.
- Testing all possible outcomes to ensure robust code.
Theories: The trainee should explain: The trainee should explain:
- The role of branching in program execution.
- How condition structures affect program flow and logic
Detailed knowledge about:
- Common errors in conditional logic and debugging strategies.
Swali
What is the correct syntax for a single alternative if statement 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 kuulizaMajadiliano
Hakuna maswali bado