3.1. Conditionals¶
Conditional statements in Python and C++ are very similar.
3.1.1. Simple if¶
In Python, we write a simple if statement in the following way:
if condition:
statement1
statement2
...
In C++ this same pattern is simply written as:
if (condition) {
statement1
statement2
...
}
Once again you can see that in C++ the curly braces define a block
rather than indentation. In C++ the parenthesis around the condition
are required because if
is technically a function that
evaluates to true
or false
.
3.1.2. if else¶
The if-else statement in Python looks like this:
if condition:
statement1
statement2
...
else:
statement1
statement2
...
In C++ this is written as:
if (condition) {
statement1
statement2
...
} else {
statement1
statement2
...
}
3.1.3. elif¶
C++ does not have an elif pattern like Python. In C++ you can get the functionality of an elif statement by nesting if and else. Here is a simple example in both Python and C++.
In C++ we have a couple of ways to write this
We can get closer to the look of the elif statement in C++ by taking advantage of the C++ rule that a single statement does not need to be enclosed in curly braces. Since the if is the only statement used in each else we can get away with the following.
3.1.4. switch¶
C++ also supports a switch
statement that acts something like the
elif statement of Python under certain conditions because the statement
takes cases and checks the validity of the case against the code.
It uses cases instead of conditions and the case must be based on
integers or a user-defined data type called an enumerated constant.
To write the grade program using a switch statement we would use the following:
Frankly, the switch
statement is not used very often.
It is not as powerful as the else if
model
because the switch variable can only be compared for equality with an
integer or something called an enumerated constant.
Second it is very easy to forget to put
in the break
statement. Note above how cases 10 and 9 are coded together.
If the break statement is left out then then
the next alternative will be automatically executed.
For example if the
grade was 95 and the break
was omitted from the case 9:
alternative then the program would print out both (A and B.)
So, you might want to just avoid it and use if…
3.1.4.1. Check Yourself¶
-
Q-53: When indicating the condition for a C++ if statement, what symbols are used?
- { }
- No. Try again.
- [ ]
- No. Try again.
- ( )
- Right!
- Any set of matching braces may be used.
- No. Try again.
- none of the above
- One of the above is indeed correct.
-
Q-54: When using a switch, what prevents all the cases from passing as correct?
- Ending statements with ;
- No. This is always needed.
- Using break
- Good job!
- Enclosing each cases with { }
- No. Try again.
- Setting a default case
- No. This is a good idea, but it will not help.
-
Q-55: What symbol is used to indicate the “and” in C++ such as in a compound condition?
- ||
- No, this means "or".
- and
- No, this is Python.
- !
- No, this means "not"
- &&
- Very good!