Conditional statements
if
conditional statement
The if
conditional statement allows us to check if a given condition is fulfilled. We commonly use logical operators in conditional statements.
int x;
cin >> x;
if (x == 5)
cout << "The user entered the number 5.";
We use the modulo operator very often in conditional statements. In the example below, the condition checks if the x
variable is divisible by 2 (whether it is even).
int x;
cin >> x;
if (x % 2 == 0)
cout << "The user entered an even number.";
We can simplify the if
statement by using boolean conditions. We can just write if(x)
, and if x
is true
, the instruction will be executed. true
means any value other than 0.
else
and else if
conditional statements
else
is a statement that allows us to simplify our code by avoiding the need for another if
statement, replacing it with an alternative action when the original condition is false
.
int x;
cin >> x;
if (x > 0)
cout << "The user entered a positive number.";
else
cout << "The user entered a negative number.";
The code above doesn't handle the case where the user enters 0. To do that, we need else if
. It works similarly to else
, but allows us to specify an additional condition (like in an if
statement). We can use as many else if
instructions as we want, relating to a single if
.
int x;
cin >> x;
if (x > 0)
cout << "The user entered a positive number.";
else if (x < 0)
cout << "The user entered a negative number.";
else
cout << "The user entered 0.";
We use this instruction instead of writing multiple if
statements because if any of the conditions relating to a single if
statement have been met, the rest will not be considered.
break
and continue
keywords
The break
keyword is used to break the continuity of a loop, and continue
can skip particular iterations within it.
int x;
while (true) {
cin >> x;
if (x > 0)
break;
}
for (int y = 1; y <= 10; y++) {
if (y == 4 || y == 8)
continue;
cout << y << endl;
}
switch
conditional statement
switch
is an instruction that simplifies complex conditions by replacing multiple if
and else if
statements with a more concise structure.
int x;
cin >> x;
switch(x) {
case 0:
cout << "x equals 0";
break;
case 25:
cout << "x equals 25";
break;
case 50:
cout << "x equals 50";
break;
default:
cout << "x doesn't equal 0, 25, or 50";
}
An argument of the switch
statement can be an int
or a char
(it will convert characters to numbers based on the ASCII array).
If we want several cases
to be called upon in a given case
, we do not put the break
statement at the end of it (they will execute until a break
is encountered or the switch
instruction ends).
The ternary operator (?
)
The ternary operator allows us to create a simplifed one-line version of the if-else
statement. If the condition is true
, the instruction on the left side of the colon will execute, and if not - the one on the right side. Check the results in your editor.
cout << (7 > 4 ? 4: 7) << endl;
int a = 7, b = 10;
string x = a >= b ? "a >= b": "a <= b";
cout << x << endl;