-
Notifications
You must be signed in to change notification settings - Fork 0
/
if.c++
64 lines (51 loc) · 1.12 KB
/
if.c++
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <string>
using namespace std;
int main(){
if (10 > 8) {
cout << "10 is greater than 8" << endl;
}
else {
cout << "10 is not greater than 8" << endl;
}
//using variables
int x = 20;
int y = 18;
if (x > y) {
cout << "x is greater than y" << endl;
}
else {
cout << "x is not greater than y" << endl;
}
// else if statement
string name;
string name2;
string name3;
cin >> name;
cin >> name2;
cin >> name3;
if (name == "John"){
cout << "Hello " << name << endl;
} else if (name2 == "John"){
cout << "Hello " << name2 << endl;
} else if (name3 == "John"){
cout << "Hello " << name3 << endl;
} else {
cout << "Hello " << name << endl;
}
// short hand if else
// Ternary Operator
// variable = (condition) ? expressionTrue : expressionFalse;
//normal if statement
int time = 5;
if (time < 12){
cout << "Good morning" << endl;
}
else {
cout << "Good evening" << endl;
}
// Ternary Operator
int time2 = 16;
string result = (time2 < 12) ? "Good morning" : "Good evening";
cout << result << endl;
}