C++ Programing Language
Chapter 3: Control Flow and Loops in C++
Introduction:
3.1 Conditional Statements:
Conditional statements allow us to control the flow of our program based on different conditions. C++ provides two primary conditional statements: the if statement and the switch statement.
3.1.1 The if Statement:
The if statement evaluates a condition and executes a block of code if the condition is true. The basic syntax is as follows:
code
```c++
if (condition) {
// Code to execute if the condition is true
}
```
For example,
consider a program that checks if a number is positive:
Code
```c++
int number;
std::cout << "Enter a number: ";
std::cin >> number;
if (number > 0) {
std::cout << "The number is positive." << std::endl;
}
```
In this example, if the condition `(number > 0)` is true, the message "The number is positive." will be displayed.
3.1.2 The else Statement:
The else statement is used in conjunction with the if statement to specify a block of code to execute when the condition is false. The syntax is as follows:
Code
```c++
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
```
For example,
let's modify our previous program to handle negative numbers as well:
Code
```c++
int number;
std::cout << "Enter a number: ";
std::cin >> number;
if (number > 0) {
std::cout << "The number is positive." << std::endl;
} else {
std::cout << "The number is non-positive." << std::endl;
}
```
Now, if the number entered is not greater than 0, the message "The number is non-positive." will be displayed.
3.1.3 The switch Statement:
The switch statement allows us to evaluate a variable or an expression against a series of possible values and execute different blocks of code based on the matched value. The syntax is as follows:
Code
```c++
switch (expression) {
case value1:
// Code to execute if expression matches value1
break;
case value2:
// Code to execute if expression matches value2
break;
// More cases...
default:
// Code to execute if no cases match the expression
break;
}
```
For example,
consider a program that displays the name of a day based on its corresponding number:
Code
```c++
int day;
std::cout << "Enter a day number (1-7): ";
std::cin >> day;
switch (day) {
case 1:
std::cout << "Sunday" << std::endl;
break;
case 2:
std::cout << "Monday" << std::endl;
break;
// More cases...
default:
std::cout << "Invalid day" << std::endl;
break;
}
```
In this example, if the user enters `1`, the program will display "Sunday," and so on. If an invalid day number is entered, the message "Invalid day" will be displayed.
3.2 Loops:
Loops are used to repeatedly execute a block of code as long as a specified condition is true. C++ provides three types of loops: the while loop, the do-while loop, and the for loop.
3.2.1 The while Loop:
The while loop executes a block of code repeatedly as long as a specified condition is true. The basic syntax is as follows:
Code
```c++
while (condition) {
// Code to execute while the condition is true
}
```
For example,
let's write a program to print the numbers from 1 to 5 using a while loop:
Code
```c++
int i = 1;
while (i <= 5) {
std::cout << i << " ";
i++;
}
std::cout << std::endl;
```
In this example, the loop will iterate five times, displaying the numbers 1, 2, 3, 4, and 5.
3.2.2 The do-while Loop:
The do-while loop is similar to the while loop but guarantees that the block of code is executed at least once before checking the condition. The basic syntax is as follows:
Code
```c++
do {
// Code to execute
} while (condition);
```
Let's modify our previous program to use a do-while loop instead:
Code
```c++
int i = 1;
do {
std::cout << i << " ";
i++;
} while (i <= 5);
std::cout << std::endl;
```
The output will remain the same, but this time, the loop will execute even if the condition is initially false.
3.2.3 The for Loop:
The for loop is a compact loop that combines initialization, condition, and increment/decrement in a single line. The basic syntax is as follows:
Code
```c++
for (initialization; condition; increment/decrement) {
// Code to execute
}
```
Let's rewrite our previous program using a for loop:
Code
```c++
for (int i = 1; i <= 5; i++) {
std::cout << i << " ";
}
std::cout << std::endl;
```
The output and functionality remain the same, but the for loop offers a more concise way to express the iteration.
Conclusion:
In Chapter 3, we explored control flow structures and different types of loops available in C++. We learned how to make decisions using conditional statements like if and switch, and we discovered how to iterate over code using while, do-while, and for loops. Understanding these control flow mechanisms is crucial for building dynamic and interactive programs that respond to different conditions or repeat tasks. Armed with the knowledge of control flow and loops, you can now enhance the logic and functionality of your C++ programs.
0 Comments