Last Updated:

Conditional if and switch statements in C++

One of the main constructs of the algorithm is a branching process. It is implemented in the C++ language by two conditional statements: if and switch. Let's look at each of them.

Conditional if statement

To organize calculations depending on a condition, C++ provides a conditional if statement, which is generally written as follows:

if (condition) statement_1; else statement_2;

Here, a condition is a Boolean expression, variable, or constant.

The conditional statement works as follows: First, the values of the expression that is written as a condition are evaluatedIf it is true, operator_1 is executed. Otherwise (false) operator_2.

For example, to compare the values of variables a and b, you write the following part of the code:

Do not confuse the equality check sign == and the assignment operator =

If your task requires that multiple statements instead of one statement be executed depending on the value of a condition, you must enclose them in curly braces as a composite operator.

if (condition) { statement_1; operator_2; ... }

else { operator_1; operator_2; ... }

An alternate branch else in a conditional statement may not exist if it is not needed.

Switch variant operator

It is necessary in cases where, depending on the values of the variable, it is necessary to execute certain operators:

switch (expression)
{
case value_1: operators_1; break;
case value_2: statements_2; break;
case value_3: statements_3; break;
...
case value_n: operators_n; break;
default: statements; break;

}

The operator works as follows: The value of the expression is evaluated. Operators are then executed that are marked with a value that matches the value of the expression. That is, if the expression takes the value _1, then the operators_1 are executed, and so on. If the expression does not accept any of the values, the operators after the default word are executed.

The default branch may be missing, then the operator is as follows:

switch (expression)
{
case value_1: operators_1; break;
case value_2: statements_2; break;
case value_3: statements_3; break;
...
case value_n: operators_n; break;

}

The break statement is required to exit the switch statement. If it is not specified, the following operators from the list will be executed, even though the value with which they are marked does not match the value of the expression.

Some things may be incomprehensible, and so for the sake of clarity, let's look at an example of a simple task.

Task

The day of the week must be inscribed on the name of the week corresponding to the specified number D, provided that the month has 31 days and the 1st day is Monday.

To solve the problem, we will use the %, operation to calculate the remainder of the division of two numbers. The program will display the name of the day of the week depending on the number we set.

Here is a screenshot of the result of the code:

 

Logical operations (and, or, not)

In C++, there are logical operations that make it convenient to check several conditions. Let's get acquainted with each of them.

  • And (&&) is a logical conjunction (multiplication). Returns a truth only when the two simple conditions on either side of it are true. For example:

This program outputs true only if both simple conditions are true. The first condition turned out to be false! - The second is not verifiable.

 

Those who have studied logic know that conjunctions take on the logical meaning of 1 (truth) only when all expressions are true.

  • Or (||) - Logical disjunction (addition). An external condition will be true when at least one of the internal conditions is true. Let's look at the example of the previous code:

To return true, one of the conditions is true.

 

In mathematical logic, disjunction returns the value 0 only when zeros are "compared".

  • not (!) is a logical inversion (negation). Returns truth when the condition with the particle is not satisfied. The condition in the program below is met if the variable one is not equal to 1.

Of course, you can not use logical operations, but then the code will look noticeably rougher and uglier!