Chapter 4.2: Flowcharts

Computer Fundamentals and Introduction to Programming
SymbolsSequenceDecisionLoopVerification
Sercan KÜLCÜ · Giresun University

Introduction

Learning Outcomes

Symbols

Recognize the basic flowchart symbols.

Flow

Order the steps with arrows.

Control

Show decisions and loops.

Translation

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.
The example: an everyday task (a door code) as an algorithm. Two paths leave the decision box and join again.
YesNoStartread codecorrect?open doorwarnEnd

Symbols

Basic Symbols

Start / EndTerminalOne start and at least one end per chartsum = a + bProcessCalculation, assignmenta > b ?DecisionTwo exits: Yes / Noread aInput / outputGet data (read), show it (print)Flow lineDirection of execution; the arrowhead mattersAConnectorPaths merge / page break

The shape tells you the kind of step; inside, write only a short phrase for that step.

Symbols

Symbol and Role

SymbolRoleExample
OvalStart or endStart
RectangleProcess (calculation, assignment)sum = a + b
DiamondDecision (condition)a > b ?
ParallelogramInput / outputread a
ArrowDirection of executiongo to the next step
A decision holds a question (a > b ?); a process holds a command (sum = a + b).

Symbols

More Symbols

SymbolRoleExample
Off-page connectorThe chart continues on another page; label it with page and point.2-A
Predefined processCalls a subprocess drawn separately.Compute average
PreparationGives a loop counter's start and limit in one symbol.i = 1 → n
DocumentPrinted output or a report.Grade sheet
Data storeData kept on disk or in a database.Student records
AnnotationAttaches 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

1. Fix input and output2. Order the steps3. Separate decisions4. Connect arrows5. Trace an exampleread a, bprint larger

Problem: print the larger of two numbers. Input a and b (parallelogram: read), output the larger one (parallelogram: print).

1. Fix input and output2. Order the steps3. Separate decisions4. Connect arrows5. Trace an exampleStartread a, bEndprint larger

Add Start and End; order the steps top to bottom: read first, print last.

1. Fix input and output2. Order the steps3. Separate decisions4. Connect arrows5. Trace an exampleStartread a, ba > b ?print aprint bEnd

Which number to print depends on a question: add the decision a > b ? and split the output into two boxes.

1. Fix input and output2. Order the steps3. Separate decisions4. Connect arrows5. Trace an exampleYesNoStartread a, ba > b ?print aprint bEnd

Arrows give the direction of execution. Label the decision exits Yes/No and merge the paths at a connector.

1. Fix input and output2. Order the steps3. Separate decisions4. Connect arrows5. Trace an exampleYesNoStartread a, ba > b ?print aprint bEnd

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

Starta > bprint b, end, a=bprint aEnd

✗ Crossing arrows, unlabeled decision, three steps in one box

YesNoStarta > b ?print aprint bEnd

✓ 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?

FlowchartPseudocode
FormSymbols and arrowsShort indented lines
StrengthBranches and loops are visible at a glanceQuick to write, very close to code
WeaknessOutgrows the page; tedious to changeStructure depends on indentation
Best forLearning, presentations, business processesLong 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.

YesNoStartread xx < 0 ?x = −xprint xEnd
read x
if x < 0 then
    x = −x
print x

Design

The Three Structures of Structured Programming

Sequenceprocess Aprocess BSelectioncondition ?ABYesNoRepetitioncondition ?bodyYesNo
  • 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, goto is a reserved word but cannot be used.

Design

Flowchart Drawing Tools

Pen and paper

Fastest for a first draft; easy to erase and redraw. Exam charts are drawn by hand too.

Word and PowerPoint

The Flowchart group under Insert → Shapes; connector arrows follow when a box moves.

draw.io (diagrams.net)

Free and browser-based; ready-made flowchart shapes and export to PNG, SVG and PDF.

Microsoft Visio

A commercial tool for business process and workflow diagrams.

Flowgorithm

Runs the chart, shows variables step by step and converts it to code in many languages, including Java.

Mermaid

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

Startread aread bsum = a + bprint sumEnd
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: sum cannot be computed before a and b are read.

Kinds of Flow · Run the chart

Sequential Flow Step by Step (a = 4, b = 7)

Startread aread bsum = a + bprint sumEnd

Execution begins at the Start box. No variables exist yet.

Startread aread bsum = a + bprint sumEnd

Input box: the user enters 4 → a = 4.

Startread aread bsum = a + bprint sumEnd

The arrow leads to the next box: read b = 7.

Startread aread bsum = a + bprint sumEnd

Process box: sum = 4 + 7 = 11.

Startread aread bsum = a + bprint sumEnd

Output box: 11 is printed.

Startread aread bsum = a + bprint sumEnd

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

YesNoStartread gradegrade >= 60 ?print "Fail"print "Pass"End
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)

YesNoStartread gradegrade >= 60 ?print "Fail"print "Pass"End

Run 1 (grade = 72): execution begins at Start.

YesNoStartread gradegrade >= 60 ?print "Fail"print "Pass"End

The user enters 72 → grade = 72.

YesNoStartread gradegrade >= 60 ?print "Fail"print "Pass"End

Decision: 72 >= 60 → true. Flow continues along the Yes arrow.

YesNoStartread gradegrade >= 60 ?print "Fail"print "Pass"End

"Pass" is printed. The box on the No branch never runs this time.

YesNoStartread gradegrade >= 60 ?print "Fail"print "Pass"End

The two paths merge at the connector and reach End. Now run again with grade = 45 to see the other branch.

YesNoStartread gradegrade >= 60 ?print "Fail"print "Pass"End

Run 2 (grade = 45): execution begins at Start.

YesNoStartread gradegrade >= 60 ?print "Fail"print "Pass"End

The user enters 45 → grade = 45.

YesNoStartread gradegrade >= 60 ?print "Fail"print "Pass"End

Decision: 45 >= 60 → false. Flow continues along the No arrow.

YesNoStartread gradegrade >= 60 ?print "Fail"print "Pass"End

"Fail" is printed. The box on the Yes branch never runs this time.

YesNoStartread gradegrade >= 60 ?print "Fail"print "Pass"End

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

YesNoYesNoStartread a, b, cmax = ab > max ?max = bc > max ?max = cprint maxEnd

Run 1 (4, 9, 7): Start.

YesNoYesNoStartread a, b, cmax = ab > max ?max = bc > max ?max = cprint maxEnd

Read three numbers: a = 4, b = 9, c = 7.

YesNoYesNoStartread a, b, cmax = ab > max ?max = bc > max ?max = cprint maxEnd

First candidate for the largest: max = a = 4.

YesNoYesNoStartread a, b, cmax = ab > max ?max = bc > max ?max = cprint maxEnd

Decision: 9 > 4 → true → Yes.

YesNoYesNoStartread a, b, cmax = ab > max ?max = bc > max ?max = cprint maxEnd

max = b = 9. The path merges below.

YesNoYesNoStartread a, b, cmax = ab > max ?max = bc > max ?max = cprint maxEnd

Decision: 7 > 9 → false → No.

YesNoYesNoStartread a, b, cmax = ab > max ?max = bc > max ?max = cprint maxEnd

9 is printed.

YesNoYesNoStartread a, b, cmax = ab > max ?max = bc > max ?max = cprint maxEnd

End. Now try an input where c is the largest.

YesNoYesNoStartread a, b, cmax = ab > max ?max = bc > max ?max = cprint maxEnd

Run 2 (3, 2, 8): Start.

YesNoYesNoStartread a, b, cmax = ab > max ?max = bc > max ?max = cprint maxEnd

Read three numbers: a = 3, b = 2, c = 8.

YesNoYesNoStartread a, b, cmax = ab > max ?max = bc > max ?max = cprint maxEnd

First candidate for the largest: max = a = 3.

YesNoYesNoStartread a, b, cmax = ab > max ?max = bc > max ?max = cprint maxEnd

Decision: 2 > 3 → false → No; max stays.

YesNoYesNoStartread a, b, cmax = ab > max ?max = bc > max ?max = cprint maxEnd

Decision: 8 > 3 → true → Yes.

YesNoYesNoStartread a, b, cmax = ab > max ?max = bc > max ?max = cprint maxEnd

max = c = 8.

YesNoYesNoStartread a, b, cmax = ab > max ?max = bc > max ?max = cprint maxEnd

8 is printed.

YesNoYesNoStartread a, b, cmax = ab > max ?max = bc > max ?max = cprint maxEnd

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

YesNoYesNoYesNoread ss < 0 ?print "Icy"s < 15 ?print "Cold"s < 25 ?print "Mild"print "Hot"End
  • 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 … else chain.

Kinds of Flow

Compound Conditions: AND / OR

AND: both must be Yesage >= 18 ?has license ?YesCan rentYesCannot rentNoNoOR: one Yes is enoughday == Sat ?day == Sun ?NoWeekdayNoWeekendYesYes
  • 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)

YesNoYesNoYesNoStartread yearyear % 400 == 0 ?print "Leap"year % 100 == 0 ?print "Common"year % 4 == 0 ?print "Leap"print "Common"End

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.

YesNoYesNoYesNoStartread yearyear % 400 == 0 ?print "Leap"year % 100 == 0 ?print "Common"year % 4 == 0 ?print "Leap"print "Common"End

Read year = 2024.

YesNoYesNoYesNoStartread yearyear % 400 == 0 ?print "Leap"year % 100 == 0 ?print "Common"year % 4 == 0 ?print "Leap"print "Common"End

2024 % 400 = 24 → false → No.

YesNoYesNoYesNoStartread yearyear % 400 == 0 ?print "Leap"year % 100 == 0 ?print "Common"year % 4 == 0 ?print "Leap"print "Common"End

2024 % 100 = 24 → false → No.

YesNoYesNoYesNoStartread yearyear % 400 == 0 ?print "Leap"year % 100 == 0 ?print "Common"year % 4 == 0 ?print "Leap"print "Common"End

2024 % 4 = 0 → true → Yes.

YesNoYesNoYesNoStartread yearyear % 400 == 0 ?print "Leap"year % 100 == 0 ?print "Common"year % 4 == 0 ?print "Leap"print "Common"End

“Leap” is printed. 2024 is a leap year (February has 29 days).

YesNoYesNoYesNoStartread yearyear % 400 == 0 ?print "Leap"year % 100 == 0 ?print "Common"year % 4 == 0 ?print "Leap"print "Common"End

End. Run 2: 1900.

YesNoYesNoYesNoStartread yearyear % 400 == 0 ?print "Leap"year % 100 == 0 ?print "Common"year % 4 == 0 ?print "Leap"print "Common"End

Read year = 1900.

YesNoYesNoYesNoStartread yearyear % 400 == 0 ?print "Leap"year % 100 == 0 ?print "Common"year % 4 == 0 ?print "Leap"print "Common"End

1900 % 400 = 300 → false → No.

YesNoYesNoYesNoStartread yearyear % 400 == 0 ?print "Leap"year % 100 == 0 ?print "Common"year % 4 == 0 ?print "Leap"print "Common"End

1900 % 100 = 0 → true → Yes. The third decision is never asked.

YesNoYesNoYesNoStartread yearyear % 400 == 0 ?print "Leap"year % 100 == 0 ?print "Common"year % 4 == 0 ?print "Leap"print "Common"End

“Common” is printed. 1900 is divisible by 4 but is not a leap year; a chart that checked only % 4 would get this wrong.

YesNoYesNoYesNoStartread yearyear % 400 == 0 ?print "Leap"year % 100 == 0 ?print "Common"year % 4 == 0 ?print "Leap"print "Common"End

End. Run 3: 2000.

YesNoYesNoYesNoStartread yearyear % 400 == 0 ?print "Leap"year % 100 == 0 ?print "Common"year % 4 == 0 ?print "Leap"print "Common"End

Read year = 2000.

YesNoYesNoYesNoStartread yearyear % 400 == 0 ?print "Leap"year % 100 == 0 ?print "Common"year % 4 == 0 ?print "Leap"print "Common"End

2000 % 400 = 0 → true → Yes. The other two decisions are skipped.

YesNoYesNoYesNoStartread yearyear % 400 == 0 ?print "Leap"year % 100 == 0 ?print "Common"year % 4 == 0 ?print "Leap"print "Common"End

“Leap” is printed. 2000 is divisible by 100, but also by 400, so it is a leap year.

YesNoYesNoYesNoStartread yearyear % 400 == 0 ?print "Leap"year % 100 == 0 ?print "Common"year % 4 == 0 ?print "Leap"print "Common"End

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

YesrepeatNoStarti = 1i <= 5 ?print ii = i + 1End
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

YesrepeatNoStarti = 1i <= 5 ?print ii = i + 1End

Start.

YesrepeatNoStarti = 1i <= 5 ?print ii = i + 1End

The counter gets its first value: i = 1.

YesrepeatNoStarti = 1i <= 5 ?print ii = i + 1End

Decision: 1 <= 5 → true → Yes, enter the loop body.

YesrepeatNoStarti = 1i <= 5 ?print ii = i + 1End

1 is printed.

YesrepeatNoStarti = 1i <= 5 ?print ii = i + 1End

i = 1 + 1 = 2. The back arrow returns to the decision.

YesrepeatNoStarti = 1i <= 5 ?print ii = i + 1End

Decision: 2 <= 5 → true → Yes, enter the loop body.

YesrepeatNoStarti = 1i <= 5 ?print ii = i + 1End

2 is printed.

YesrepeatNoStarti = 1i <= 5 ?print ii = i + 1End

i = 2 + 1 = 3. The back arrow returns to the decision.

YesrepeatNoStarti = 1i <= 5 ?print ii = i + 1End

Decision: 3 <= 5 → true → Yes, enter the loop body.

YesrepeatNoStarti = 1i <= 5 ?print ii = i + 1End

3 is printed.

YesrepeatNoStarti = 1i <= 5 ?print ii = i + 1End

i = 3 + 1 = 4. The back arrow returns to the decision.

YesrepeatNoStarti = 1i <= 5 ?print ii = i + 1End

Decision: 4 <= 5 → true → Yes, enter the loop body.

YesrepeatNoStarti = 1i <= 5 ?print ii = i + 1End

4 is printed.

YesrepeatNoStarti = 1i <= 5 ?print ii = i + 1End

i = 4 + 1 = 5. The back arrow returns to the decision.

YesrepeatNoStarti = 1i <= 5 ?print ii = i + 1End

Decision: 5 <= 5 → true → Yes, enter the loop body.

YesrepeatNoStarti = 1i <= 5 ?print ii = i + 1End

5 is printed.

YesrepeatNoStarti = 1i <= 5 ?print ii = i + 1End

i = 5 + 1 = 6. The back arrow returns to the decision.

YesrepeatNoStarti = 1i <= 5 ?print ii = i + 1End

Decision: 6 <= 5 → false → No, leave the loop.

YesrepeatNoStarti = 1i <= 5 ?print ii = i + 1End

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.
count3+1 each passsum13+x each pass

Pass-by-pass values for inputs 4, 7, 2:

Stepxcountsum
start—00
pass 1414
pass 27211
pass 32313

Loops · Run the chart

Counter and Accumulator Step by Step (4, 7, 2)

YesNoStartcount = 0, sum = 0count < 3 ?read xsum = sum + xcount = count + 1print sumEnd

Start. Three numbers will be read and added; the inputs are 4, 7, 2.

YesNoStartcount = 0, sum = 0count < 3 ?read xsum = sum + xcount = count + 1print sumEnd

Counter and accumulator start at 0.

YesNoStartcount = 0, sum = 0count < 3 ?read xsum = sum + xcount = count + 1print sumEnd

Decision: 0 < 3 → true → Yes.

YesNoStartcount = 0, sum = 0count < 3 ?read xsum = sum + xcount = count + 1print sumEnd

Read x = 4.

YesNoStartcount = 0, sum = 0count < 3 ?read xsum = sum + xcount = count + 1print sumEnd

Accumulator: sum = 0 + 4 = 4.

YesNoStartcount = 0, sum = 0count < 3 ?read xsum = sum + xcount = count + 1print sumEnd

Counter: count = 0 + 1 = 1. Go back.

YesNoStartcount = 0, sum = 0count < 3 ?read xsum = sum + xcount = count + 1print sumEnd

Decision: 1 < 3 → true → Yes.

YesNoStartcount = 0, sum = 0count < 3 ?read xsum = sum + xcount = count + 1print sumEnd

Read x = 7.

YesNoStartcount = 0, sum = 0count < 3 ?read xsum = sum + xcount = count + 1print sumEnd

Accumulator: sum = 4 + 7 = 11.

YesNoStartcount = 0, sum = 0count < 3 ?read xsum = sum + xcount = count + 1print sumEnd

Counter: count = 1 + 1 = 2. Go back.

YesNoStartcount = 0, sum = 0count < 3 ?read xsum = sum + xcount = count + 1print sumEnd

Decision: 2 < 3 → true → Yes.

YesNoStartcount = 0, sum = 0count < 3 ?read xsum = sum + xcount = count + 1print sumEnd

Read x = 2.

YesNoStartcount = 0, sum = 0count < 3 ?read xsum = sum + xcount = count + 1print sumEnd

Accumulator: sum = 11 + 2 = 13.

YesNoStartcount = 0, sum = 0count < 3 ?read xsum = sum + xcount = count + 1print sumEnd

Counter: count = 2 + 1 = 3. Go back.

YesNoStartcount = 0, sum = 0count < 3 ?read xsum = sum + xcount = count + 1print sumEnd

Decision: 3 < 3 → false → No, the loop ends.

YesNoStartcount = 0, sum = 0count < 3 ?read xsum = sum + xcount = count + 1print sumEnd

Print the sum: 13.

YesNoStartcount = 0, sum = 0count < 3 ?read xsum = sum + xcount = count + 1print sumEnd

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

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd
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)

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

Start. Input: n = 4. Expected result 1 + 2 + 3 + 4 = 10.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

Read n = 4.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

Accumulator sum = 0, counter i = 1.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

Decision: 1 <= 4 → true → Yes.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

sum = 0 + 1 = 1.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

i = 2; back to the decision.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

Decision: 2 <= 4 → true → Yes.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

sum = 1 + 2 = 3.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

i = 3; back to the decision.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

Decision: 3 <= 4 → true → Yes.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

sum = 3 + 3 = 6.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

i = 4; back to the decision.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

Decision: 4 <= 4 → true → Yes.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

sum = 6 + 4 = 10.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

i = 5; back to the decision.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

Decision: 5 <= 4 → false → No, leave the loop.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

10 is printed.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

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-controlledSentinel-controlled
Number of repetitionsKnown in advance (n times)Unknown; stops when a special value arrives
Decisioni <= n ?x == −1 ? (sentinel yet?)
ExampleRead the grades of 30 students in a classItems scanned at a checkout, until the “done” key
Usually in Javaforwhile
  • 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)

NoYesStartsum = 0, count = 0read xx == −1 ?sum = sum + xcount = count + 1avg = sum / countprint avgEnd

Start. The user enters grades and types −1 when done. Inputs: 80, 64, 90, −1.

NoYesStartsum = 0, count = 0read xx == −1 ?sum = sum + xcount = count + 1avg = sum / countprint avgEnd

Accumulator and counter start at 0.

NoYesStartsum = 0, count = 0read xx == −1 ?sum = sum + xcount = count + 1avg = sum / countprint avgEnd

Read x = 80.

NoYesStartsum = 0, count = 0read xx == −1 ?sum = sum + xcount = count + 1avg = sum / countprint avgEnd

80 == −1 → false → No: this is a real grade.

NoYesStartsum = 0, count = 0read xx == −1 ?sum = sum + xcount = count + 1avg = sum / countprint avgEnd

sum = 0 + 80 = 80.

NoYesStartsum = 0, count = 0read xx == −1 ?sum = sum + xcount = count + 1avg = sum / countprint avgEnd

count = 1.

NoYesStartsum = 0, count = 0read xx == −1 ?sum = sum + xcount = count + 1avg = sum / countprint avgEnd

Read x = 64. The back arrow returns to the read box.

NoYesStartsum = 0, count = 0read xx == −1 ?sum = sum + xcount = count + 1avg = sum / countprint avgEnd

64 == −1 → false → No: this is a real grade.

NoYesStartsum = 0, count = 0read xx == −1 ?sum = sum + xcount = count + 1avg = sum / countprint avgEnd

sum = 80 + 64 = 144.

NoYesStartsum = 0, count = 0read xx == −1 ?sum = sum + xcount = count + 1avg = sum / countprint avgEnd

count = 2.

NoYesStartsum = 0, count = 0read xx == −1 ?sum = sum + xcount = count + 1avg = sum / countprint avgEnd

Read x = 90. The back arrow returns to the read box.

NoYesStartsum = 0, count = 0read xx == −1 ?sum = sum + xcount = count + 1avg = sum / countprint avgEnd

90 == −1 → false → No: this is a real grade.

NoYesStartsum = 0, count = 0read xx == −1 ?sum = sum + xcount = count + 1avg = sum / countprint avgEnd

sum = 144 + 90 = 234.

NoYesStartsum = 0, count = 0read xx == −1 ?sum = sum + xcount = count + 1avg = sum / countprint avgEnd

count = 3.

NoYesStartsum = 0, count = 0read xx == −1 ?sum = sum + xcount = count + 1avg = sum / countprint avgEnd

Read x = −1.

NoYesStartsum = 0, count = 0read xx == −1 ?sum = sum + xcount = count + 1avg = sum / countprint avgEnd

−1 == −1 → true → Yes: the sentinel arrived, so the loop ends. −1 was not added to the sum or the count.

NoYesStartsum = 0, count = 0read xx == −1 ?sum = sum + xcount = count + 1avg = sum / countprint avgEnd

avg = 234 / 3 = 78.

NoYesStartsum = 0, count = 0read xx == −1 ?sum = sum + xcount = count + 1avg = sum / countprint avgEnd

The average is printed.

NoYesStartsum = 0, count = 0read xx == −1 ?sum = sum + xcount = count + 1avg = sum / countprint avgEnd

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.
inner loop: j = 1, 2, 3 →outer loop: i = 1, 2, 3 ↓j=1j=2j=3i=1123i=2246i=3369

3 × 3 = 9 cells: the inner body runs 9 times. Orange row: the three passes of j while i = 2.

Deep nesting may be a sign that the algorithm should be split into subprocesses.

Loops · Run the chart

Nested Loop Step by Step (i = 1)

NoNoStarti = 1i <= 2 ?j = 1j <= 2 ?print i × jj = j + 1i = i + 1EndYesYes

Start. Outer loop i = 1, 2; inner loop j = 1, 2. Each row prints i × j.

NoNoStarti = 1i <= 2 ?j = 1j <= 2 ?print i × jj = j + 1i = i + 1EndYesYes

i = 1: the outer counter starts.

NoNoStarti = 1i <= 2 ?j = 1j <= 2 ?print i × jj = j + 1i = i + 1EndYesYes

Outer decision: 1 <= 2 → true → enter the inner loop.

NoNoStarti = 1i <= 2 ?j = 1j <= 2 ?print i × jj = j + 1i = i + 1EndYesYes

j = 1: the inner counter is reset to 1 on every outer pass.

NoNoStarti = 1i <= 2 ?j = 1j <= 2 ?print i × jj = j + 1i = i + 1EndYesYes

Inner decision: 1 <= 2 → true

NoNoStarti = 1i <= 2 ?j = 1j <= 2 ?print i × jj = j + 1i = i + 1EndYesYes

1 × 1 = 1 is printed.

NoNoStarti = 1i <= 2 ?j = 1j <= 2 ?print i × jj = j + 1i = i + 1EndYesYes

j = 2; back to the inner decision.

NoNoStarti = 1i <= 2 ?j = 1j <= 2 ?print i × jj = j + 1i = i + 1EndYesYes

Inner decision: 2 <= 2 → true

NoNoStarti = 1i <= 2 ?j = 1j <= 2 ?print i × jj = j + 1i = i + 1EndYesYes

1 × 2 = 2 is printed.

NoNoStarti = 1i <= 2 ?j = 1j <= 2 ?print i × jj = j + 1i = i + 1EndYesYes

j = 3; back to the inner decision.

NoNoStarti = 1i <= 2 ?j = 1j <= 2 ?print i × jj = j + 1i = i + 1EndYesYes

Inner decision: 3 <= 2 → false → No, the inner loop ends.

NoNoStarti = 1i <= 2 ?j = 1j <= 2 ?print i × jj = j + 1i = i + 1EndYesYes

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)

NoNoStarti = 1i <= 2 ?j = 1j <= 2 ?print i × jj = j + 1i = i + 1EndYesYes

Outer decision: 2 <= 2 → true → enter the inner loop.

NoNoStarti = 1i <= 2 ?j = 1j <= 2 ?print i × jj = j + 1i = i + 1EndYesYes

j = 1: the inner counter is reset to 1 on every outer pass.

NoNoStarti = 1i <= 2 ?j = 1j <= 2 ?print i × jj = j + 1i = i + 1EndYesYes

Inner decision: 1 <= 2 → true

NoNoStarti = 1i <= 2 ?j = 1j <= 2 ?print i × jj = j + 1i = i + 1EndYesYes

2 × 1 = 2 is printed.

NoNoStarti = 1i <= 2 ?j = 1j <= 2 ?print i × jj = j + 1i = i + 1EndYesYes

j = 2; back to the inner decision.

NoNoStarti = 1i <= 2 ?j = 1j <= 2 ?print i × jj = j + 1i = i + 1EndYesYes

Inner decision: 2 <= 2 → true

NoNoStarti = 1i <= 2 ?j = 1j <= 2 ?print i × jj = j + 1i = i + 1EndYesYes

2 × 2 = 4 is printed.

NoNoStarti = 1i <= 2 ?j = 1j <= 2 ?print i × jj = j + 1i = i + 1EndYesYes

j = 3; back to the inner decision.

NoNoStarti = 1i <= 2 ?j = 1j <= 2 ?print i × jj = j + 1i = i + 1EndYesYes

Inner decision: 3 <= 2 → false → No, the inner loop ends.

NoNoStarti = 1i <= 2 ?j = 1j <= 2 ?print i × jj = j + 1i = i + 1EndYesYes

Row done: println() ends the line. Then i = 3; back to the outer decision.

NoNoStarti = 1i <= 2 ?j = 1j <= 2 ?print i × jj = j + 1i = i + 1EndYesYes

Outer decision: 3 <= 2 → false → No, go to End.

NoNoStarti = 1i <= 2 ?j = 1j <= 2 ?print i × jj = j + 1i = i + 1EndYesYes

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)

YesNoYesNoStartsecret = 37, tries = 0read guesstries = tries + 1guess == secret ?guess < secret ?print "Higher"print "Lower"print "Got it"End

Start. The secret number (normally random) is 37; the player guesses 50, 25, 37.

YesNoYesNoStartsecret = 37, tries = 0read guesstries = tries + 1guess == secret ?guess < secret ?print "Higher"print "Lower"print "Got it"End

The secret is set and the try counter is 0.

YesNoYesNoStartsecret = 37, tries = 0read guesstries = tries + 1guess == secret ?guess < secret ?print "Higher"print "Lower"print "Got it"End

The player enters 50.

YesNoYesNoStartsecret = 37, tries = 0read guesstries = tries + 1guess == secret ?guess < secret ?print "Higher"print "Lower"print "Got it"End

tries = 1.

YesNoYesNoStartsecret = 37, tries = 0read guesstries = tries + 1guess == secret ?guess < secret ?print "Higher"print "Lower"print "Got it"End

Decision: 50 == 37 → false → No, give a hint.

YesNoYesNoStartsecret = 37, tries = 0read guesstries = tries + 1guess == secret ?guess < secret ?print "Higher"print "Lower"print "Got it"End

Decision: 50 < 37 → false.

YesNoYesNoStartsecret = 37, tries = 0read guesstries = tries + 1guess == secret ?guess < secret ?print "Higher"print "Lower"print "Got it"End

"Lower" is printed; the arrow returns to the read box.

YesNoYesNoStartsecret = 37, tries = 0read guesstries = tries + 1guess == secret ?guess < secret ?print "Higher"print "Lower"print "Got it"End

The player enters 25.

YesNoYesNoStartsecret = 37, tries = 0read guesstries = tries + 1guess == secret ?guess < secret ?print "Higher"print "Lower"print "Got it"End

tries = 2.

YesNoYesNoStartsecret = 37, tries = 0read guesstries = tries + 1guess == secret ?guess < secret ?print "Higher"print "Lower"print "Got it"End

Decision: 25 == 37 → false → No, give a hint.

YesNoYesNoStartsecret = 37, tries = 0read guesstries = tries + 1guess == secret ?guess < secret ?print "Higher"print "Lower"print "Got it"End

Decision: 25 < 37 → true.

YesNoYesNoStartsecret = 37, tries = 0read guesstries = tries + 1guess == secret ?guess < secret ?print "Higher"print "Lower"print "Got it"End

"Higher" is printed; the arrow returns to the read box.

YesNoYesNoStartsecret = 37, tries = 0read guesstries = tries + 1guess == secret ?guess < secret ?print "Higher"print "Lower"print "Got it"End

The player enters 37.

YesNoYesNoStartsecret = 37, tries = 0read guesstries = tries + 1guess == secret ?guess < secret ?print "Higher"print "Lower"print "Got it"End

tries = 3.

YesNoYesNoStartsecret = 37, tries = 0read guesstries = tries + 1guess == secret ?guess < secret ?print "Higher"print "Lower"print "Got it"End

Decision: 37 == 37 → true → Yes.

YesNoYesNoStartsecret = 37, tries = 0read guesstries = tries + 1guess == secret ?guess < secret ?print "Higher"print "Lower"print "Got it"End

"Correct!" is printed with the try count, 3.

YesNoYesNoStartsecret = 37, tries = 0read guesstries = tries + 1guess == secret ?guess < secret ?print "Higher"print "Lower"print "Got it"End

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)

YesNoStartread num1 <= num <= 5 ?print numEndprint "Invalid"

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.

YesNoStartread num1 <= num <= 5 ?print numEndprint "Invalid"

Body first: read num = 7. The condition has not been checked yet.

YesNoStartread num1 <= num <= 5 ?print numEndprint "Invalid"

Decision: 1 ≤ 7 ≤ 5 ? Since 7 > 5, false → No.

YesNoStartread num1 <= num <= 5 ?print numEndprint "Invalid"

"Invalid" is printed.

YesNoStartread num1 <= num <= 5 ?print numEndprint "Invalid"

The arrow returns to the read box: read num = 0.

YesNoStartread num1 <= num <= 5 ?print numEndprint "Invalid"

Decision: 1 ≤ 0 ≤ 5 ? Since 0 < 1, false → No.

YesNoStartread num1 <= num <= 5 ?print numEndprint "Invalid"

"Invalid" is printed again.

YesNoStartread num1 <= num <= 5 ?print numEndprint "Invalid"

Read again: num = 3.

YesNoStartread num1 <= num <= 5 ?print numEndprint "Invalid"

Decision: 1 ≤ 3 ≤ 5 → true → Yes.

YesNoStartread num1 <= num <= 5 ?print numEndprint "Invalid"

The valid number is printed: 3.

YesNoStartread num1 <= num <= 5 ?print numEndprint "Invalid"

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

YesNoi = 1i <= n ?sum = sum + ii = i + 1print sum1234
for (i = 1; i <= n; i++) {
    sum = sum + i;
}
  1. Initialization (i = 1): runs once, on entering the loop.
  2. Condition (i <= n): checked before every pass; for is a pre-test loop.
  3. Body: runs if the condition is true.
  4. 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

YesNoNoYesi = 1i <= n ?a[i] < 0 ?i = i + 1print a[i]print "None"Endearly exit

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

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd
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 while condition and body.
  • Output → println.

Translating to Code · Symbol ↔ line of code

From Flowchart to Java Code Step by Step (n = 3)

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

The Start symbol is the start of the method: main opens.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

Input symbol → int n = 3; (the input is fixed).

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

Process symbol → the initial values of two variables.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

Decision symbol → while (i <= n): 1 <= 3 is true.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

The Yes path is the loop body: sum = 0 + 1 = 1.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

i = i + 1 → i = 2. The next arrow goes back to the decision.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

The back arrow is the jump from the end of the loop block to the condition: 2 <= 3 is true.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

sum = 1 + 2 = 3.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

i = 3.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

3 <= 3 is true.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

sum = 3 + 3 = 6.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

i = 4.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

4 <= 3 is false → the No path leaves the loop block.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

Output symbol → println(sum): 6 is printed.

YesNoStartread nsum = 0, i = 1i <= n ?sum = sum + ii = i + 1print sumEnd

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

Main chartSubprocessStartread a, b, cavg = Average(a, b, c)print avgEndAverage(x, y, z)t = x + y + zReturn t / 3call: x=70, y=85, z=91return: 82
  • 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

YesNoStartread gradegrade >= 60 ?print "Fail"print "Pass"End

Normal input grade = 72: the Yes path runs.

YesNoStartread gradegrade >= 60 ?print "Fail"print "Pass"End

The other branch with grade = 45: the No path runs. Now every box has run at least once.

YesNoStartread gradegrade >= 60 ?print "Fail"print "Pass"End

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)

YesNoread a, bb != 0 ?k = a % ba = bb = kprint a
Stepabkb != 0 ?
start4818—18 != 0 true
pass 118121212 != 0 true
pass 212666 != 0 true
pass 36000 != 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

MistakeEffectFix
Unlabeled decision pathUnclear which path is whichWrite Yes/No
Counter never updatedInfinite loopAdd a counter step
Unconnected symbolFlow breaksConnect with an arrow
Many steps in one symbolHarder to readSplit into steps
YesNoi = 1i <= 3 ?print iEnd

✗ i stays 1: 1 1 1 1 … End is never reached

YesNoi = 1i <= 3 ?print ii = i + 1End

✓ i = i + 1 added: 1 2 3, then End

Verification

Common Mistakes (cont.)

MistakeSymptomFix
The accumulator is reset inside the loopFor 4, 7, 2 it prints 2 instead of 13: only the last value survivesMove the sum = 0 box before the loop
The back arrow skips the read boxThe same x is processed over and over; the sentinel never arrivesRoute the arrow back above the read box
Assignment in a decision box: x = 0 ?Unclear whether it is a question or an assignmentWrite a comparison: x == 0 ?
Sentinel checked after processing−1 is added to the sum and the count is one too highAsk the decision before adding and counting
Broad condition first in a decision chainThe lower branches never runOrder from narrow to broad
Only one arrow leaves a decisionFlow breaks in the other caseDraw both exits, label Yes/No
No zero check before dividingDivision by zero if no data is enteredAdd 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.
step 1step 2step 3condition?YesNoABcondition?YesprocessrepeatNoSequential: linearConditional: branchesRepetition: loops back

Summary

Review Questions

  1. What do the process and decision symbols represent?
  2. Why must decision exits be labeled?
  3. What causes an infinite loop in a flowchart?
  4. What is the difference between a counter and an accumulator?
  5. Which inputs should you choose to test a flowchart?

Summary

Exercises

  1. Draw a flowchart that prints whether a number is odd or even (hint: num % 2 == 0 ?).
  2. 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.
  3. Draw a chart for the product of the numbers from 1 to n; what should the accumulator start at?
  4. Draw a chart that reads numbers until the user enters 0, then prints their sum and how many were entered (counter + accumulator).
  5. Add a 5-try limit to the guessing game: if the number is not found in 5 tries, print "You lose".
  6. Find the bug: i = 1, i <= 10 ? Yes → print i, back to the decision. How do you fix it?
Hints
  1. One decision, two exits.
  2. Order the decisions from high to low: grade >= 90 ?, grade >= 85 ? …
  3. 1 for a product.
  4. Decision: num == 0 ?
  5. Second condition: tries < 5 ?
  6. 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).

The End

Chapter 4.2: Flowcharts
Previous: Chapter 4.1: Algorithms · Next: Chapter 4.3: Introduction to Programming
1 / 1Sercan KÜLCÜ. All rights reserved.