Tasks on the "for" loop

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

The following is the detailed explanation of the simplest tasks to help those of you who don’t have any experience in programming and just start to break fresh ground in this field. After looking into them it will be much easier to move on to more complex tasks.

Task No.1

It is necessary to display numbers from 1 to 5 on the screen. You should see the following at the screen:

1 2 3 4 5

Solution:

Comments to the task:

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 it? It means that we need to use a loop. It remains to understand which loop exactly do you need to use. We know for sure that the numbers increase from 1 to 5. Since we know how many times the loop repeats, we can use the for loop.

We write the line with the construction of the for loop:

  • int i = 1; is the initial value of the variable
  • i<=5; is the condition, under which the loop executes
  • i++ is the step of the loop. In this case we used the increment, i.e. the variable will be increased by 1. That is, from 1 to 2, from 2 to 3, from 3 to 4, from 4 to 5.

In order to display the new value after each step of the code we wrote the following line:

Since we need to display the numbers in one line: 1 2 3 4 5, we use System.out.print ();

If we would use System.out.println(); the numbers would be displayed like this:

1

2

3

4

5

Be sure to pay attention to the fact that the number of opening { and closing } brackets should coincide. In this case we have 3 opening and 3 closing brackets.

Task No.2

It is necessary to display numbers from 5 to 1 on the screen. You should see the following at the screen:

5 4 3 2 1

Solution:

Comments to the task:

As we can see from the condition, the numbers increase from 5 to 1 in increments of 1. There is a certain pattern, isn’t it? It means that we need to use a loop. It remains to understand which loop exactly do you need to use. We know for sure that the numbers decrease from 5 to 1. Since we know how many times the loop repeats, we can use the for loop.

We write the line with the construction of the for loop:

  • int i = 5; is the initial value of the variable
  • i>=1; is the condition, under which the loop executes
  • i—is the decrement, i.e. the variable will be decreased by 1. That is, from 5 to 4, from 4 to 3, from 3 to 2, from 2 to 1.

And we display the value of the variable with the help of this line:

We use System.out.print ();, because we need to display the numbers in one line: 5 4 3 2 1

If we would use System.out.println(); the numbers would be displayed like this:

5

4

3

2

1

Once more we remind you to be sure to pay attention to the fact that the number of opening { and closing } brackets should coincide. In this case we have 3 opening and 3 closing brackets.

Task No.3

It is necessary to display the multiplication table by 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:

Comments to the problem:

As we can see from the condition, first we multiply 3 by 1, then by 2, then by 3 and so on to 10. Don’t you think that we need to use a loop? That’s right, we need to use it. It remains to understand which loop exactly do we need to use. Since we know how many times the loop repeats – from 1 to 10 -  we can use the for loop.

We write the line with the construction of the for loop:

  • int i=1; is the initial value of the variable
  • I <= 10; is the condition, under which the loop executes
  • i++ is the increment, i.e. the variable will be increased by 1 – from 1 to 2, from 2 to 3, from 3 to 4 and so on.

Let’s analyze the following line, with the help of which we display the values to the console

We suggest you to take a look at first 3 lines of the problem:

3*1=3

3*2=6

3*3=9

As we can see, there are:

  • text part: “3*” and “=”
  • algorithmic part: it will substitute the values with the help of the for loop. These are all others values.

Therefore, we enclose the whole text part of System.out.println() within brackets. And we get: System.out.println ("3*"+ i + "=" + 3*i);

Task No.4

You need to write a program, where the user can enter any positive integer. The program should be able to sum up all the numbers from 1 to 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 have already understood the essence of the problem. Now it’s time to move on to its solution.

Solution:

Comments to the problem:

First we need to ask the user to enter any positive integer. We will do it with the help of the following line:

We need the scanner in order to read out the number entered by the user. We need to take 3 steps:

Step No.1 – to import the scanner from the java.util library with the help of this line:

Step No.2to declare the scanner

Step No.3to read out the number entered by the user. For this reason, we use the nextInt () method

According to the condition of the problem the numbers will be summed up from 1 to the number entered by the user. Don’t you think that it’s a loop? Yes, this is the loop. And we know in advance how many times the numbers will scale: from 1 to n, where n is the number entered by the user. I.e. we need to use the for loop.

As the condition of the problem implies, we need to sum up the numbers from 1 to n. To do it we declare the variable int sum before the loop and assign the value 0 to it:

The numbers will be summed up in the variable sum with the help of the for loop

Then with the help of this line we display the result to the console

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Publiction author

offline 4 hours

Vertex Academy

Comments: 0Publics: 274Registration: 06-03-2016

Leave a Reply

Your email address will not be published.

Subscribe news

Subscribe to our newsletter we will keep you up-to-date!

Самоучители--узнать детальнее--