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. Symbols
- 2. Sequence
- 3. Selection
- 4. Repetition (loops)
- 5. Converting Java to a flowchart
- 6. Common mistakes
1. Flowchart symbols
Memorize these. Symbol questions are the easiest points on any flowchart quiz.
| Symbol | Name | Meaning | Java example |
|---|---|---|---|
| Terminal oval / rounded rectangle |
Where the program starts or ends. | public static void main(...) { … } |
|
| Input / Output parallelogram |
Reading data in, or displaying results. | x = sc.nextInt();System.out.println(x); |
|
| Process rectangle |
A calculation or assignment. | sum = a + b;count++; |
|
| 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 | |
| On-page connector small circle |
Joins flow lines on the same page so they don't cross. | (no code, drawing aid only) | |
| Off-page connector home-plate pentagon |
The flowchart continues on another page. | (no code, drawing aid only) | |
| Predefined process rectangle with double sides |
Calls a method/module that is defined somewhere else. | avg = getAverage(nums); |
|
| 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.
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.
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.
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.
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.
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
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.
int sum = 0;
for (int i = 1; i <= 5; i++) {
sum = sum + i;
}
System.out.println(sum); // 15
Part of the for | Where it goes |
|---|---|
int i = 1 | Process box before the diamond (runs once) |
i <= 5 | The 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.
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
| Loop | Diamond position | Test type | Minimum runs | Best for |
|---|---|---|---|---|
while | Before the body | Pre-test | 0 | Unknown number of repeats (e.g. until the user enters -1) |
for | Before the body | Pre-test | 0 | Known number of repeats (counting) |
do-while | After the body | Post-test | 1 | Must 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:
- Draw Start for the beginning of
main. - Every
sc.next…()becomes an Input parallelogram. EverySystem.out.print…becomes an Output parallelogram. - Every assignment or calculation (
=,++,+=) becomes a Process rectangle. Declarations with no value (int x;) are usually skipped. - Every
if,while,forordo-whilecondition becomes a Decision diamond. Label both exits True/False. - For loops, draw the arrow back up to the diamond (or to the top of the body, for
do-while). - Draw End where
mainfinishes, 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.
Ready? Take a quiz →