Embercode
✕
Flowcharts and PseudocodeLesson 13 of 13

40 min read

One flowchart in three languages

Why this matters

A flowchart shows what a program does without tying it to one language. The same boxes and arrows can be written in Python, in Java or in C++. Each language has its own spelling, its own rules for reading input, and its own way of ending a program. The logic underneath is the same.

This lesson takes one flowchart and writes it three times. For each box in the flowchart you will find the line in each language that carries it out. If you can point at the matching lines, you can read code in a language you have not learned yet, and you can check that a program matches the plan it was built from.

You will need Python 3, Java and a C++ compiler. This lesson was checked with Python 3.14.4, OpenJDK 25.0.4.1 and g++ 15.2.0. Any recent version of each will do for the code shown here.

The lesson on pseudocode, the earlier lesson on flowchart symbols, and the lesson on tracing a program by hand all come before this one. Here we do not explain the symbols again. We only use them.

The flowchart

The problem is this. Read whole numbers one at a time until the number 0 is entered. Count how many of the numbers, not counting the 0, are even. Print the count.

The steps of the flowchart are below. Each step is a box, and the shape of the box tells you what kind of step it is. A process box changes a value. A decision box asks a question with a yes or no answer and sends the flow one of two ways. An input box reads a value from the user. An output box shows a value. A terminator box, drawn as a rounded shape, marks the start or the end.

  1. Start (terminator).
  2. Process: set count to 0.
  3. Input: read a number.
  4. Decision: is the number 0? If yes, go to step 8. If no, go to step 5.
  5. Decision: is the number even? If yes, go to step 6. If no, go to step 7.
  6. Process: add 1 to count.
  7. Go back to step 3 and read the next number.
  8. Output: print count.
  9. End (terminator).

Step 7 is a line in the flowchart that points back up to an earlier box. A loop like this is where the flow goes around and around. The loop ends only when the decision in step 4 says yes.

Work through an example by hand. The input is 4, then 7, then 10, then 0.

  • Start. count is 0.
  • Read 4. Is it 0? No. Is it even? Yes. count becomes 1. Go back.
  • Read 7. Is it 0? No. Is it even? No. Go back.
  • Read 10. Is it 0? No. Is it even? Yes. count becomes 2. Go back.
  • Read 0. Is it 0? Yes. Print count, which is 2. End.

The answer is 2. Keep that example in mind, because we will run the same input in all three languages.

Python

Python is the shortest of the three. Here is the program, with line numbers added in this lesson's notes only. The numbers are not part of the file.

count = 0
number = int(input())
while number != 0:
    if number % 2 == 0:
        count = count + 1
    number = int(input())
print(count)

Line 1 sets count to 0. Line 2 reads a line of text from the user and changes it into a whole number with int. The input() function waits for the user to type a line and press Enter, and it returns that line as text. int turns the text into a number that the program can do arithmetic with.

Line 3 starts the loop. The loop body is the indented block below it, lines 4 to 6. The loop runs while number is not 0. The flowchart asks the opposite question in step 4, "is the number 0?", and leaves the loop when the answer is yes. A while line asks whether to keep going, so the two questions are the same test with the answer flipped.

Line 4 is the even-number test. The % symbol gives the remainder after division. A number divided by 2 has remainder 0 when it is even. So number % 2 == 0 is true for even numbers.

Line 5 adds 1 to count. Python uses indentation to show which lines belong to the if, and which belong to the loop. The indentation is part of the syntax, so a mistake in the spaces changes the program.

Line 6 reads the next number. This is the flowchart's step 7, the return to the input box. Python has no separate arrow. The loop goes back to line 3 by itself, after the last line of the body.

Line 7 prints count after the loop ends. This is step 8.

Java

Java needs more lines than Python, because every program lives inside a class and a method. Here is the same logic written in Java. The file must be named EvenCounter.java, because Java requires the file name to match the public class name.

import java.util.Scanner;
 
public class EvenCounter {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int count = 0;
        int number = in.nextInt();
        while (number != 0) {
            if (number % 2 == 0) {
                count = count + 1;
            }
            number = in.nextInt();
        }
        System.out.println(count);
    }
}

Line 1 imports the Scanner class, which reads values from the keyboard. Without this line, Java does not know the name Scanner.

Lines 3 and 4 open the class and the main method. The main method is where Java starts running the program.

Line 5 creates a Scanner that reads from the keyboard, which Java calls System.in. Line 6 sets count to 0. Line 7 reads the first number with nextInt, which reads one whole number and returns it.

Line 8 starts the loop, with the same test as the Python loop. Lines 9 to 11 are the even-number test and the add. Line 12 reads the next number, which is the return to the input box. Line 13 closes the loop.

Line 14 prints count. Java's System.out.println prints a value and then moves to a new line.

Java uses braces, { and }, to group lines. Python used indentation for the same job. The Java compiler does not care about indentation, so a missing brace is an error even when the lines look right.

C++

C++ sits between the two. It has braces like Java, but it has no class around the program, so main stands alone. Here is the C++ version.

#include <iostream>
 
int main() {
    int count = 0;
    int number;
    std::cin >> number;
    while (number != 0) {
        if (number % 2 == 0) {
            count = count + 1;
        }
        std::cin >> number;
    }
    std::cout << count << std::endl;
    return 0;
}

Line 1 includes the input and output library. Line 3 starts the main function, where the program begins.

Line 4 sets count to 0. Line 5 declares number without giving it a value. Line 6 reads the first number from the keyboard with std::cin >>. The std:: prefix says that cin belongs to the standard library.

Line 7 starts the loop, with the same test as before. Lines 8 to 10 hold the even-number test and the add. Line 11 reads the next number. Line 12 closes the loop.

Line 13 prints count. The std::endl at the end moves to a new line. Line 14 returns 0 to the system, which tells it that the program finished without an error. Python and Java end the program without this line, but C++ expects it.

Matching the boxes to the lines

Here is the table that points each flowchart step at the line that carries it out in each language. Read across a row to see one step in all three languages.

Flowchart stepPython lineJava lineC++ line
2. Set count to 0164
3. Read a number276
4. Is the number 0?387
5. Is the number even?498
6. Add 1 to count5109
7. Read the next number61211
8. Print count71413

Two rows need a note. Step 4 is a decision that exits the loop, but in all three languages the line that does it is the while line. That line asks whether to keep looping, which is the opposite of the flowchart's question. Step 3 appears twice in the code, once before the loop and once inside it. The table lists the first read for step 3, and the read inside the loop for step 7.

Notice what is the same across the three versions and what is not. The logic lines up one for one. The != test is the same in all three. The input calls differ: Python uses input and int, Java uses a Scanner, and C++ uses std::cin. The end of the program is different too. Python and Java end on their own, and C++ returns 0 explicitly.

Running all three

Put the input in a text file, one number per line. The file below holds the example from the flowchart section.

4
7
10
0

Save it as input1.txt and run each version with the input redirected from that file. The < symbol in the command tells the terminal to read the keyboard input from the file instead of from you.

python3 even_counter.py < input1.txt
javac EvenCounter.java && java EvenCounter < input1.txt
g++ -std=c++17 even_counter.cpp -o even_counter && ./even_counter < input1.txt

Each command prints the same line:

2
2
2

The three programs agree with the hand trace, which gave 2. They also agree on the other two inputs we tried. The input 0 on its own gives 0, and the input 3, -2, 5, 8, 6, 0 gives 3, because -2, 8 and 6 are the even numbers.

input: 0               each version prints 0
input: 3 -2 5 8 6 0    each version prints 3

Negative numbers work here because number % 2 gives 0 for -2 in all three languages. An even number, positive or negative, has remainder 0.

Common mistakes

Python reads text that is not a number. If the user types abc where a number is expected, int fails. With the input 4, abc, 0 on separate lines, Python stops with this message:

ValueError: invalid literal for int() with base 10: 'abc'

The message points at line 2 or line 6 of the file, depending on which read failed. Read the last line of the message first. It names the bad text.

Python runs out of input. If the file ends before a 0 is read, input has nothing to return. With the input 4 and 6 and then the end of the file, Python stops with this message:

EOFError: EOF when reading a line

EOF stands for end of file. It means the program asked for another line, and there was none.

Java reads text that is not a number. With the input 4, abc, 0, Java stops with this message:

Exception in thread "main" java.util.InputMismatchException

The nextInt call on line 7 or line 12 failed. Java throws an exception, which is its name for a runtime error, and stops the program. The lines after the message show which line of your file was running.

Java runs out of input. With the input 4, 6 and then the end of the file, Java stops with this message:

Exception in thread "main" java.util.NoSuchElementException

It means nextInt found no more numbers to read.

C++ stops silently on bad input. C++ does not stop with a message when std::cin fails. With the input 4, abc, 0, the program prints this and nothing else:

1

When the read fails, C++ sets number to 0. The loop test then sees 0 and ends. The count is 1 because only the 4 was counted before the bad text. The program reports a wrong answer with no error. This is why the C++ version needs a check on every read.

C++ loops forever when the input ends early. This mistake is the most serious of these. With the input 4, 6 and then the end of the file, the program never prints anything. We ran it with a three second time limit, and the program was still running when the limit stopped it.

The reason is in lines 6 and 11. When the input is at its end, std::cin >> number fails and leaves number unchanged. It still holds 6, so the loop test number != 0 stays true, and the read fails again and again. The loop never reaches 0.

A fix is to put the read inside the loop test, so that a failed read ends the loop. The fixed program reads once, in the loop test, so the reads on lines 6 and 11 are removed. The whole program looks like this:

#include <iostream>
 
int main() {
    int count = 0;
    int number;
    while (std::cin >> number && number != 0) {
        if (number % 2 == 0) {
            count = count + 1;
        }
    }
    std::cout << count << std::endl;
    return 0;
}

The && means both parts must be true. If the read fails, the first part is false and the loop ends. This version gives 2 for the early-end input, and 1 for the input with abc, which is a silent stop as described above, but it no longer hangs.

Changing one language and forgetting the others. Once you have three versions of the same program, a change to the logic has to go into all three. The flowchart is the place to make the change first, and the table tells you which line to change in each language. If you change the test in one file only, the three programs will give different answers for the same input.

Practice

Exercise 1. Trace the Python program by hand with the input 2, 4, 5, 0. What does it print?

Show the answer: It prints 2. The numbers 2 and 4 are even, and 5 is odd. The 0 ends the loop.

Exercise 2. The input is 6, 0, 8, 0. What does the flowchart print?

Show the answer: It prints 1. The first 0 ends the loop, so the 8 is never read.

Exercise 3. You want the program to count odd numbers instead of even numbers. Which line changes in each language, and what does the new test look like?

Show the answer: Python line 4, Java line 9 and C++ line 8 change. The new test is number % 2 != 0, which is true for every number that is not even. This test also works for negative numbers, because -3 % 2 is -1 in Java and C++ and 1 in Python, and both are not 0.

Exercise 4. In the Java file, which line reads the second number, and which box of the flowchart does it belong to?

Show the answer: Line 12, number = in.nextInt(); inside the loop. It belongs to step 7, the return to the input box.

Exercise 5. A student's C++ program prints nothing and keeps running when the input file has no 0 at the end. Which line is the cause, and what is the smallest change that fixes it?

Show the answer: The cause is line 11, std::cin >> number;, together with the loop test on line 7. When the input ends, the read fails and number keeps its last value, which is never 0. The smallest fix is to move the read into the loop test, while (std::cin >> number && number != 0), and delete the other two reads, so the loop ends when the read fails.

Summary

  • A flowchart can be written in any language, and each box maps to one or more lines in the code.
  • Decision boxes become if lines, and loops become while lines. The loop's test is the flowchart's question with the answer flipped.
  • Python uses indentation, Java uses a class and braces, and C++ uses braces and returns 0 from main.
  • Run all three versions on the same input, and check that they agree with a hand trace before you trust them.
  • Each language fails in its own way on bad input. Python and Java stop with a message, and C++ can stop silently or loop forever unless each read is checked.
Check yourself: Flowchart to code (6 questions) →

Stuck? Ask the tutor

Ask about anything in this lesson. Answers are short and use simple words.

Sign in to ask the tutor.

← Comparing two algorithms on paper