-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16-conditional_statements.cpp
More file actions
188 lines (148 loc) · 3.14 KB
/
16-conditional_statements.cpp
File metadata and controls
188 lines (148 loc) · 3.14 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
C++ supports the usual mathematical conditional operators.
- <
- <=
- >
- >=
- ==
- !=
C++ has the same conditional statements as C
- if
- else
- else if
- switch
*/
// If statement Syntax
#include <iostream>
using namespace std;
void main() {
if(condition) {
// code to be executed if condition is true
}
}
// else statement syntax
#include <iostream>
using namespace std;
void main() {
if(condition) {
// code to be executed if condition is true
} else {
// code to execute if condition is false
}
}
// nested if statement
#include <iostream>
using namespace std;
void main() {
if(condition1) {
if(condition2) {
//execute something here.
}
}
}
// else-if statement syntax
#include <iostream>
using namespace std;
void main() {
if(condition) {
// code to be executed if condition 1 is true
} else if(condition) {
// code to execute if condition 2 is true
} else {
// code to execute if both conditions are false.
}
}
//Ternary Operator
// the ternary operator is a short way of writing an conditional if-else statement
#include <iostream>
using namespace std;
void main() {
int time = 20;
string result = (time<20) ? "good day" : "good evening.";
cout << result;
}
//Switch statements
#include <iostream>
using namespace std;
void main() {
int day = 4;
switch(day) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
case 7:
cout << "Sunday";
break; // breaks after execution
default:
cout << "There's only 7 days in a week" //this executes in case of invalid inputs
}
}
//Loops
//Loops can execute a block of code until a condition is satisfied.
// While loop
//Syntax
while (condition) {
//code to be run.
}
//Example
#include <iostream>
using namespace std;
void main() {
int i = 0;
while(i<5) {
cout << i << "\n";
i++;
}
}
//do while loop
#include <iostream>
using namespace std;
void main() {
int i = 0;
do {
cout << i << "\n";
i++;
} while(i < 5)
}
// only difference here is the do-while loop checks the condition each time it executes.
//for loop
#include <iostream>
using namespace std;
void main() {
for(int i = 0; i < 5; i++) {
cout << i << "\n";
}
}
// first int i is set to 0.
// a condition is applied i < 5, when i = 5 or i > 5 the condition will return false and stop execution.
// i is incremented i++.
//break and continue statements
#include <iostream>
using namespace std;
void main() {
int i = 0;
while (i < 10) {
cout << i << "\n";
i++;
if (i == 4) {
break; // stops execution if i = 4
} else if(i == 8) {
continue; // continues if i = 8
}
}
}