Chapter 4: Basic Control Flow

Chapter Goals


The if Statement

Syntax 4.1: if Statement
if (condition) statement
Example:
if (x >= 0) y = sqrt(x);
Purpose: Execute the statement if the condition is true.
Syntax 4.2: Block Statement
{ statement1
 
statement2
  ...
  statement
}
Example:
{ double length = sqrt(area);
  cout << area << "\n";
}
Purpose: Group several statements into a block that can be controlled by another statement.

The if Statement (area1.cpp)


The if/else Statement

Syntax 4.3: if Statement
if (condition) statement1 else statement2
Example:
if (x >= 0) y = sqrt(x); else cout << "Bad input\n";
Purpose: Execute the first statement if the condition is true, the second statement if the condition is false.

The if/else Statement (area2.cpp)

The selection operator

C++ has a selection operator of the form:

test ? value
1 : value2

The value of this expression is either value1 if the test passes or value2 if it fails.

   y = x >= 0 ? x : -x;
is equivalent to
   if (x >= 0) y = x;
else y = -x;

    Expression
Statement
  x >= 0 ? x : -x
if (x >= 0) y = x;
else y = -x;
  y = x
y = x;
  -b + sqrt(d)
-b + sqrt(d);

Relational Operators

Relational Operators (comparing strings)

ASCII Table

(American Standard Code for Information Interchange)

0-31 are control codes, for example "/n" (newline) has ASCII code 10.

  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:

This is Windows-1251 encoding table.


Input Validation

Input Validation (area3.cpp)


Simple Loops

You put $10,000 into a bank account that earns 5% interest per year. How many years does it take for the account balance to be double the original?
Syntax 4.4: while Statement
while (condition) statement
Example:
while (x >= 10) x = sqrt(x);
Purpose: Execute the statement while the condition remains true.

Simple Loops (doublinv.cpp)



Processing a Sequence of Inputs (Sentinels)

Processing a Sequence of Inputs (sentinel.cpp)

Processing a Sequence of Inputs (Causing the Stream to Fail)

Processing a Sequence of Inputs (maxtemp.cpp)


Using Boolean Variables