Control Structures I (if, if-else, and switch)
Learn how to write the if-else and switch statements in C++.
Control structures
Control structures are fundamental in programming languages that allow developers to control the flow of code execution based on certain conditions. These structures enable programmers to create programs that can make decisions, iterate over collections of data, and execute blocks of code repeatedly. This lesson will cover three such structures:
The
if
statementThe
else if
statementThe
switch
statement
Since all control structures rely on conditional statements that involve relational and logical operators, it’s a good idea to start by refreshing our knowledge of these operators.
Relational operators
Relational operators, also known as comparison operators, are used in programming to compare two values and return a boolean result. C++ provides six comparison operators, which are ==
, !=
, >
, >=
, <
, and <=
. When a comparison operator is executed, it returns either 1
or 0
, depending on whether the comparison is true
or false
, respectively. Let’s take a look at an example of a comparison expression and see how it works with different relational operators.
Get hands-on with 1200+ tech skills courses.