Java Flowchart Study Kit
Lessons

From flowchart to Java

Each lesson puts a flowchart next to the Java code it becomes. Read the flowchart first, then find each symbol in the code.

1. Flowchart symbols

Memorize these. Symbol questions are the easiest points on any flowchart quiz.

SymbolNameMeaningJava example
Start Terminal
oval / rounded rectangle
Where the program starts or ends. public static void main(...) { … }
Input x Input / Output
parallelogram
Reading data in, or displaying results. x = sc.nextInt();
System.out.println(x);
sum = a + b Process
rectangle
A calculation or assignment. sum = a + b;
count++;
x > 0? Decision
diamond
A yes/no (true/false) question. Has two exits. if (x > 0)
while (i <= 10)
Flow line
arrow
Shows which step runs next. The order of your statements
A On-page connector
small circle
Joins flow lines on the same page so they don't cross. (no code, drawing aid only)
1 Off-page connector
home-plate pentagon
The flowchart continues on another page. (no code, drawing aid only)
getAvg() Predefined process
rectangle with double sides
Calls a method/module that is defined somewhere else. avg = getAverage(nums);
i = 1 Preparation
hexagon
Setup/initialization, often a for loop's start value. Not every professor uses it. for (int i = 1; …)

2. Sequence

The simplest structure: steps run once each, in order, top to bottom. There are no diamonds and no arrows going back up.

Start Input length, width area = length * width Output area End
Area of a rectangle
import java.util.Scanner;

public class AreaOfRectangle {
    public static void main(String[] args) {   // Start
        Scanner sc = new Scanner(System.in);

        System.out.print("Length: ");
        double length = sc.nextDouble();        // Input
        System.out.print("Width: ");
        double width = sc.nextDouble();         // Input

        double area = length * width;           // Process

        System.out.println("Area = " + area);   // Output
    }                                           // End
}

Tip: the prompt System.out.print("Length: ") is usually drawn inside the same Input parallelogram. You don't need a separate Output symbol for every prompt.

3. Selection (decisions)

A diamond asks a true/false question. The flow splits into two paths and then joins back together before continuing.

3a. if (one-way selection)

Only the True path does something. The False path skips straight to the next step.

n < 0? True n = -n False Print n
Absolute value: only negative numbers get changed
if (n < 0) {            // Decision
    n = -n;             // True path
}                       // False path: skip
System.out.println(n);  // paths join here

3b. if-else (two-way selection)

Both paths do something. Exactly one of them runs, never both.

Start Input score score >= 75? True False Print "Passed" Print "Failed" End
Pass or fail
Scanner sc = new Scanner(System.in);
int score = sc.nextInt();              // Input

if (score >= 75) {                     // Decision
    System.out.println("Passed");      // True
} else {
    System.out.println("Failed");      // False
}

3c. if-else-if ladder (multi-way selection)

A chain of diamonds. Each False exit leads to the next diamond. The first condition that is True wins and the rest are skipped. The final else is what happens when every condition is False.

score >= 90? True Print "A" False score >= 80? True Print "B" False score >= 70? True Print "C" False Print "F" End
Letter grade: only one Print ever runs
if (score >= 90) {
    System.out.println("A");
} else if (score >= 80) {
    System.out.println("B");
} else if (score >= 70) {
    System.out.println("C");
} else {
    System.out.println("F");
}

Why doesn't a score of 95 print A, B and C? Because 95 goes down the True path of the first diamond, which skips every other diamond. Order matters: check the biggest range first.

3d. Nested if

A nested if is a diamond placed inside the True or False path of another diamond. In Java it's an if inside the braces of another if.

if (age >= 18) {                        // outer diamond
    if (hasId) {                        // inner diamond, on the True path
        System.out.println("Allowed");
    } else {
        System.out.println("Show your ID");
    }
} else {
    System.out.println("Too young");
}

3e. switch

A switch is drawn like the ladder above: one diamond per case (day == 1?, day == 2?, …), and the default is the last False path. Some books draw one diamond with several labeled exits instead. Both are accepted.

switch (day) {
    case 1:  System.out.println("Monday");  break;
    case 2:  System.out.println("Tuesday"); break;
    default: System.out.println("Other day");
}

Watch out for break. Without it, Java "falls through" and also runs the next case. In a flowchart, break is the arrow that leaves the switch and goes to the join point.

4. Repetition (loops)

You can spot a loop in a flowchart by the arrow that goes back up to an earlier step. Every loop needs three things:

Initialize

Give the loop variable a starting value, before the loop. i = 1

Test

The diamond. While it's True, keep looping. i <= 5?

Update

Change the variable so the test eventually becomes False. i = i + 1

Forget the update and you get an infinite loop: the arrow never reaches End.

4a. while loop (pre-test)

The diamond comes before the body. If the condition is False the first time, the body runs zero times.

Start i = 1 i <= 5? True Print i i = i + 1 False End
Print 1 to 5 with a while loop
int i = 1;                   // initialize
while (i <= 5) {             // test (diamond)
    System.out.println(i);   // body
    i = i + 1;               // update
}                            // arrow back up
// False: exit to End
Step through it
Step 0 i = ? Output: (none)

Press Step to follow the flowchart one symbol at a time. The current symbol is outlined.

4b. for loop (pre-test, counter-controlled)

A for loop is a while loop with the three parts packed into one line. The flowchart is the same shape as the while loop: initialize → test → body → update → back to test.

Start sum = 0, i = 1 i <= 5? True sum = sum + i i++ False Print sum End
Sum of 1 to 5 (result: 15)
int sum = 0;
for (int i = 1; i <= 5; i++) {
    sum = sum + i;
}
System.out.println(sum);   // 15
Part of the forWhere it goes
int i = 1Process box before the diamond (runs once)
i <= 5The diamond (checked every time)
i++Process box at the end of the body, just before the arrow goes back up

4c. do-while loop (post-test)

The diamond comes after the body, so the body always runs at least once. This is the only loop where the arrow goes back up from the bottom diamond.

Start num = 1 Print num num = num + 1 num <= 5? True False End
Body first, test last
int num = 1;
do {
    System.out.println(num);   // body runs first
    num = num + 1;
} while (num <= 5);            // test at the bottom
                               // note the semicolon!

Classic use: a menu or a password prompt. You always want to ask at least once, then repeat if the answer is invalid.

Loop comparison

LoopDiamond positionTest typeMinimum runsBest for
whileBefore the bodyPre-test0Unknown number of repeats (e.g. until the user enters -1)
forBefore the bodyPre-test0Known number of repeats (counting)
do-whileAfter the bodyPost-test1Must run at least once (menus, input validation)

Counter vs. sentinel loops

Counter-controlled

Repeats a known number of times using a counter. "Print 1 to 10."

for (int i = 1; i <= 10; i++) { ... }

Sentinel-controlled

Repeats until a special value (the sentinel) is entered. "Enter numbers, -1 to stop."

int num = sc.nextInt();
while (num != -1) {
    sum += num;
    num = sc.nextInt();
}

5. Converting Java code to a flowchart

This is a very common quiz question: "Draw the flowchart of this program." Work through the code line by line:

  1. Draw Start for the beginning of main.
  2. Every sc.next…() becomes an Input parallelogram. Every System.out.print… becomes an Output parallelogram.
  3. Every assignment or calculation (=, ++, +=) becomes a Process rectangle. Declarations with no value (int x;) are usually skipped.
  4. Every if, while, for or do-while condition becomes a Decision diamond. Label both exits True/False.
  5. For loops, draw the arrow back up to the diamond (or to the top of the body, for do-while).
  6. Draw End where main finishes, and check that every path reaches it.

6. Common mistakes (and how to avoid them)

Wrong symbol for I/O

Printing is Output (parallelogram), not Process (rectangle).

Unlabeled decisions

Always write True/False (or Yes/No) on both exits of a diamond.

Paths that never join

After an if-else, both paths must merge before the next step.

Missing update

No i++ in the loop means the diamond never becomes False: an infinite loop.

Initializing inside the loop

sum = 0 goes before the loop. Inside the loop it resets every time.

Loop arrow to the wrong place

while/for loops return to the diamond. They don't return to Start or to the initialization.