← Java from Scratch Lesson 18 of 88 · 10 XP

The for loop

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

for (int i = 0; i < 5; i++) { … }        // start; condition; step
for (int i = 10; i > 0; i -= 2) { … }

int[] nums = {3, 1, 4};
for (int x : nums) { … }                  // for-each: over all the elements
for (char c : "IGD".toCharArray()) { … }

Task. Read n and print a triangle of stars n lines high, aligned to the right: line i has n - i spaces and i stars.

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.