Java Lesson 3: Conditions and Loops

📎 This article includes 1 downloadable practice file ↓

⏱ 1 min read

📘 Java Beginner Course · Lesson 3 of 8

“`java
int[] marks = {78, 91, 45, 33};
int passed = 0;
for (int m : marks) {
if (m >= 40) passed++;
}
String grade = switch (marks[1] / 10) { case 9, 10 -> “A+”; case 7, 8 -> “A”; default -> “B”; };
“`

Strings are compared with .equals(), not ==: region.equals("North").

💡 The enhanced for loop (for (int m : marks)) is clearer than index loops whenever you don’t need the index.

Code for this course follows standard, current syntax; we test JavaScript and PowerShell scripts before publishing, and R/Java/Rust examples are kept to core language features. Tell us if anything fails on your setup.

Practice

Add a grade calculation to Basics.java.

📎 Practice files for this article

Free to use for learning. Files with macros (.bas) are plain text — import them with Alt+F11 → File → Import File, and always test on a copy.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free · AI can be wrong