0% found this document useful (0 votes)
6 views18 pages

Lesson 6 & 7

Uploaded by

gnhj9by4pr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views18 pages

Lesson 6 & 7

Uploaded by

gnhj9by4pr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Lesson 6: Conditional Control Structure (Pages 79-88)

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;

if (age >= 18) {


cout << "You are eligible to vote.";
}

○​ 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;

if (age >= 18) {


cout << "You are eligible to vote.";
} else {
cout << "You are not eligible to vote.";
}

○​ 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;

if (score > 85) {


cout << "Grade: A";
} else if (score >= 70) {
cout << "Grade: B";
} else if (score >= 50) {
cout << "Grade: C";
} else {
cout << "Grade: D";
}

○​ 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.

4.​ switch Statement:


○​ The switch statement is used to execute one out of many possible blocks of
code based on the value of a variable. It is typically used when there are many
possible values to check.

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.

What will the following code output?​



int x = 5;
if (x > 10) {
cout << "Greater";
} else {
cout << "Smaller";
}

○​ The output will be "Smaller" because x is not greater than 10.


3.​ What happens if you forget to include the break statement in a switch case?
○​ If you forget the break statement, the program will continue to execute the
subsequent cases until it hits a break or reaches the end of the switch block.
This is called "fall-through."

Lesson 7: Repetition Control Structure (Pages 89-100)

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:

1.​ for Loop:


○​ The for loop is used when the number of iterations is known in advance. It
initializes a counter, checks a condition, and updates the counter after each
iteration.

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.

3.​ do-while Loop:


○​ The do-while loop is similar to the while loop but guarantees that the code
inside the loop runs at least once before checking the condition.

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) {

// Code to execute if condition is true

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:

int age = 20;

if (age >= 18) {

cout << "You are an adult.";

●​ 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:

●​ The if...else statement is an extension of the if statement. It allows you to execute


one block of code if the condition is true, and a different block of code if the condition is
false.

Syntax:

if (condition) {

// Code to execute if condition is true

} else {

// Code to execute if condition is false

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:

int age = 16;

if (age >= 18) {

cout << "You are an adult.";

} else {

cout << "You are a minor.";

●​ 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:

●​ A nested if statement occurs when an if statement is placed inside another if or


else block. This is useful for checking more complex conditions or making decisions
based on multiple conditions.

Syntax:

if (condition1) {

if (condition2) {

// Code to execute if both conditions are true

Explanation:

●​ The inner if statement is only evaluated if the outer if condition is true.


●​ This allows you to create more complex decision-making processes.
Example:

int age = 20;

bool isStudent = true;

if (age >= 18) {

if (isStudent) {

cout << "You are an adult student.";

} else {

cout << "You are an adult.";

●​ 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:

// Code to execute if variable == value1

break;

case value2:

// Code to execute if variable == value2

break;

default:

// Code to execute if no case matches

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:

cout << "Monday";

break;

case 2:

cout << "Tuesday";

break;

case 3:

cout << "Wednesday";

break;

default:

cout << "Invalid day";

●​ Here, the value of day is 3, so the output will be "Wednesday."

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) {

// Code to execute as long as condition is true

Explanation:

●​ The condition is evaluated before each iteration.


●​ The loop will keep executing as long as the condition is true. If the condition becomes
false, the loop stops.

Example:

int i = 1;

while (i <= 5) {

cout << i << " ";

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:

●​ The loop body is executed once before the condition is tested.


●​ If the condition is true, the loop will continue. If it is false, the loop stops after the first
execution.

Example:

int i = 1;

do {

cout << i << " ";

i++; // Increment i

} while (i <= 5);

●​ 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) {

// Code if condition is true

} else {

// Code if condition is false

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:

int number = -5;

if (number >= 0) {

cout << "Positive or zero";

} else {

cout << "Negative number";

●​ 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) {

// Code to execute if condition1 is true

} else if (condition2) {

// Code to execute if condition1 is false and condition2 is true

} else {

// Code to execute if all conditions are false

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:

int score = 85;

if (score > 90) {

cout << "Grade: A";

} else if (score >= 80) {

cout << "Grade: B";

} else if (score >= 70) {

cout << "Grade: C";

} else {

cout << "Grade: D";

●​ Since score is 85, the program will output "Grade: B" because it matches the second
condition (score >= 80).

Summary

●​ if: Executes code based on a single condition.


●​ if...else: Provides an alternative block of code if the condition is false.
●​ Nested if: An `

You might also like