Skip to content

JAVA FOUNDATIONS · JAVA 17

Java control flow: validate, filter and count marks

Combine if, a loop and continue to process marks while making boundary decisions explicit.

Forsk Coding School learning resources · Updated 2026-10-26

What you will learn

  • Trace a loop one iteration at a time
  • Separate invalid data from failing marks
  • Check inclusive and exclusive boundaries

Before you start: Variables, arithmetic and basic main method structure.

Turn a sentence into separate decisions

Consider this rule: accept marks between 0 and 100, and count accepted marks of at least 40 as passes. There are two decisions, not one. First decide whether a mark is valid; then decide whether a valid mark passes. Keeping these decisions separate prevents a value such as 105 from being counted as a pass just because it is greater than 40.

The example loops over an array using the enhanced for statement. Each iteration assigns the next value to mark. The invalid condition uses || because a mark is invalid when it is below 0 or above 100. A mark cannot satisfy both conditions at the same time. Use && for a condition that requires both comparisons to be true.

Trace the counters instead of guessing

Before running the program, build a small trace with columns for the current mark, valid status, validCount and passCount. The two counters begin at zero and change only inside the loop. continue skips the remaining statements of the current iteration, so an invalid mark cannot reach either counter update.

An if statement does not repeat itself. The loop provides repetition and the if statements choose work within each iteration. Use braces even for a one-line branch while learning: adding a second statement later will not accidentally leave it outside the intended condition. Avoid writing a semicolon immediately after an if or for condition.

Test the edges of the rule

The most informative small cases here are -1, 0, 39, 40, 100 and 101. They exercise the transitions between invalid, valid failing and valid passing data. An empty array should produce two zero counters. An array containing only invalid marks should do the same. Neither situation should require an exception or a special fake record.

If the task later asks for an average, divide by validCount rather than the original array length. Also handle validCount equal to zero before division. That next requirement illustrates why it is useful to name and maintain the accepted-record count separately from the passing-record count.

Run the worked example

Save as Main.java. Run javac Main.java, then java Main.

Main.java
public class Main {
    public static void main(String[] args) {
        int[] marks = {39, 40, 100, -1, 101};
        int validCount = 0;
        int passCount = 0;
        for (int mark : marks) {
            if (mark < 0 || mark > 100) {
                continue;
            }
            validCount++;
            if (mark >= 40) {
                passCount++;
            }
        }
        System.out.println(validCount);
        System.out.println(passCount);
    }
}

Expected result

3
2
  1. 39 is valid but does not pass; 40 and 100 are valid passes.
  2. -1 and 101 execute continue before validCount is updated.
  3. The final counters describe accepted marks and accepted passes separately.

Common mistakes to check

  • mark >= 40 alone does not validate the upper bound.
  • Use || for the two invalid ranges; && would never be true here.
  • Keep counters outside the loop so they are not reset on every iteration.

Try it yourself

Count even values in {0, 3, 4, -2, 7}. Treat zero and negative even numbers as even. Print only the final count.

Give me a hint

Use remainder: a value is even when value % 2 == 0.

Compare with one working solution
Main.java
public class Main {
    public static void main(String[] args) {
        int count = 0;
        for (int value : new int[]{0, 3, 4, -2, 7}) {
            if (value % 2 == 0) {
                count++;
            }
        }
        System.out.println(count);
    }
}

Expected result

3

Quick check

Choose an answer, then check the explanation. Your answer stays in this page and is not saved as a student record.

Which marks pass the stated rule?

Keep learning

Explain the result in your own words, change one input and predict the new output. For additional structured assignments, explore the workbook sample before requesting the full pack.

Explore the practice workbook

Reference documentation

Use the official reference to check language details. The explanations, examples and exercises on this page are written for this learning resource.