Else Expression
In the previous task, we mentioned what the program should do if our statement is true, but what to do if we need to specify actions if the condition is also false. For such situations in C++ exists the else
statement.
The syntax is the following:
912345if (condition) {// code here will be executed if the condition is true} else {// code here will be executed if the condition is false}
Let’s define if the number is even (can be divided by 2 into two equal numbers) or not, using the else
statement:
91234567int num;cin >> num;if (num % 2 == 0) {cout << "The number is even!";} else {cout << "The number isn't even!";}
The code accepts a number from the user. If the number by division by 2 gives 0 as the remainder (for example 6/2 = 3 the remainder is 0) then the condition is true and the user printed an even number, otherwise, the number is not even.
To pick up a draggable item, press the space bar.
While dragging, use the arrow keys to move the item.
Press space again to drop the item in its new position, or press escape to cancel.
Tudo estava claro?
Obrigado pelo seu feedback!
Seção 3. Capítulo 4
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo