Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

# Loops

Problem Description

In this problem, we will learn how to use for loop in Java with the help of examples and we will also learn about the working of Loop in computer programming.

In computer programming, loops are used to repeat a block of code. For example, if you want to show a message 100 times, then rather than typing the same code 100 times, you can use a loop.

Java for Loop

Java for loop is used to run a block of code for a certain number of times. The syntax of for loop is:

```for (initialExpression; testExpression; updateExpression) {
// body of the loop
}
```

Here,

• The initialExpression initializes and/or declares variables and executes only once.
• The condition is evaluated. If the condition is true, the body of the for loop is executed.
• The updateExpression updates the value of initialExpression.
• The condition is evaluated again. The process continues until the condition is false.

Example: Display numbers from 1 to 5

```// Program to print numbers from 1 to 5

class Main {
public static void main(String[] args) {

int n = 5;
// for loop
for (int i = 1; i <= n; ++i) {
System.out.println(i);
}
}
}
```

Given an integer n as an input print the numbers from 1 to n in separate lines.

Problem Constraints

1 <= n <= 100

Input Format

A single line of input containing an integer n.

Output Format

Print all the numbers from 1 to n in separate lines.

Example Input

Input 1:

` 5`

Example Output

Output 1:

``` 1
2
3
4
5```
Hints
• Complete Solution

## Discussion

Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College/University Name
Job Title
Job Title
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer