Nesting Loops and Ifs
3. Nested IF
Answer:
Use an if
statement.
Nested IF
The flowchart is now a refinement of the first one. It
explicitly shows three sums, and how each sum is handled. The loop body shows that the current value of
count
is always added to sumAll
. An if
statement determines if count
is
added to sumEven
or sumOdd
.
An if
statement that is part of a loop body is a nested if. (Another type of nested if is when an if
statement is part of another if
statement.)
Play with the flowchart and see if it works. Pick some small integer, perhaps N==3, and follow the flowchart by hand. Then try an unexpected value, like N==1 and see if the logic works.
Question 3:
Does the flowchart work for N==1?