CodeGym /Java Course /Module 1. Java Syntax /Example of using a for loop

Example of using a for loop

Module 1. Java Syntax
Level 6 , Lesson 4
Available

1. Using a for loop to count the number of entered lines

Let's write a program that reads 10 lines from the keyboard and displays the number of lines that were numbers. Example:

Code Explanation
Scanner console = new Scanner(System.in);
int count = 0;
for (int i = 0; i < 10; i++)
{
   if (console.hasNextInt())
      count++;
   console.nextLine();
}
System.out.println(count);
Create a Scanner object to read data from the console.
Store the number of numbers in the count variable.
Loop from 0 to 10 (not including 10).

If a number is entered,
then increase count by one.
Read a line from the console and don't save it anywhere.

Display the calculated count on the screen.
Note

If the line contains multiple tokens separated by spaces, and the first of them is a number, then the hasNextInt() method will return true, even if the other tokens are not numbers. That means that our program will work correctly only if just one token is entered on each line.


2. Calculating the factorial using a for loop

Let's write a program that doesn't read in anything, but instead calculates something. Something difficult. For example, the factorial of the number 10.

The factorial of a number n (denoted by n!) is the product of a series of numbers: 1*2*3*4*5*..*n;

Code Explanation
int f = 1;
for (int i = 1; i <= 10; i++)
   f = f * i;
System.out.println(f);
We store the product of numbers in the f variable.
Loop from 1 to 10 (inclusive).
Multiply f by the next number (save the result in f).
Display the calculated amount on the screen.

The starting value is f = 1, because we are multiplying f by the numbers. If f were originally 0, then the product of all the numbers multiplied by 0 would be 0.


3. Using a for loop to draw on the screen

Let's write a program that draws a triangle on the screen. The first line consists of 10 asterisks, the second — 9 asterisks, then 8, etc.

Code Explanation
for (int i = 0; i < 10; i++)
{
   int starCount = 10 - i;
   for (int j = 0; j < starCount; j++)
      System.out.print("*");
   System.out.println();
}
Loop through the lines (there should be 10 lines in total).

Calculate how many asterisks should be in the line.
Loop over the individual asterisks
(display starCount asterisks).
Move the cursor to the next line so the lines are separate.

We need to have two nested loops here: the inner loop is responsible for displaying the correct number of asterisks on a given line.

And the outer loop is needed to loop through the lines.


6
Task
New Java Syntax, level 6, lesson 4
Locked
One large array and two small ones
1. Create an array of 20 numbers. 2. Populate it with values from the keyboard. 3. Create two arrays of 10 numbers each. 4. Copy the large array into the two small ones: half the numbers into the first small array, and the second half into the second small array. 5. Display the second small array, e
6
Task
New Java Syntax, level 6, lesson 4
Locked
Street and houses
1. Create an array of 15 integers. 2. Populate it with values from the keyboard. 3. Let the array index represent the house number. The array value at a particular index represents the number of people living in the corresponding house. Houses with odd numbers are located on one side of the street.
6
Task
New Java Syntax, level 6, lesson 4
Locked
2 arrays
1. Create an array of 10 strings. 2. Create an array of 10 numbers. 3. Enter 10 strings from the keyboard, and put them in an array of strings. 4. In each element of the number array, record the length of the string whose string array index coincides with the current index of the number array. Displ
Comments (7)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Mark Palmer Level 6, Brooklyn, United States
4 January 2024
that was a little tricky, I had to edit my code to remove the first line output of " " (hint: consider your for loop statement 1 limit...)
Juan Carlos Gil Level 20, Medellín, Colombia
18 October 2023
I love that it's not boring exercises.
Juan Carlos Gil Level 20, Medellín, Colombia
18 October 2023
I think I am becoming addicted to java coding.
Serge Level 72, Australia Expert
22 October 2023
Same here.
Omar Yousif Level 7, Iraq Expert
10 November 2023
same
Ahsan, Usman Level 5, Norco, USA
11 December 2023
Same
Campbell, Jackson Level 12, Corona, USA
5 October 2023
First :)