0% found this document useful (0 votes)
12 views11 pages

If, If Else

The document provides a comprehensive lecture on control structures in C++, explaining types such as if, if-else, else if, if-else-if ladder, and switch statements. Each structure is accompanied by syntax and examples to illustrate their usage in decision-making within programs. Additionally, it includes assignments for students to practice implementing these control structures.

Uploaded by

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

If, If Else

The document provides a comprehensive lecture on control structures in C++, explaining types such as if, if-else, else if, if-else-if ladder, and switch statements. Each structure is accompanied by syntax and examples to illustrate their usage in decision-making within programs. Additionally, it includes assignments for students to practice implementing these control structures.

Uploaded by

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

Let’s dive into a simple and clear lecture on Control Structures in C++ for your students.

I'll
explain each type of control structure and provide easy-to-understand examples.

Lecture: Control Structures in C++

What are Control Structures?

Control structures allow us to control the flow of our program by making decisions. In other
words, we can tell the computer, “If something happens, do this; otherwise, do that.” This is very
useful when our program needs to behave differently based on certain conditions.

In C++, there are several types of control structures:

1. if
2. if-else
3. else if
4. if-else-if
5. switch

1. if Statement

The if statement checks if a condition is true. If it's true, the code inside the if block runs.

Syntax:

cpp
Copy code
if (condition) {
// Code that runs if condition is true
}

Example:

cpp
Copy code
#include <iostream>
using namespace std;

int main() {
int age = 18;

if (age >= 18) {


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

return 0;
}

Explanation:

 Here, we check if age is greater than or equal to 18. If it is, the message "You are eligible
to vote" is printed.

2. if-else Statement

The if-else statement provides an alternative. If the condition is true, the if block runs; if it’s
false, the else block runs.

Syntax:

cpp
Copy code
if (condition) {
// Code that runs if condition is true
} else {
// Code that runs if condition is false
}

Example:

cpp
Copy code
#include <iostream>
using namespace std;

int main() {
int age = 16;

if (age >= 18) {


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

return 0;
}

Explanation:

 Here, age is 16, so the condition age >= 18 is false, and the message "You are not
eligible to vote" is printed.
3. else if Statement

The else if statement allows us to check multiple conditions. If the first condition is false, the
next else if condition is checked, and so on.

Syntax:

cpp
Copy code
if (condition1) {
// Code for condition1
} else if (condition2) {
// Code for condition2
}

Example:

cpp
Copy code
#include <iostream>
using namespace std;

int main() {
int marks = 85;

if (marks >= 90) {


cout << "Grade: A" << endl;
} else if (marks >= 75) {
cout << "Grade: B" << endl;
} else if (marks >= 50) {
cout << "Grade: C" << endl;
} else {
cout << "Grade: F" << endl;
}

return 0;
}

Explanation:

 This example checks the student's marks. If marks are 85, it matches the second condition
marks >= 75, so "Grade: B" is printed.

4. if-else-if Ladder
The if-else-if ladder is a combination of multiple else if statements, allowing us to check
many conditions one after another. Only one of these conditions will be true, and the
corresponding block of code will run.

Syntax:

cpp
Copy code
if (condition1) {
// Code if condition1 is true
} else if (condition2) {
// Code if condition2 is true
} else if (condition3) {
// Code if condition3 is true
} else {
// Code if no conditions are true
}

Example:

cpp
Copy code
#include <iostream>
using namespace std;

int main() {
int temperature = 30;

if (temperature >= 40) {


cout << "It's too hot!" << endl;
} else if (temperature >= 30) {
cout << "It's warm." << endl;
} else if (temperature >= 20) {
cout << "It's mild." << endl;
} else {
cout << "It's cold!" << endl;
}

return 0;
}

Explanation:

 This example checks the temperature and gives different messages depending on the
value. Since the temperature is 30, the message "It's warm." is printed.

5. switch Statement

The switch statement is used when you want to compare a single value against many fixed
cases. It's cleaner when you're checking one variable against multiple possible values.
Syntax:

cpp
Copy code
switch (variable) {
case constant1:
// Code for constant1
break;
case constant2:
// Code for constant2
break;
// More cases
default:
// Code if no cases match
}

Example:

cpp
Copy code
#include <iostream>
using namespace std;

int main() {
int day = 3;

switch (day) {
case 1:
cout << "Monday" << endl;
break;
case 2:
cout << "Tuesday" << endl;
break;
case 3:
cout << "Wednesday" << endl;
break;
case 4:
cout << "Thursday" << endl;
break;
case 5:
cout << "Friday" << endl;
break;
default:
cout << "Weekend" << endl;
}

return 0;
}

Explanation:

 Here, the variable day is checked against different cases (1, 2, 3, etc.). Since day is 3, the
message "Wednesday" is printed.
Summary of Control Structures:

 if: Runs a block of code if a condition is true.


 if-else: Runs one block of code if the condition is true, and another if it's false.
 else if: Lets you check multiple conditions in sequence.
 if-else-if Ladder: A chain of conditions where only one block of code is executed.
 switch: Checks a single variable against multiple possible values and runs the
corresponding block of code.

Assignment for Students:

1. Write a program that asks the user to enter their marks and prints their grade based on the
following rules:
o Marks >= 90: Grade A
o Marks >= 75: Grade B
o Marks >= 50: Grade C
o Marks >= 40: Grade D
o Marks < 40: Grade F
2. Write a program that takes an input number (1 to 7) and prints the day of the week using
a switch statement (e.g., 1 = Monday, 2 = Tuesday, etc.).

1. if Statement

The if statement checks if a condition is true, and if it is, it executes a block of code.

Syntax:
cpp
Copy code
if (condition) {
// Code to execute if the condition is true
}

Example:

cpp
Copy code
#include <iostream>
using namespace std;

int main() {
int number = 10;

if (number > 5) {
cout << "The number is greater than 5!" << endl;
}

return 0;
}

In this example, since number is 10 (which is greater than 5), the condition is true, and the
message is printed.

2. if-else Statement

The if-else statement lets you specify an alternative block of code to execute if the condition is
false.

Syntax:

cpp
Copy code
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}

Example:

cpp
Copy code
#include <iostream>
using namespace std;

int main() {
int number = 3;
if (number > 5) {
cout << "The number is greater than 5!" << endl;
} else {
cout << "The number is less than or equal to 5!" << endl;
}

return 0;
}

In this example, since number is 3 (which is less than 5), the else block is executed, and the
second message is printed.

3. else if Statement

The else if statement lets you check multiple conditions one after the other. If the first
condition is false, it checks the next one.

Syntax:

cpp
Copy code
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else {
// Code to execute if none of the conditions are true
}

Example:

cpp
Copy code
#include <iostream>
using namespace std;

int main() {
int number = 7;

if (number > 10) {


cout << "The number is greater than 10!" << endl;
} else if (number > 5) {
cout << "The number is greater than 5 but less than or equal to 10!"
<< endl;
} else {
cout << "The number is 5 or less!" << endl;
}

return 0;
}
In this example, number is 7, so the else if condition number > 5 is true, and the second
message is printed.

4. if-else-if Ladder

An if-else-if ladder is useful when you have multiple conditions to check in sequence.

Syntax:

cpp
Copy code
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else if (condition3) {
// Code to execute if condition3 is true
} else {
// Code to execute if none of the conditions are true
}

Example:

cpp
Copy code
#include <iostream>
using namespace std;

int main() {
int marks = 75;

if (marks >= 90) {


cout << "Grade: A" << endl;
} else if (marks >= 75) {
cout << "Grade: B" << endl;
} else if (marks >= 50) {
cout << "Grade: C" << endl;
} else {
cout << "Grade: F" << endl;
}

return 0;
}

In this example, since marks is 75, the second condition (marks >= 75) is true, and "Grade: B"
is printed.

5. switch Statement
The switch statement is used when you want to check a variable against different constant
values. It's a cleaner alternative to using multiple else if statements when you're comparing the
same variable.

Syntax:

cpp
Copy code
switch (expression) {
case constant1:
// Code to execute if expression equals constant1
break;
case constant2:
// Code to execute if expression equals constant2
break;
// You can add more cases here
default:
// Code to execute if no cases match
}

Example:

cpp
Copy code
#include <iostream>
using namespace std;

int main() {
int day = 3;

switch (day) {
case 1:
cout << "Monday" << endl;
break;
case 2:
cout << "Tuesday" << endl;
break;
case 3:
cout << "Wednesday" << endl;
break;
case 4:
cout << "Thursday" << endl;
break;
case 5:
cout << "Friday" << endl;
break;
default:
cout << "Weekend!" << endl;
}

return 0;
}

In this example, day is 3, so the case 3 block is executed, and "Wednesday" is printed.
Summary:

 if: Executes a block of code if a condition is true.


 if-else: Executes one block of code if a condition is true, another if false.
 else if: Checks multiple conditions one after another.
 switch: Compares a value against several constant values.

These control structures are very useful for making decisions in your programs. The if
statements are versatile and flexible, while switch is good for specific, fixed choices.

You might also like