-
Notifications
You must be signed in to change notification settings - Fork 0
/
control_flow_lesson3.cpp
194 lines (155 loc) · 3.56 KB
/
control_flow_lesson3.cpp
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
188
189
190
191
192
193
/* C++ has several control flow options.
if-else statements
for loops
while loops
switch statements */
//Note-1
//Relational Operators
#include<iostream>
#include<string>
int main()
{
//instead of printing 0 and 1, create an array where
//0 = False, 1 = True
std::string TorF[] = {"False", "True"};
int a = 100;
int b = 33;
int c = 33;
//Print out the string values of each relational operation
std::cout<<"a < b is "<<TorF[a<b]; //Output: a<b is False
//Logical Operators
// The && operator = AND ; The || operator = OR ; ! = The 'Not' operator
//Note-2
//if statements
//For example:
if(guess < TARGET)
{
std::cout<<"Your guess is too low.\n";
}
else if(guess > TARGET)
{
std::cout<<"Your guess is too high.\n";
}
else
{
std::cout<<"Yay! You guessed correctly.\n";
}
return 0;
//Note-3
//switch statement
//For example:
#include <iostream>
int main()
{
float in1, in2;
char operation;
float answer;
std::cout<<"Enter two numbers:\n";
std::cin>>in1;
std::cin>>in2;
std::cout<<"Enter the operation '+','-':\n";
std::cin>>operation;
switch(operation)
{
case('+'): {
answer=in1 + in2;
break;
}
case('-'): {
answer=in1 - in2;
break;
}
default:
std::cout<<"Illegal operation";
}
std::cout<<in1<<operation<<in2<<" = "<<answer<<"\n";
return 0;
}
//Note-4:
//for loops
/*
for ( declaration : range ) statement;
for (initialization; condition; increase) statement; */
/* size_t: https://www.youtube.com/watch?v=w3brYyLx8S0 */
//for example:
#include <iostream>
int main()
{
for(int i=0; i< 10;i++) //!! It is important to where you put ++ or --. In front of i or after i.
{
std::cout<<"i = "<<i<<"\n";
}
return 0;
}
//Note-5:
//while loops & do..while loops
//for example:
#include <iostream>
int main()
{
int count = 0;
//This do..while loop will execute until count =5
do
{
std::cout<<"Count = "<<count<<"\n";
count++;
}while(count < 5);
int otherCount = 6;
//This do..while loop will execute once. Even though
//otherCount > 5
do
{
std::cout<<"othercount = "<<otherCount<<"\n";
otherCount++;
}while(otherCount < 5);
return 0;
}
//Ouput:
/*
Count = 0
Count = 1
Count = 2
Count = 3
Count = 4
othercount = 6 */
//example:
/*Goal: In the programming quiz, use a while loop to prompt
**the user to guess a target number.
**Tell the user if the guess is too high or too low.
**The user enters -1 or guesses the target number to end
**the program.
*/
#include <iostream>
#include<sstream>
int main()
{
int target = 55;
std::string userString;
int guess = -1;
while(guess != target )
{
std::cout<<"Guess a number between 0 and 100: ";
std::cin>>guess;
std::cout<<guess<<"\n";
if(guess > target)
std::cout<<"Your guess is too high\n";
else if(guess < target)
std::cout<<"Your guess is too low\n";
else
std::cout<<"You guessed the target!\n";
//Note I had to use double quotes around "q"
//because it is a string
if(guess == -1)
{
std::cout<<"good bye!";
break;
}
}
return 0;
}
//Note-6
//infinite loop
while(1)
{
std::cout<<"This while loop will run forever\n";
}