# For Loop

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-LTW2F1MzW6j4SQha6QU%2F-LTW2m7cZwjuF_2zjhAf%2Fimage.png?alt=media\&token=6f41bf89-509f-45fb-8bfd-caff1efa5f60)

<br>

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-LTW2F1MzW6j4SQha6QU%2F-LTW2pPVfe2G89dFODUY%2Fimage.png?alt=media\&token=3d96a3fa-13a1-4fc0-8e17-731772f5e1e9)

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-LTWJlNp8LHHf0L3iKk-%2F-LTWJqgdG5_4QTLNVemI%2Fslide_17.jpg?alt=media\&token=b46563d3-618b-4dc5-9ebe-0706470e7a02)

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-LTW2F1MzW6j4SQha6QU%2F-LTW2xPTG0cpgACKF4P0%2Fforflowchart.PNG?alt=media\&token=e29dd8c3-f14d-4460-bacd-e63d741c5dca)

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-LTW2F1MzW6j4SQha6QU%2F-LTW3DgO25FT8zGS7kTS%2Fimage.png?alt=media\&token=3c784c97-887c-4ecf-95af-bca92be8db8e)

### Examples:

#### **1**) Program to print Even Numbers:

```java
/*
	List Even Numbers Java Example
	This List Even Numbers Java Example shows how to find and list even
	numbers between 1 and any given number.
*/
 
public class ListEvenNumbers {
 
	public static void main(String[] args) {
		
		//define limit
		int limit = 50;
		
		System.out.println("Printing Even numbers between 1 and " + limit);
		
		for(int i=1; i <= limit; i++){
			
			// if the number is divisible by 2 then it is even
			if( i % 2 == 0){
				System.out.print(i + " ");
			}
		}
	}
}
 
/*
Output of List Even Numbers Java Example would be
Printing Even numbers between 1 and 50
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 
*/
```

#### 2) Program to find the sum of natural numbers from 1 to 1000.

```java
// Program to find the sum of natural numbers from 1 to 1000.

class Number {
   public static void main(String[] args) {
      
      int sum = 0;
	   
      for (int i = 1; i <= 1000; ++i) {
         sum += i;     // sum = sum + i
      }
	   
      System.out.println("Sum = " + sum);
   }
}
```

When you run the program, the output will be:

```
Sum = 500500
```

Here, the variable sum is initialized to 0. Then, in each iteration of for loop, variable sum is assigned `sum + i` and the value of i is increased until i is greater than 1000. For better visualization,

```
1st iteration: sum = 0+1 = 1
2nd iteration: sum = 1+2 = 3
3rd iteration: sum = 3+3 = 6
4th iteration: sum = 6+4 = 10
... .. ...

999th iteration: sum = 498501 + 999 = 499500
1000th iteration: sum = 499500 + 1000 = 500500
```

### Nested For Loop:

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-LTW2F1MzW6j4SQha6QU%2F-LTWFJ8dlEpl40p-X2Q6%2Fnestedfor.png?alt=media\&token=759c885e-c340-4a69-877c-cd2f7f631b8b)

```java
package com.test.test1;

public class Clock {
	public static void main(String[] args) {
		for (int hrs = 1; hrs <= 6; hrs++) {
			for (int min = 1; min <= 60; min++) {
				System.out.println(hrs + ":" + min);
			}
		}
	}
}

```

#### Example 1: Program to create a pattern

```
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
```

Here is a program to create the above pattern using nested loops.

```java
class Pattern {
   public static void main(String[] args) {
      
      int rows = 5;
      
      for(int i = 1; i <= rows; ++i)
      {
          for(int j = 1; j <= i; ++j)
          {
             System.out.print(j + " ");
          }
          System.out.println("");
      }
   }
}
```

#### Example 2: Program to create a pattern

```
*
* *
* * *
* * * *
```

Here is a program to create the above pattern using nested loops.

```java
class Pattern {
   public static void main(String[] args) {
      
      int rows = 4;
      
      for(int i = 1; i <= rows; ++i)
      {
          for(int j = 1; j <= i; ++j)
          {
             System.out.print("*" + " ");
          }
          System.out.println("");
      }
   }
}
```

#### Example 3: Program to create a pattern

```
*****
****
***
**
*
```

```java
/*
	Java Pyramid 2 Example
	This Java Pyramid example shows how to generate pyramid or triangle like
	given below using for loop.
	
	*****
	****
	***
	**
	*
*/
 
public class JavaPyramid2 {
 
	public static void main(String[] args) {
		
		for(int i=5; i>0 ;i--){
			
			for(int j=0; j < i; j++){
				System.out.print("*");
			}
			
			//generate a new line
			System.out.println("");
		}
	}
}
 
/*
 
Output of the example would be
*****
****
***
**
*
 
*/

```

### Problems:

#### 1) WAP to print Odd Numbers.

#### 2) WAP to draw the below patterm.

```
*
**
***
****
*****
*****
****
***
**
*
```

#### 3) WAP to draw the below pattern.

```
        *
      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *
```

#### 4) WAP to draw the below pattern.

```
* * * * * * * * *
  * * * * * * *
    * * * * *
      * * *
        *
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gyansetu-java.gitbook.io/core-java/flow-control/for-loop.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
