Conditional Statements

Conditional statements are fundamental to programming as they allow the execution of specific code blocks based on different conditions. In C++, there are four main conditional statements: if statement, if-else statement, else if statement, and switch statement. Let's dive into each of them!

If Statement

The if statement is used to execute a block of code if a certain condition evaluates to true. Here is the syntax:


            if (condition) {
                // code to be executed if the condition is true
            }
        
Example:

            #include <iostream>

            using namespace std;
            
            int main() {
                int x = 10;

                if (x > 5) {
                    cout << "x is greater than 5";
                }

                return 0;
            }
        

In this example, the code inside the `if` block is executed only if the condition `x > 5` is true. The output of this code will be x is greater than 5.


If-Else Statement

The if-else statement provides an additional alternative block of code to execute if the condition in the if statement evaluates to false. Here is the syntax:


            if (condition) {
                // code to be executed if the condition is true
            } 
            
            else {
                // code to be executed if the condition is false
            }
        
Example:

            #include <iostream>

            using namespace std;
            
            int main() {
                int x = 3;

                if (x > 5) {
                    cout << "x is greater than 5";
                } 
                
                else {
                    cout << "x is less than or equal to 5";
                }

                return 0;
            }
        

In this example, if the condition `x > 5` is true, the code inside the `if` block is executed. Otherwise, the code inside the `else` block is executed.

The output of this code is x is less than or equal to 5.


Else If Statement

The elseif statement, also known as the else-if statement, is an extension of the if-else statement in C++. It allows you to test multiple conditions and execute different code blocks based on these conditions. This statement is especially useful when you have more than two possible outcomes. Here is the syntax:


            if (condition1) {
                // code to be executed if condition1 is true
            } 

            else if (condition2) {
                // code to be executed if condition2 is true
            }
            
            else {
                // code to be executed if none of the conditions are true
            }
        
Example:

            #include <iostream>

            using namespace std;
            
            int main() {
                int x = 10;

                if (x > 5) {
                    cout << "x is greater than 5";
                }

                else if (num < 5) {
                    cout << "x is less than 5";
                }
                
                else {
                    cout << "x is equal to 5";
                }

                return 0;
            }
        

In the above example, the elseif statement is used to test multiple conditions. If the initial condition `x > 5` is false, it moves to the next condition `x < 5`. If this condition is false as well, it moves to the else block.


Switch Statement

The switch statement allows the selection of one code block to execute from several alternatives, based on the value of an expression. Here is the syntax:


            switch (expression) {
                case value1:
                  // code to be executed if expression matches value1
                  break;

                case value2:
                  // code to be executed if expression matches value2
                  break;

                default:
                  // code to be executed if none of the case statements match
              }
              
        
Example:

            #include <iostream>

            using namespace std;
            
            int main() {
                int day = 3;

                switch (day) {
                    case 1:
                        cout << "Monday";
                        break;

                    case 2:
                        cout << "Tuesday";
                        break;

                    case 3:
                        cout << "Wednesday";
                        break;

                    default:
                        cout << "Invalid day";
                }

                return 0;
            }
        

In this example, the variable day was assigned with a value. The program then evaluates the value assigned and executes the corresponding case. The `break` statement is used to exit the switch statement after each case.

If the user enters a value other than 1, 2, or 3, the program executes the `default` case, displaying an "Invalid day" message.


Compound Conditional Statements

Compound conditional statements, also known as logical operators, are an essential part of programming. They allow you to combine multiple conditions together to control the flow of your program. In this tutorial, we will explore the three main compound conditional operators in C++: the logical AND operator, the logical OR operator, and the logical NOT operator.

Logical AND Operator

The logical AND operator (&&) is used to combine two conditions. It returns true only if all the conditions are true; otherwise, it returns false. It is represented by the double ampersand ( `&&` ). Here is an example:


            #include <iostream>

            using namespace std;
            
            int main() {
                int age = 25;
                bool isStudent = true;

                if (age >= 18 && isStudent) {
                    cout << "You are eligible for a student discount.";
                } 
                
                else {
                    cout << "Sorry, you are not eligible for a student discount.";
                }

                return 0;
            }
        

In this example, the program checks if the age is greater than or equal to 18 and if the person is a student. If both conditions are true, it will print "You are eligible for a student discount." Otherwise, it will print "Sorry, you are not eligible for a student discount."


Logical OR Operator

The logical OR operator returns true if at least one of the conditions it connects is true. It is represented by the double vertical bar ( `||` ). Here is an example:


            #include <iostream>

            using namespace std;
            
            int main() {
                int temperature = 35;
                bool isRainy = false;

                if (temperature > 30 || isRainy) {
                    cout << "It's a hot day or it's raining.";
                }
                
                else {
                    cout << "It's a pleasant day.";
                }

                return 0;
            }
        

In this example, the program checks if the temperature is greater than 30 or if it is rainy. If either of the conditions is true, it will print "It's a hot day or it's raining." Otherwise, it will print "It's a pleasant day.".


Logical NOT Operator

The logical NOT operator negates the result of a condition. It is represented by an exclamation mark ( `!` ). Here's an example:


            #include <iostream>

            using namespace std;
            
            int main() {
                bool isLogged = false;

                if (!isLogged) {
                    cout << "You need to log in to access this feature.";
                } 
                
                else {
                    cout << "Welcome to the feature!";
                }

                return 0;
            }
        

In this example, the program checks if `isLogged` is false. The logical NOT operator negates the value, so if `isLogged` is false, the condition becomes true, and it will print "You need to log in to access this feature." Otherwise, it will print "Welcome to the feature!"

By using logical operators, you can make your C++ programs more dynamic and responsive to different scenarios. Experiment with them to enhance the functionality and decision-making capabilities of your programs.