6- iterative constructs: while, do-while, for Flashcards

1
Q

increment

A

increase value by one, unary operator: ++

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

decrement

A

decrease value by one, unary operator: –

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Iterative Constructs

A

provide ability to execute the same code multiple times
1. while statement
2. do-while statement
3. for statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

while

A

syntax
while (expression)
body
semantics
if expression is true then execute body
body is either a single statement or a block
iteration: single execution of body
iterate until expression evaluates to false
example
while (n != 0) {
cin&raquo_space; n;
if (n > max) max = n;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

do-while

A

syntax
do
body
while (expression);
semantics
execute body
if expression is true then iterate again
iterate until expression evaluates to false
example
int max=0, n;
do {
cin&raquo_space; n;
if (n > max) max = n;
} while (n != 0);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

for

A

syntax
for(initStatement;
expression; postStatement)
body
semantics
execute initStatement
evaluate expression, if true: iterate
iteration:
execute body
execute postStatement
repeat expression evaluation
example
for (int i=0; i<20; ++i)
cout &laquo_space;“i is “ &laquo_space;i &laquo_space;endl;
loop variable - declared inside for
its scope is body of the loop
modifying loop variable inside body is poor style, use while instead

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

break

A

exits innermost loop
avoid break with loops as they make code less readable (makes regular loop exit unnecessary): first try to code loop without it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

continue

A

skip the remaining statements and start a new iteration (evaluate expression)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly