Exercises for the "For" Loop
Below, you will see a detailed explanation of simple exercises to help those of you who don’t have much programming experience and are just taking your first steps in the programming world. After looking at these exercises, it will be much easier to move on to more complex exercises.
Exercise No. 1
It is necessary to display numbers from 1 to 5 on the screen. You want to see the following on the screen:
1 2 3 4 5
Solution:
1 2 3 4 5 6 7 8 |
class Test { public static void main(String[] args) { for (int i=1; i <= 5; i++) { System.out.print (i + " "); } } } |
Commentary:
As we can see from the condition, the numbers increase from 1 to 5 in increments of 1. There is a certain pattern, isn’t there? That means that we need to use a loop. But what loop do we need to use? We know for sure that the numbers increase 4 times (from 1 to 5), and since we know how many times the loop repeats, we use the "for" loop.
We write the line with the construction of the "for" loop:
1 |
for (int i=1; i <= 5; i++) { |
- int i = 1; is the initial value of the variable
- i<=5; is the condition under which the loop executes
- i++ is the action which will be executed by the loop. In this example, we use an increment (i.e. the variable will increase by 1: from 1 to 2, 2 to 3, 3 to 4, and 4 to 5)
In order to display the new value after each step of the code, we write the following line:
1 |
System.out.print (i + " "); |
Since we need to display the numbers in one line: 1 2 3 4 5, we use System.out.print ();
If we had used System.out.println(); the numbers would have been displayed like this:
1
2
3
4
5
Notice that we need to have the same number of opening { and closing } brackets. In our example, we have 3 opening and 3 closing brackets.
Exercise No. 2
It is necessary to display the numbers from 1 to 5 in reverse order on the screen. You want to see the following on the screen:
5 4 3 2 1
Solution:
1 2 3 4 5 6 7 8 |
class Test { public static void main(String[] args) { for (int i=5; i>=1; i--) { System.out.print (i + " "); } } } |
Commentary:
As we can see from the condition, the numbers decrease from 5 to 1 in increments of 1. There is a certain pattern, isn’t there? That means that we need to use a loop. But what loop do we need to use? We know for sure that the numbers decrease 4 times (from 5 to 1) and, since we know how many times the loop repeats, we use the "for" loop.
We write the line with the construction of the "for" loop:
1 |
for (int i=5; i>=1; i--) { |
- int i = 5; is the initial value of the variable
- i>=1; is the condition, under which the loop executes
- i— is a decrement (i.e. the variable will decrease by 1: from 5 to 4, 4 to 3, 3 to 2, and 2 to 1)
And we display the value of the variable with the help of this line:
1 |
System.out.print (i + " "); |
We use System.out.print ();, because we need to display the numbers in one line: 5 4 3 2 1
If we had used System.out.println(); the numbers would have been displayed like this:
5
4
3
2
1
Once again, notice how we need to have the same number of opening { and closing } brackets. In this example, we have 3 opening brackets and 3 closing brackets.
Exercise No. 3
It is necessary to display a multiplication table of the number 3 on the screen:
3*1=3
3*2=6
3*3=9
3*4=12
3*5=15
3*6=18
3*7=21
3*8=24
3*9=27
3*10=30
Solution:
1 2 3 4 5 6 7 8 |
class Test { public static void main(String[] args) { for (int i=1; i<=10; i++) { System.out.println ("3*" + i + "=" + 3*i); } } } |
Commentary:
As we can see from the condition, we first multiply 3 by 1, then by 2, then by 3, etc, etc., until 10. Should we use a loop? That’s right, we need to. But what loop do we need to use? Since we know how many times the loop repeats (10 times, from 1 to 10) we can use the "for" loop.
We write the line with the construction of the "for" loop:
1 |
for (int i=1; i<=10; i++) { |
- int i=1; is the initial value of the variable
- I <= 10; is the condition under which the loop executes
- i++ is an increment (i.e. the variable will increase by 1: from 1 to 2, 2 to 3, 3 to 4 and so on)
Let’s take a look at the following line, which helps us display the values to the console:
1 |
System.out.println ("3*" + i + "=" + 3*i); |
Look at the first 3 lines of the problem:
3*1=3
3*2=6
3*3=9
As we can see, there is:
- a text part: “3*” and “=”
- an algorithmic part: it will substitute the values with the help of the "for" loop.
Because some parts are text and some are algorithmic, we surround the entire text part in System.out.println() with brackets. So we have to write: System.out.println ("3*"+ i + "=" + 3*i);
Exercise No. 4
You need to write a program in which a user can enter any positive integer. This program needs to be able to calculate the sum of all the numbers, starting from 1 until the number entered by the user.
For example:
- If the user enters the number 3, the program should calculate the sum of the numbers from 1 to 3 (i.e. 1+2+3) and display the answer: 6.
- If the user enters the number 5, the program should calculate the sum of the numbers from 1 to 5 (i.e. 1+2+3+4+5) and display the answer: 15.
I think you understand the task. Let's move on to the solution.
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Scanner; class Test { public static void main(String[] args) { System.out.print("Enter any positive integer: "); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int sum = 0; for (int i=1; i<= n; i++){ sum = sum + i; } System.out.println (sum); } } |
Commentary:
First, we need to ask the user to enter any positive integer. We do this with the help of the following line:
1 |
System.out.print("Enter any positive integer: "); |
We want the scanner to read out the number that the user entered. Therefore, we need to take 3 steps:
Step No. 1 – import the scanner from the java.util library with the help of this line:
1 |
import java.util.Scanner; |
Step No. 2 – declare the scanner
1 |
Scanner sc = new Scanner(System.in); |
Step No. 3 – read out the number that the user entered. To do this, we use the nextInt () method.
1 |
int n = sc.nextInt(); |
According to the condition of the task, the computer will calculate the sum of the numbers from 1 to the number entered by the user. Does that sound like a loop? Yes, it's a loop. And since we know how many times the numbers will increase (from 1 to n, where n is the number entered by the user), then we need to use the "for" loop.
We need to sum up the numbers from 1 to n, and in order to do this, we declare the variable "int sum" before the loop and assign the value "0" to it:
1 |
int sum = 0; |
The numbers will be summed up in the "sum" variable with the help of the "for" loop:
1 2 3 4 |
int sum = 0; for (int i=1; i<= n; i++){ sum = sum + i; } |
Then with the help of this line, we display the result to the console:
1 |
System.out.println (sum); |