Short Forms
It's pretty easy to get confused by these expressions. To make code more readable you can use the short version of these statements or also called ternary operator, since it consists of three operands. The syntax is the following:
pythonvariable = (condition) ? expression#1 : expression#2;
First expression will be executed if the condition returns true, second - if the condiction returns false. Let’s look at the example:
99
1
2
3
4
5
6
7
8
9
10
11
// Define variables
int x = 3;
int y = 7;
// Check conditions
string result1 = (x < 5) ? "x is less than 5." : "x is greater than 5.";
string result2 = (y < 5) ? "y is less than 5." : "y is greater than 5.";
// Print results
cout << result1 << endl;
cout << result2 << endl;
1234567891011// Define variables int x = 3; int y = 7; // Check conditions string result1 = (x < 5) ? "x is less than 5." : "x is greater than 5."; string result2 = (y < 5) ? "y is less than 5." : "y is greater than 5."; // Print results cout << result1 << endl; cout << result2 << endl;
In the first line where we check conditions the statement returns true and we assign to the variable first expression. In the second line the statement returns false and we assign second expression.
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.
Was alles duidelijk?
Bedankt voor je feedback!
Sectie 3. Hoofdstuk 6
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.