For loop

New Java Syntax
Level 4 , Lesson 5
Available

A lecture snippet with a mentor as part of the Codegym University course. Sign up for the full course.


"I want to tell you about one more loop. The for loop. It's just another way to express a while loop, just more compact and convenient (for programmers). Here are some examples:"

while
int i = 3;
while (i >= 0)
{
    System.out.println(i);
    i--;
}
for

for (int i = 3; i >= 0; i--) { System.out.println(i); }
while
int i = 0;
while (i < 3)
{
    System.out.println(i);
    i++;
}
for

for (int i = 0; i < 3; i++) { System.out.println(i); }
while
boolean isExit = false;
while (!isExit)
{
    String s = buffer.readLine();
    isExit = s.equals("exit");
}
for

for (boolean isExit = false; !isExit; ) { String s = buffer.readLine(); isExit = s.equals("exit"); }
while
while (true)
    System.out.println("C");
for
for (; true; )
    System.out.println("C");
while
while (true)
{
    String s = buffer.readLine();
    if (s.equals("exit"))
        break;    
}
for
for (; true; )
{
    String s = buffer.readLine();
    if (s.equals("exit"))
        break;    
}

"Eh?"

"These loops are equivalent. A while loop contains a single condition in the parentheses, but there are three elements in a for loop statement. But the compiler turns a for loop into an equivalent while loop."

"The first expression in a for loop (highlighted in green) is executed once before the loop begins."

"The second expression is evaluated every time before the loop body is executed. This is like the condition in a while loop."

"The third expression is evaluated after every execution of the loop body."

"Why do we need one more loop? Everything is perfectly clear with the while loop."

"It's for programmers' convenience. Loops are very common in programming. It's helpful to have a single line contain information on the loop counter's initial value, the termination condition, and the increment expression."

4
Task
New Java Syntax, level 4, lesson 5
Locked
Multiplication table
Use any kind of loop to display a 10x10 multiplication table. Separate the numbers with spaces. Example output: 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 7
Comments (41)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
theLand Level 6, Tampa, United States
27 August 2024
The best way to solve this is through a nested for loop.
BaDZeR Level 6, Doha, Qatar
27 January 2025
Yup, here was my solution:

public class Solution {
    public static void main(String[] args) {
        for(int i = 1; i < 11; i++){
            for(int j = 1; j < 11; j++){
                System.out.print(i*j + " ");
            }
            System.out.println();
        }
    }
}
xrontux Level 10, Singapore, Singapore
8 July 2024
I did it this way. 1. Outer Loop: It runs from 1 to 10 and sets up the current number, i, for each line. 2. Inner Loop: For each j, it starts at i and keeps adding i until it reaches i * 10 (increment of i). It prints these numbers on the same line. Once the inner loop is finished, i used System.out.println(); to go on to the next row
Luke Level 5, Saint Paul, United States
18 February 2024
Was able to solve this using a single "for" loop and a single (albeit rather long) print statement in the loop body. Makes the user have to scroll horizontally to see the entire code (which I hate) but is probably the simplest solution. Create a for loop that iterates 10 times using "i" as your iterable. In the body of the loop your print statement will print out i*1 + " " + i*2 + " " + i*3 ...i*10. Use + " " to place spaces in between each value. In the first iteration of the loop i = 1, so the print statement prints out. 1*1, 1*2, 1*3...1*10. Which is 1,2,3...10 In the second iteration of the loop i = 2, so it now prints 2*1, 2*2, 2*3...2*10 which is 2, 4, 6, 20. This continues through i = 10 and then ends.
12 July 2023
code solution almost consists 8 lines of code in main method.✨I just used cycle inside of cycle
J Deezy Level 22, Seattle, United States
21 June 2023
One While loop and 3 if statements! Boom. Programming!
Hubert Matlak Level 34, Poland, Poland
14 December 2022
I used nested for-loops and got a 10x10 multiplication table, but the programme still says "The program should display a 10x10 multiplication table."
Anonymous #11224824 Level 10, Ukraine, Ukraine Expert
2 January 2023
may be you have a space, after number in the end
Anonymous #11073930 Level 6, Manchester, United States
11 September 2022
why are we adding the "" in this statement? System.out.print(i * j + " ");
Winston Level 9, Netherlands
27 October 2022
Cuz to create some room between the different number like in the example.
calibers16 Level 6, Knowhere, United States
18 August 2022
This exercise was great. It took me over a day of pondering how to get this working, but when I did it felt AMAZING. I discovered afterwards that my while loop and for loop solution wasn't as optimized as the "official solution," but I learned a lot from both.
Anonymous #10960894 Level 19, Arnold, United Kingdom
17 March 2022
Yup, loop in a loop with a break in the right spot equals solution in 10 lines of code including closing brackets
Roman Bortnyk Level 10, Italy
23 February 2022
I used loop in the loop. Did you?
Marko Ressler Level 7, Leoben, Austria
7 March 2022
I used for loop 10 times, if there is a more elegant solution i would sure love to know :)
Krig Raseri Level 38, Dallas, United States
6 August 2022
Two for loops, and two print statements.