CodeGym/Java Course/Java Syntax Zero/Example of using a for loop

Example of using a for loop

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  to  (inclusive).
Multiply  by the next number (save the result in ).
Display the calculated amount on the screen.110ff

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.


Comments (8)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Knightrider
Level 11 , London, United Kingdom
24 December 2024, 09:30
public class Solution { public static void main(String[] args) { //write your code here for(int i = 0; i < 10; i++){ int count = i + 1; for(int j = 0; j < count ; j++){ System.out.print(8); } System.out.println(); } } }
Mark Palmer
Level 6 , Brooklyn, United States
4 January 2024, 15:44
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, 15:01
I love that it's not boring exercises.
Juan Carlos Gil
Level 20 , Medellín, Colombia
18 October 2023, 14:46
I think I am becoming addicted to java coding.
Serge
Level 84 , Australia
Expert
22 October 2023, 01:32
Same here.
Omar Yousif
Level 7 , Iraq
Expert
10 November 2023, 10:22
same
Ahsan, Usman
Level 5 , Norco, USA
11 December 2023, 23:24
Same
Campbell, Jackson
Level 12 , Corona, USA
5 October 2023, 22:11
First :)