Lesson 6 & 7
Lesson 6 & 7
Overview:
Conditional control structures allow a program to choose different paths of execution based on
conditions. They are essential for making decisions in a program. The primary conditional
statements include if, else if, else, and switch.
Key Concepts:
1. if Statement:
○ The if statement is used to test a condition. If the condition evaluates to true,
the code inside the if block is executed. If the condition is false, the block is
skipped.
Syntax:
if (condition) {
// Code to execute if condition is true
}
○
Example:
int age;
cout << "Enter your age: ";
cin >> age;
○ In this case, if the age is 18 or older, the message will say "You are eligible to
vote."
2. else Statement:
○ The else statement works in conjunction with the if statement. If the condition
in the if is false, the else block will execute.
○
Syntax:
if (condition) {
// Code if condition is true
} else {
// Code if condition is false
}
○
Example:
int age;
cout << "Enter your age: ";
cin >> age;
○ Here, if the person is under 18, the program will display "You are not eligible to
vote."
3. else if Statement:
○ The else if statement allows you to check multiple conditions in sequence. If
the first if condition is false, the program will check the conditions in the else
if blocks.
Syntax:
ruby
Copy
if (condition1) {
// Code if condition1 is true
} else if (condition2) {
// Code if condition2 is true
} else {
// Code if none of the conditions are true
}
Example:
int score;
cout << "Enter your score: ";
cin >> score;
○ In this example, the program will display a grade based on the score entered. If
the score is above 85, the program will print "Grade: A". If it's between 70 and 85,
it will print "Grade: B", and so on.
Syntax:
switch (variable) {
case value1:
// Code to execute if variable == value1
break;
case value2:
// Code to execute if variable == value2
break;
default:
// Code to execute if no case matches
}
Example:
int day;
cout << "Enter a number (1-7) representing a day of the week: ";
cin >> day;
switch (day) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
case 7:
cout << "Sunday";
break;
default:
cout << "Invalid input";
}
○ This program checks the value of day and prints the corresponding day of the
week. If the input is outside the range 1-7, it prints "Invalid input."
Review Questions:
1. What is the difference between if, else, and else if statements?
○ if checks a condition and executes the associated block if true.
○ else is used when the if condition is false and provides an alternative path.
○ else if allows checking multiple conditions in sequence.
2. How does the switch statement work and when would you use it over if
statements?
○ The switch statement is ideal when you have a single variable with multiple
possible values, and you want to execute different blocks of code depending on
the value. It is more readable than using multiple if-elsestatements in cases
with many conditions.
Overview:
Repetition control structures, or loops, allow a block of code to execute repeatedly. There are
three common types of loops: for, while, and do-while.
Key Concepts:
Syntax:
for (initialization; condition; increment/decrement) {
// Code to be repeated
}
○
Example:
for (int i = 1; i <= 5; i++) {
cout << i << " ";
}
○ This loop prints numbers from 1 to 5. The initialization (int i = 1) sets the
starting point, the condition (i <= 5) ensures the loop runs until i exceeds 5,
and the increment (i++) increases i by 1 after each iteration.
2. while Loop:
○ The while loop repeats as long as the specified condition is true. It first checks
the condition, and if true, it executes the code inside the loop.
Syntax:
while (condition) {
// Code to be repeated
}
○
Example:
int i = 1;
while (i <= 5) {
cout << i << " ";
i++;
}
This loop works similarly to the for loop but uses a separate initialization (int i
= 1) and increment (i++) inside the loop.
Syntax:
do {
// Code to be repeated
} while (condition);
○
Example:
int i = 1;
do {
cout << i << " ";
i++;
} while (i <= 5);
○ This loop prints numbers from 1 to 5, and it will execute at least once even if the
condition (i <= 5) is initially false.
Review Questions:
1. What is the difference between a while loop and a do-while loop?
○ A while loop checks the condition before executing the code, meaning it might
never execute if the condition is false from the start. A do-while loop
guarantees at least one execution, even if the condition is false initially.
2. When would you prefer to use a for loop over a while loop?
○ You would use a for loop when you know how many iterations the loop should
execute. If the number of iterations is determined by a condition, you may prefer
a while loop.
What will the following code output?
int i = 5;
while (i > 0) {
cout << i << " ";
i--;
}
3.
○ The output will be 5 4 3 2 1 because the while loop decreases i after each
iteration, and it runs while i is greater than 0.
4. How can you make sure a loop will never run indefinitely?
○ Make sure that the loop condition eventually becomes false. For while and
do-while loops, ensure that the loop variable is updated within the loop, and for
for loops, check the loop condition and increment/decrement values.
1. if Statement
Definition:
● The if statement is used to evaluate a condition. If the condition evaluates to true, the
block of code inside the if statement will execute. If the condition is false, the code
inside the if block is skipped.
Syntax:
if (condition) {
Explanation:
● The condition inside the parentheses is a Boolean expression (i.e., it evaluates to either
true or false).
● If the condition is true, the code inside the block will execute. If it is false, the
program will continue after the ifblock.
Example:
● In this example, the condition age >= 18 evaluates to true (because age is 20), so
the message "You are an adult." will be printed.
2. if...else Statement
Definition:
Syntax:
if (condition) {
} else {
Explanation:
● The else block will execute only if the if condition evaluates to false.
● The else block is optional, and you can leave it out if you don’t want any action when
the condition is false.
Example:
} else {
● In this example, since the condition age >= 18 is false (because age is 16), the
message "You are a minor." will be printed.
3. Nested if Statements
Definition:
Syntax:
if (condition1) {
if (condition2) {
Explanation:
if (isStudent) {
} else {
● Here, the program first checks if age >= 18. Since the age is 20, it proceeds to check if
the person is a student. The output will be "You are an adult student." because
isStudent is true.
4. switch Statement
Definition:
● The switch statement is used to perform one of many possible actions based on the
value of a variable. It's more efficient than using multiple if...else statements when
there are many possible conditions.
Syntax:
switch (variable) {
case value1:
break;
case value2:
break;
default:
Explanation:
● The switch statement compares the value of variable with each case value.
● If a match is found, the corresponding block of code is executed.
● The break statement is used to exit the switch after a match is found, preventing
fall-through.
● The default case is optional and executes if no cases match.
Example:
int day = 3;
switch (day) {
case 1:
break;
case 2:
break;
case 3:
break;
default:
5. while Loop
Definition:
● A while loop is used to repeat a block of code as long as a specified condition remains
true. If the condition is initially false, the loop will not execute even once.
Syntax:
while (condition) {
Explanation:
Example:
int i = 1;
while (i <= 5) {
i++; // Increment i
● This loop prints numbers from 1 to 5. The loop checks the condition i <= 5 before each
iteration, and as long as it is true, the loop executes. When i exceeds 5, the loop stops.
6. do...while Loop
Definition:
● The do...while loop is similar to the while loop, but it guarantees that the code
inside the loop will execute at least once, even if the condition is false initially.
Syntax:
do {
// Code to execute
} while (condition);
Explanation:
Example:
int i = 1;
do {
i++; // Increment i
● This loop will also print numbers from 1 to 5. Even if i were initially greater than 5, the
loop would run at least once.
7. else Statement
Definition:
● The else statement is used to specify a block of code that should be executed if the if
condition is false. It is always paired with an if statement.
Syntax:
if (condition) {
} else {
Explanation:
● If the condition in the if statement is false, the else block will execute.
● The else block is optional and can be omitted if you only need to perform an action
when the condition is true.
Example:
if (number >= 0) {
} else {
● The output will be "Negative number" because the condition number >= 0 is false.
8. else...if Statement
Definition:
● The else if statement allows you to check additional conditions if the first if condition
is false. You can chain multiple else if statements to check various conditions.
Syntax:
if (condition1) {
} else if (condition2) {
} else {
Explanation:
● The program checks each condition in sequence. If one condition is true, the
associated block of code will execute, and the remaining conditions will be skipped.
● If all conditions are false, the code in the else block will execute.
Example:
} else {
● Since score is 85, the program will output "Grade: B" because it matches the second
condition (score >= 80).
Summary