Chapter 4.2: Flowcharts
Introduction
Learning Outcomes
Recognize the basic flowchart symbols.
Order the steps with arrows.
Show decisions and loops.
Turn a flowchart into pseudocode and a program.
Basics
What Is a Flowchart?
- A diagram of the order in which an algorithm's steps run.
- It lets us discuss complex logic apart from code.
- Symbols show the kind of each step; arrows show the direction of execution.
Symbols
Basic Symbols
The shape tells you the kind of step; inside, write only a short phrase for that step.
Symbols
Symbol and Role
| Symbol | Role | Example |
|---|---|---|
| Oval | Start or end | Start |
| Rectangle | Process (calculation, assignment) | sum = a + b |
| Diamond | Decision (condition) | a > b ? |
| Parallelogram | Input / output | read a |
| Arrow | Direction of execution | go to the next step |
Symbols
More Symbols
| Symbol | Role | Example |
|---|---|---|
| Off-page connector | The chart continues on another page; label it with page and point. | 2-A |
| Predefined process | Calls a subprocess drawn separately. | Compute average |
| Preparation | Gives a loop counter's start and limit in one symbol. | i = 1 → n |
| Document | Printed output or a report. | Grade sheet |
| Data store | Data kept on disk or in a database. | Student records |
| Annotation | Attaches a note to a symbol with a dashed line; not part of the flow. | grade 0–100 |
These symbols come from ISO 5807 and appear in common drawing tools. Five basic symbols are enough for a small algorithm; the extra ones help when a chart grows across pages or must show external media such as files and reports.
Design · Step by step
Building a Flowchart: The Larger of Two Numbers
Problem: print the larger of two numbers. Input a and b (parallelogram: read), output the larger one (parallelogram: print).
Add Start and End; order the steps top to bottom: read first, print last.
Which number to print depends on a question: add the decision a > b ? and split the output into two boxes.
Arrows give the direction of execution. Label the decision exits Yes/No and merge the paths at a connector.
Trace an example: a = 4, b = 9 → 4 > 9 is false → No path → "print b" → output 9. The orange path is the one traced.
Design
Readability Rules
✗ Crossing arrows, unlabeled decision, three steps in one box
✓ Top to bottom, Yes/No labels, one step per box
- Flow should run top to bottom and left to right wherever possible.
- Decision exits need meaningful labels such as Yes/No; keep crossing arrows to a minimum.
- Put one basic step in each symbol; split large charts into subprocesses.
Design
Flowchart or Pseudocode?
| Flowchart | Pseudocode | |
|---|---|---|
| Form | Symbols and arrows | Short indented lines |
| Strength | Branches and loops are visible at a glance | Quick to write, very close to code |
| Weakness | Outgrows the page; tedious to change | Structure depends on indentation |
| Best for | Learning, presentations, business processes | Long algorithms, right before coding |
Both describe the same algorithm, and each translates line by line into the other. Pseudocode has no fixed standard; clarity is what counts.
read x if x < 0 then x = −x print x
Design
The Three Structures of Structured Programming
- Böhm and Jacopini (1966): every algorithm can be written with just sequence, selection and repetition.
- Each structure has one entry and one exit, so a structure can replace any box inside another one (nesting).
- Arrows that jump to arbitrary points (spaghetti flow) make charts hard to read and test. In Java,
gotois a reserved word but cannot be used.
Design
Flowchart Drawing Tools
Fastest for a first draft; easy to erase and redraw. Exam charts are drawn by hand too.
The Flowchart group under Insert → Shapes; connector arrows follow when a box moves.
Free and browser-based; ready-made flowchart shapes and export to PNG, SVG and PDF.
A commercial tool for business process and workflow diagrams.
Runs the chart, shows variables step by step and converts it to code in many languages, including Java.
You describe the chart in text and the tool draws it; common in documents and code repositories.
Whatever the tool, the meaning of the symbols and the readability rules stay the same.
Kinds of Flow
Sequential Flow
Start read a read b sum = a + b print sum End
- Each step runs after the previous one finishes.
- No branches or repetition: one entry, one exit.
- Arrows always point down.
- Order matters:
sumcannot be computed before a and b are read.
Kinds of Flow · Run the chart
Sequential Flow Step by Step (a = 4, b = 7)
Execution begins at the Start box. No variables exist yet.
Input box: the user enters 4 → a = 4.
The arrow leads to the next box: read b = 7.
Process box: sum = 4 + 7 = 11.
Output box: 11 is printed.
We reach End. Every box ran exactly once: the mark of sequential flow.
Start read a read b sum = a + b print sum End
Kinds of Flow
Conditional Flow
Start read grade grade >= 60 ? Yes → print "Pass" No → print "Fail" End
- Two paths leave the decision; each run follows only one of them.
- The paths merge again at the connector (●) below.
- Test the boundary separately: with grade = 60,
>=gives “Pass” but>gives “Fail”.
Kinds of Flow · Run the chart
Conditional Flow Step by Step (grade = 72, then 45)
Run 1 (grade = 72): execution begins at Start.
The user enters 72 → grade = 72.
Decision: 72 >= 60 → true. Flow continues along the Yes arrow.
"Pass" is printed. The box on the No branch never runs this time.
The two paths merge at the connector and reach End. Now run again with grade = 45 to see the other branch.
Run 2 (grade = 45): execution begins at Start.
The user enters 45 → grade = 45.
Decision: 45 >= 60 → false. Flow continues along the No arrow.
"Fail" is printed. The box on the Yes branch never runs this time.
The two paths merge at the connector and reach End. Two inputs exercised both branches.
Start read grade grade >= 60 ? Yes → print "Pass" No → print "Fail" End
Kinds of Flow · Run the chart
Largest of Three Numbers Step by Step
Run 1 (4, 9, 7): Start.
Read three numbers: a = 4, b = 9, c = 7.
First candidate for the largest: max = a = 4.
Decision: 9 > 4 → true → Yes.
max = b = 9. The path merges below.
Decision: 7 > 9 → false → No.
9 is printed.
End. Now try an input where c is the largest.
Run 2 (3, 2, 8): Start.
Read three numbers: a = 3, b = 2, c = 8.
First candidate for the largest: max = a = 3.
Decision: 2 > 3 → false → No; max stays.
Decision: 8 > 3 → true → Yes.
max = c = 8.
8 is printed.
End. Each decision is asked only once; the method stays the same with more numbers.
Start read a, b, c max = a b > max ? Yes → max = b c > max ? Yes → max = c print max End
Kinds of Flow
Multi-Way Decision: A Decision Chain
- With more than two options, decisions form a chain: each one sits on the No branch of the previous one.
- The branch of the first true condition runs; later decisions are never asked. For s = 18 three decisions are asked: 18 < 0 and 18 < 15 are false, 18 < 25 is true → Mild.
- Each decision checks only the upper bound; the lower bound is already known (when s < 15 is asked, s ≥ 0 is certain).
- Order matters: if s < 25 came first, s = −5 would also print “Mild”.
- n options need n − 1 decisions. The Java equivalent is an
if … else if … elsechain.
Kinds of Flow
Compound Conditions: AND / OR
- A compound condition can also go in a single decision box: age >= 18 AND has license ?. Expand it into two decisions when the chart needs detail.
- AND: if the first decision is No, the second is never asked; the result is already No. OR: if the first is Yes, the second is unnecessary. Java's
&&and||work the same way: short-circuit evaluation. - To test, exercise both exits of every decision: in the AND example, (20, yes), (20, no) and (16, —) run three different paths.
Kinds of Flow · Run the chart
Leap Year Step by Step (2024, 1900, 2000)
The leap-year rule: years divisible by 4 are leap years, except those divisible by 100, unless also divisible by 400. A chain of three decisions. Run 1: 2024.
Read year = 2024.
2024 % 400 = 24 → false → No.
2024 % 100 = 24 → false → No.
2024 % 4 = 0 → true → Yes.
“Leap” is printed. 2024 is a leap year (February has 29 days).
End. Run 2: 1900.
Read year = 1900.
1900 % 400 = 300 → false → No.
1900 % 100 = 0 → true → Yes. The third decision is never asked.
“Common” is printed. 1900 is divisible by 4 but is not a leap year; a chart that checked only % 4 would get this wrong.
End. Run 3: 2000.
Read year = 2000.
2000 % 400 = 0 → true → Yes. The other two decisions are skipped.
“Leap” is printed. 2000 is divisible by 100, but also by 400, so it is a leap year.
End. Three inputs exercised three of the four exits; a year like 2023 tests the last No branch. The order of the decisions handles the rule's exceptions correctly.
Start read year year % 400 == 0 ? Yes → print "Leap" year % 100 == 0 ? Yes → print "Common" year % 4 == 0 ? Yes → print "Leap" No → print "Common" End
Kinds of Flow
Repetition
Start i = 1 i <= 5 ? Yes → print i i = i + 1 back to the decision No → End
- The arrow going back (repeat) forms the loop; the condition is checked before the body (a pre-test loop), and the loop ends when it is false.
- The repeat path must contain a step that changes the condition (i = i + 1).
Kinds of Flow · Run the chart
Repetition Step by Step
Start.
The counter gets its first value: i = 1.
Decision: 1 <= 5 → true → Yes, enter the loop body.
1 is printed.
i = 1 + 1 = 2. The back arrow returns to the decision.
Decision: 2 <= 5 → true → Yes, enter the loop body.
2 is printed.
i = 2 + 1 = 3. The back arrow returns to the decision.
Decision: 3 <= 5 → true → Yes, enter the loop body.
3 is printed.
i = 3 + 1 = 4. The back arrow returns to the decision.
Decision: 4 <= 5 → true → Yes, enter the loop body.
4 is printed.
i = 4 + 1 = 5. The back arrow returns to the decision.
Decision: 5 <= 5 → true → Yes, enter the loop body.
5 is printed.
i = 5 + 1 = 6. The back arrow returns to the decision.
Decision: 6 <= 5 → false → No, leave the loop.
End. The body ran 5 times; the condition was tested 6 times (the last test, with i = 6, was false).
Start i = 1 i <= 5 ? Yes → print i i = i + 1 back to the decision No → End
Loops
Counters and Accumulators
- A counter tracks how many repetitions have run: it grows by 1 each pass.
- An accumulator holds a partial result: each pass adds a new value.
- Pick the starting value to suit the operation: 0 for a sum, 1 for a product.
Pass-by-pass values for inputs 4, 7, 2:
| Step | x | count | sum |
|---|---|---|---|
| start | — | 0 | 0 |
| pass 1 | 4 | 1 | 4 |
| pass 2 | 7 | 2 | 11 |
| pass 3 | 2 | 3 | 13 |
Loops · Run the chart
Counter and Accumulator Step by Step (4, 7, 2)
Start. Three numbers will be read and added; the inputs are 4, 7, 2.
Counter and accumulator start at 0.
Decision: 0 < 3 → true → Yes.
Read x = 4.
Accumulator: sum = 0 + 4 = 4.
Counter: count = 0 + 1 = 1. Go back.
Decision: 1 < 3 → true → Yes.
Read x = 7.
Accumulator: sum = 4 + 7 = 11.
Counter: count = 1 + 1 = 2. Go back.
Decision: 2 < 3 → true → Yes.
Read x = 2.
Accumulator: sum = 11 + 2 = 13.
Counter: count = 2 + 1 = 3. Go back.
Decision: 3 < 3 → false → No, the loop ends.
Print the sum: 13.
End. The counter recorded how many times the loop ran; the accumulator held the result.
Start count = 0, sum = 0 count < 3 ? Yes → read x sum = sum + x count = count + 1 back to the decision No → print sum End
Loops
Example: Sum from 1 to n
Start read n sum = 0, i = 1 i <= n ? Yes → sum = sum + i i = i + 1 back to the decision No → print sum End
- i is the counter, sum the accumulator.
- For n = 0 the condition is false on the first test, and 0 is printed.
Loops · Run the chart
Sum from 1 to n Step by Step (n = 4)
Start. Input: n = 4. Expected result 1 + 2 + 3 + 4 = 10.
Read n = 4.
Accumulator sum = 0, counter i = 1.
Decision: 1 <= 4 → true → Yes.
sum = 0 + 1 = 1.
i = 2; back to the decision.
Decision: 2 <= 4 → true → Yes.
sum = 1 + 2 = 3.
i = 3; back to the decision.
Decision: 3 <= 4 → true → Yes.
sum = 3 + 3 = 6.
i = 4; back to the decision.
Decision: 4 <= 4 → true → Yes.
sum = 6 + 4 = 10.
i = 5; back to the decision.
Decision: 5 <= 4 → false → No, leave the loop.
10 is printed.
End. The body ran n = 4 times; the formula confirms it: n(n+1)/2 = 10.
Start read n sum = 0, i = 1 i <= n ? Yes → sum = sum + i i = i + 1 back to the decision No → print sum End
Loops
Counter-Controlled and Sentinel Loops
| Counter-controlled | Sentinel-controlled | |
|---|---|---|
| Number of repetitions | Known in advance (n times) | Unknown; stops when a special value arrives |
| Decision | i <= n ? | x == −1 ? (sentinel yet?) |
| Example | Read the grades of 30 students in a class | Items scanned at a checkout, until the “done” key |
| Usually in Java | for | while |
- Choose a sentinel that cannot occur in real data: if grades are 0–100, −1 works and 0 does not.
- The sentinel must not join the data: ask the decision before adding and counting; the back arrow returns to the read box.
- If the first input is the sentinel, the body never runs and the counter stays 0; before dividing for an average, ask count > 0 ?.
Loops · Run the chart
Sentinel Loop Step by Step (80, 64, 90, −1)
Start. The user enters grades and types −1 when done. Inputs: 80, 64, 90, −1.
Accumulator and counter start at 0.
Read x = 80.
80 == −1 → false → No: this is a real grade.
sum = 0 + 80 = 80.
count = 1.
Read x = 64. The back arrow returns to the read box.
64 == −1 → false → No: this is a real grade.
sum = 80 + 64 = 144.
count = 2.
Read x = 90. The back arrow returns to the read box.
90 == −1 → false → No: this is a real grade.
sum = 144 + 90 = 234.
count = 3.
Read x = −1.
−1 == −1 → true → Yes: the sentinel arrived, so the loop ends. −1 was not added to the sum or the count.
avg = 234 / 3 = 78.
The average is printed.
End. The body ran 3 times, the decision 4 times. The number of grades was unknown in advance; the data stopped the loop.
Start sum = 0, count = 0 read x x == -1 ? Yes → go to line 7 sum = sum + x count = count + 1; back to line 3 avg = sum / count print avg End
Loops
Nested Structures
- A decision's branch can contain another decision.
- A loop can contain a second loop: on every pass of the outer loop, the inner loop runs from start to finish.
- The entry and exit of each structure should be visually distinct.
3 × 3 = 9 cells: the inner body runs 9 times. Orange row: the three passes of j while i = 2.
Loops · Run the chart
Nested Loop Step by Step (i = 1)
Start. Outer loop i = 1, 2; inner loop j = 1, 2. Each row prints i × j.
i = 1: the outer counter starts.
Outer decision: 1 <= 2 → true → enter the inner loop.
j = 1: the inner counter is reset to 1 on every outer pass.
Inner decision: 1 <= 2 → true
1 × 1 = 1 is printed.
j = 2; back to the inner decision.
Inner decision: 2 <= 2 → true
1 × 2 = 2 is printed.
j = 3; back to the inner decision.
Inner decision: 3 <= 2 → false → No, the inner loop ends.
Row done: println() ends the line (the cursor moves down). Then i = 2; back to the outer decision.
Start i = 1 i <= 2 ? (No → End) j = 1 j <= 2 ? (No → i = i + 1) print i × j j = j + 1, back to inner decision end line (println), i = i + 1, back to outer decision End
Loops · Run the chart
Nested Loop Step by Step (cont.: i = 2)
Outer decision: 2 <= 2 → true → enter the inner loop.
j = 1: the inner counter is reset to 1 on every outer pass.
Inner decision: 1 <= 2 → true
2 × 1 = 2 is printed.
j = 2; back to the inner decision.
Inner decision: 2 <= 2 → true
2 × 2 = 4 is printed.
j = 3; back to the inner decision.
Inner decision: 3 <= 2 → false → No, the inner loop ends.
Row done: println() ends the line. Then i = 3; back to the outer decision.
Outer decision: 3 <= 2 → false → No, go to End.
End. The inner body ran 2 × 2 = 4 times; the output is a small multiplication table.
Start i = 1 i <= 2 ? (No → End) j = 1 j <= 2 ? (No → i = i + 1) print i × j j = j + 1, back to inner decision end line (println), i = i + 1, back to outer decision End
Loops · Run the chart
Number Guessing Game Step by Step (secret = 37)
Start. The secret number (normally random) is 37; the player guesses 50, 25, 37.
The secret is set and the try counter is 0.
The player enters 50.
tries = 1.
Decision: 50 == 37 → false → No, give a hint.
Decision: 50 < 37 → false.
"Lower" is printed; the arrow returns to the read box.
The player enters 25.
tries = 2.
Decision: 25 == 37 → false → No, give a hint.
Decision: 25 < 37 → true.
"Higher" is printed; the arrow returns to the read box.
The player enters 37.
tries = 3.
Decision: 37 == 37 → true → Yes.
"Correct!" is printed with the try count, 3.
End. The loop ended when its condition (guess == secret) became true; the number of passes is not known in advance.
Start secret = 37, tries = 0 read guess tries = tries + 1 guess == secret ? Yes → print "Got it (try " tries ")", End guess < secret ? Yes → print "Higher" No → print "Lower" back to line 3 (read) End
Loops · Run the chart
do-while Flowchart Step by Step (inputs: 7, 0, 3)
Start. The inputs arrive in order: 7, 0, 3. The body (read) runs first and the condition is checked at the end: a post-test loop.
Body first: read num = 7. The condition has not been checked yet.
Decision: 1 ≤ 7 ≤ 5 ? Since 7 > 5, false → No.
"Invalid" is printed.
The arrow returns to the read box: read num = 0.
Decision: 1 ≤ 0 ≤ 5 ? Since 0 < 1, false → No.
"Invalid" is printed again.
Read again: num = 3.
Decision: 1 ≤ 3 ≤ 5 → true → Yes.
The valid number is printed: 3.
End. The body ran 3 times. Because the condition is checked after the body, the body runs at least once. In Java: do { … } while (num < 1 || num > 5);
Start read num 1 <= num <= 5 ? No → print "Invalid", back to read Yes → print num End
Loops
Flowchart of a for Loop
for (i = 1; i <= n; i++) {
sum = sum + i;
}- Initialization (i = 1): runs once, on entering the loop.
- Condition (i <= n): checked before every pass; for is a pre-test loop.
- Body: runs if the condition is true.
- Update (i++): runs after the body; the arrow returns to the condition.
The chart is the same as a while chart; for just gathers parts 1, 2 and 4 into one line. Some texts show 1, 2 and 4 in a single preparation symbol (a hexagon, “i = 1 → n”).
Loops
Early Exit and Flags
Find the first negative number: a = [4, 7, −2, 5, −8]
- Once the target is found, the remaining passes are wasted. A second arrow from inside the body to outside the loop is an early exit; in Java,
break. - In the example, −2 is found at i = 3 and printed; 5 and −8 are never examined. If there is no negative number, the loop exits via its condition and prints “None”.
- The loop now has two exits. If you want a single exit, use a flag: start with found = false, set it to true on a match, and make the condition i <= n AND not found ?. After the loop, check the flag to print the result.
- Skip (
continue) does not leave the loop: it skips the rest of the pass and goes to the update and the condition.
Translating to Code
From Flowchart to Java Code
public static void main(String[] args) { int n = 3; int sum = 0, i = 1; while (i <= n) { sum = sum + i; i = i + 1; } System.out.println(sum); }
- Start / End → the start and end of the method.
- Input → a variable (here a fixed value).
- Process → an assignment statement.
- Decision + back arrow → the
whilecondition and body. - Output →
println.
Translating to Code · Symbol ↔ line of code
From Flowchart to Java Code Step by Step (n = 3)
The Start symbol is the start of the method: main opens.
Input symbol → int n = 3; (the input is fixed).
Process symbol → the initial values of two variables.
Decision symbol → while (i <= n): 1 <= 3 is true.
The Yes path is the loop body: sum = 0 + 1 = 1.
i = i + 1 → i = 2. The next arrow goes back to the decision.
The back arrow is the jump from the end of the loop block to the condition: 2 <= 3 is true.
sum = 1 + 2 = 3.
i = 3.
3 <= 3 is true.
sum = 3 + 3 = 6.
i = 4.
4 <= 3 is false → the No path leaves the loop block.
Output symbol → println(sum): 6 is printed.
End → the method closes. Every symbol has a code counterpart.
public static void main(String[] args) { int n = 3; int sum = 0, i = 1; while (i <= n) { sum = sum + i; i = i + 1; } System.out.println(sum); }
Translating to Code
Modular Charts with Subprocesses
- The predefined process symbol calls a separately drawn chart; when the subprocess ends, flow resumes in the main chart at the same point.
- The subchart starts with its name and parameters instead of Start, and ends with Return instead of End. For a = 70, b = 85, c = 91: t = 246, returned value 82.
- One subprocess can be called from many places but is drawn once. In Java each subprocess becomes a method:
static int average(int x, int y, int z) { return (x + y + z) / 3; }
Verification
Testing a Flowchart
Normal input grade = 72: the Yes path runs.
The other branch with grade = 45: the No path runs. Now every box has run at least once.
Boundary value grade = 60: 60 >= 60 is true → "Pass". Had > been written, this test would catch it.
- Follow the arrows step by step with a normal input.
- Choose test values that exercise every exit of each decision.
- Test boundary values (like 60) separately.
- Try cases where the loop runs zero times, once and many times.
- Look for paths that never return or never reach End.
Verification
Hand Tracing with a Trace Table: Euclid (48, 18)
| Step | a | b | k | b != 0 ? |
|---|---|---|---|---|
| start | 48 | 18 | — | 18 != 0 true |
| pass 1 | 18 | 12 | 12 | 12 != 0 true |
| pass 2 | 12 | 6 | 6 | 6 != 0 true |
| pass 3 | 6 | 0 | 0 | 0 != 0 false |
- Each variable is a column and each pass a row; the decision's result is recorded too. Result: 6 is printed, GCD(48, 18) = 6.
- The table shows why the order in the chart matters: if a = b and b = k were swapped, a would take b's new value and the result would be wrong.
- The loop ran 3 passes and the decision was asked 4 times: as always, the decision runs once more than the body.
Verification
Common Mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Unlabeled decision path | Unclear which path is which | Write Yes/No |
| Counter never updated | Infinite loop | Add a counter step |
| Unconnected symbol | Flow breaks | Connect with an arrow |
| Many steps in one symbol | Harder to read | Split into steps |
✗ i stays 1: 1 1 1 1 … End is never reached
✓ i = i + 1 added: 1 2 3, then End
Verification
Common Mistakes (cont.)
| Mistake | Symptom | Fix |
|---|---|---|
| The accumulator is reset inside the loop | For 4, 7, 2 it prints 2 instead of 13: only the last value survives | Move the sum = 0 box before the loop |
| The back arrow skips the read box | The same x is processed over and over; the sentinel never arrives | Route the arrow back above the read box |
| Assignment in a decision box: x = 0 ? | Unclear whether it is a question or an assignment | Write a comparison: x == 0 ? |
| Sentinel checked after processing | −1 is added to the sum and the count is one too high | Ask the decision before adding and counting |
| Broad condition first in a decision chain | The lower branches never run | Order from narrow to broad |
| Only one arrow leaves a decision | Flow breaks in the other case | Draw both exits, label Yes/No |
| No zero check before dividing | Division by zero if no data is entered | Add a count > 0 ? decision |
Summary
Chapter Summary
- A flowchart shows an algorithm's order of execution with standard symbols.
- Sequential flow is linear, conditional flow branches, and repetition has a path going back.
- Decision paths must be labeled, and every path must reach a next step.
- Verify a chart by hand-tracing it with different test inputs.
Summary
Review Questions
- What do the process and decision symbols represent?
- Why must decision exits be labeled?
- What causes an infinite loop in a flowchart?
- What is the difference between a counter and an accumulator?
- Which inputs should you choose to test a flowchart?
Summary
Exercises
- Draw a flowchart that prints whether a number is odd or even (hint: num % 2 == 0 ?).
- Draw a flowchart with nested decisions that converts an exam score from 0–100 to a letter grade AA / BA / … / FF; trace it for 85, 60 and 42.
- Draw a chart for the product of the numbers from 1 to n; what should the accumulator start at?
- Draw a chart that reads numbers until the user enters 0, then prints their sum and how many were entered (counter + accumulator).
- Add a 5-try limit to the guessing game: if the number is not found in 5 tries, print "You lose".
- Find the bug: i = 1, i <= 10 ? Yes → print i, back to the decision. How do you fix it?
Hints
- One decision, two exits.
- Order the decisions from high to low: grade >= 90 ?, grade >= 85 ? …
- 1 for a product.
- Decision: num == 0 ?
- Second condition: tries < 5 ?
- i is never incremented → infinite loop; add i = i + 1.
Summary
References
- ISO 5807:1985, Information processing: Documentation symbols and conventions for data, program and system flowcharts.
- Deitel and Deitel, Java: How to Program, Early Objects, 11th ed., Pearson, 2018.
- Sedgewick and Wayne, Computer Science: An Interdisciplinary Approach, Addison-Wesley, 2016: 1.3 Conditionals and Loops.
- Oracle, The Java Tutorials: Control Flow Statements.
- Downey and Mayfield, Think Java, 2nd ed., O'Reilly, 2019 (free online edition).