Java Programming | Using For Loops

Introduction

This is tutorial for using for loops in java. The program is given below that prints 1 to 10 using for loop. The program is extendable. Go enjoy the program. Lets begin…….

Program for printing 1 to 10 using for loop.

// the name of our class its public
public class ForLoop {
	//void main
		public static void main (String[] args)
		{
			//declare int
			int i;
			//for loops
			for(i=1;i<=10;i++)
			System.out.println(i);
		}
}

Output

1
2
3
4
5
6
7
8
9
10

How does it work

The numbers are directly printed. no need to input.

Extending it

The program can be extended by replacing the 10 by the limit in the program. You can also let user enter the limit that can be saved in a variable and replaces 10 in the program. Go ahead and extend it…..

Explanation.

  1. Declare the class as public
  2. Add the void main function
  3. Add the for loop
  4. Add system.out.println() function to print the number.

At the end.

You learnt creating the Java program for Using For loops. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s