-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTernaryoperatorpractice.cpp
More file actions
47 lines (39 loc) · 1.16 KB
/
Ternaryoperatorpractice.cpp
File metadata and controls
47 lines (39 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
int main() {
// ternary operator ?: = replacement to an if/else statement
// condition ? expression 1 : expression 2;
int grade;
std::cout << "Enter your grade number: ";
std::cin >> grade;
grade >= 60 ? std::cout << "You pass!\n" : std::cout << "You fail!\n" ;
// this is the same as:
/* if(grade >= 60){
std::cout << "You pass!" << '\n';
} else {
std::cout << "You fail!" << '\n';
}
*/
std::cout << "Examples: \n";
int number;
std::cout << "Enter a number to see if its ODD or EVEN: ";
std::cin >> number;
number % 2 == 1 ? std::cout << "ODD\n" : std::cout << "EVEN\n";
// this is the same as:
/* if(number % 2 == 1){
std::cout << "ODD" << '\n';
} else {
std::cout << "EVEN" << '\n';
}
*/
bool hungry = false;
std::cout << "Hungry bool value set to false so\n";
std::cout << (hungry ? "You are hungry" : "You are not hungry") << '\n';
// this is the same as:
/* if(hungry){
std::cout << "You are hungry" << '\n';
} else {
std::cout << "You are not hungry" << '\n';
}
*/
return 0;
}