Nested IF

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

Use an if statement.

Nested IF

The following flowchart is 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.

flowchart of program

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?