if | ( условие) | оператор; |
Операции за числови
сравнения
> | по-голямо |
>= | по-голямо или равно |
< | по-малко |
<= | по-малко или равно |
== | равно |
!= | различно |
* ASCII таблица
32: 33:! 34:" 35:# 36:$
37:% 38:& 39:' 40:( 41:)
42:* 43:+ 44:, 45:- 46:.
47:/ 48:0 49:1 50:2 51:3
52:4 53:5 54:6 55:7 56:8
57:9 58:: 59:; 60:< 61:=
62:> 63:? 64:@ 65:A 66:B
67:C 68:D 69:E 70:F 71:G
72:H 73:I 74:J 75:K 76:L
77:M 78:N 79:O 80:P 81:Q
82:R 83:S 84:T 85:U 86:V
87:W 88:X 89:Y 90:Z 91:[
92:\ 93:] 94:^ 95:_ 96:`
97:a 98:b 99:c 100:d 101:e
102:f 103:g 104:h 105:i 106:j 107:k 108:l 109:m 110:n
111:o
112:p 113:q 114:r 115:s 116:t 117:u 118:v 119:w 120:x
121:y
122:z 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:Б 194:В 195:Г 196:Д 197:Е 198:Ж 199:З 200:И
201:Й
202:К 203:Л 204:М 205:Н 206:О 207:П 208:Р 209:С 210:Т
211:У
212:Ф 213:Х 214:Ц 215:Ч 216:Ш 217:Щ 218:Ъ 219:Ы 220:Ь
221:Э
222:Ю 223:Я 224:а 225:б 226:в 227:г 228:д 229:е 230:ж
231:з
232:и 233:й 234:к 235:л 236:м 237:н 238:о 239:п 240:р
241:с
242:т 243:у 244:ф 245:х 246:ц 247:ч 248:ш 249:щ 250:ъ
251:ы
252:ь 253:э 254:ю 255:
Проверка за валидност
на входните данни
* Проверка са положително число във входния поток
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{ double area;
cout <<
"Please enter the area of a square: ";
cin >> area;
if (area <
0)
{ cout
<< "Error: area cannot be negative\n";
return 1;
}
/* now we know
that area is >= 0 */
cout <<
"The side length of the square is " << sqrt(area) << "\n";
return 0;
}
* Проверка и на състоянието на входния поток
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{ double area;
cout <<
"Please enter the area of a square: ";
cin >> area;
if (cin.fail())
{ cout
<< "Error: bad input\n";
return 1;
}
/* now we know
that area is valid */
if (area <
0)
{ cout
<< "Error: area cannot be negative\n";
return 1;
}
/* now we know
that area is >= 0 */
cout << "The side length of the square is " << sqrt(area) << "\n";
return 0;
}
Оператор if/else
#include <iostream>
using namespace std;
int main()
{
double a, b, c;
cout << "Enter
the coeficients a, b and c:";
cin >> a >> b >> c;
double det = b*b -
4*a*c;
if (det >= 0)
{
double x1 = (-b+sqrt(det))/(2*a);
double x2 = (-b-sqrt(det))/(2*a);
cout << "The
roots are: " << x1 << " and " << x2;
}
else cout <<
"The equation has no real roots."
return 0;
}
if | (условие) | оператор1; | else | оператор2; |
double value = 0;
if (name ==
"penn")
value = n * 0.01;
else if (name
== "nick")
value = n * 0.05;
else if (name
== "dime")
value = n * 0.10;
else if (name
== "quar")
value = n * 0.25;
else
cout <<
name << " is not a valid coin name\n";
cout <<
"Value = " << value << "\n";
return 0;
}
А
без операторите else?
if
(name == "penn") value = n * 0.01;
if (name == "nick")
value = n * 0.05;
if (name == "dime")
value = n * 0.10;
if (name == "quar")
value = n * 0.25;
Вложени условни оператори
Задача за определяне на годишен данък
Ако облагаемата сума е над | но не повече от | данъкът е | върху сумата над |
0 | 21400 | 15% | 0 |
21450 | 51900 | 3217.50 + 28% | 21450 |
51900 | 11743.50 + 31% | 51900 |
Ако облагаемата сума е над | но не повече от | данъкът е | върху сумата над |
0 | 35800 | 15% | 0 |
35800 | 51900 | 5370.00 + 28% | 35800 |
86500 | 19566.00 + 31% | 86500 |
cout <<
"Please enter your income: ";
cin >> income;
cout <<
"Please enter s for single, m for married: ";
string marital_status;
cin >> marital_status;
if (marital_status
== "s")
{ if
(income <= 21450.00)
tax = 0.15 * income;
else if (income <= 51900.00)
tax = 3217.50 + 0.28 * (income - 21450.00);
else
tax = 11743.50 + 0.31 * (income - 51900.00);
}
else
{ if
(income <= 35800.00)
tax = 0.15 * income;
else if (income <= 86500.00)
tax = 5370.00 + 0.28 * (income - 35800.00);
else
tax = 19566.00 + 0.31 * (income - 86500.00);
}
cout <<
"The tax is $" << tax << "\n";
return 0;
}