|
| 1 | +--- |
| 2 | +title: Java for Loop |
| 3 | +description: In this tutorial, 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. |
| 4 | +--- |
| 5 | +import NextImage from 'next/image'; |
| 6 | + |
| 7 | +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. |
| 8 | + |
| 9 | +In Java, there are three types of loops. |
| 10 | + |
| 11 | +- for loop |
| 12 | +- while loop |
| 13 | +- do...while loop |
| 14 | + |
| 15 | +This tutorial focuses on the for loop. You will learn about the other type of loops in the upcoming tutorials. |
| 16 | + |
| 17 | +## Java for Loop |
| 18 | + |
| 19 | +Java `for` loop is used to run a block of code for a certain number of times. The syntax of `for` loop is: |
| 20 | + |
| 21 | +```java |
| 22 | +for (initialExpression; testExpression; updateExpression) { |
| 23 | + // body of the loop |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +Here, |
| 28 | + |
| 29 | +1. The **initialExpression** initializes and/or declares variables and executes only once. |
| 30 | +2. The **condition** is evaluated. If the **condition** is `true`, the body of the `for` loop is executed. |
| 31 | +3. The **updateExpression** updates the value of **initialExpression**. |
| 32 | +4. The **condition** is evaluated again. The process continues until the **condition** is `false`. |
| 33 | + |
| 34 | +To learn more about the conditions, visit [Java relational](operators#3-java-relational-operators) and [logical operators](operators#4-java-logical-operators). |
| 35 | + |
| 36 | +## Working of for loop in Java with flowchart |
| 37 | + |
| 38 | +<div> |
| 39 | + |
| 40 | +<NextImage |
| 41 | + className="object-cover object-center shadow-xl" |
| 42 | + alt="Working of Java if statement" |
| 43 | + src={require('@/img/docs/working-of-for-loop.svg').default} |
| 44 | + width={876} |
| 45 | + height={1307} |
| 46 | + layout="responsive" |
| 47 | + blurDataURL={require('@/img/docs/working-of-for-loop.svg').default} |
| 48 | + placeholder="blur" |
| 49 | + loading="lazy" |
| 50 | + quality={100} |
| 51 | + /> |
| 52 | + |
| 53 | +<p align="center" className="text-base text-gray-600 font-medium">Flowchart of Java for loop</p> |
| 54 | + |
| 55 | +</div> |
| 56 | + |
| 57 | + |
| 58 | +### Example 1: Display a Text Five Times |
| 59 | + |
| 60 | +#### Input |
| 61 | + |
| 62 | +```java |
| 63 | +// Program to print a text 5 times |
| 64 | + |
| 65 | +class Main { |
| 66 | + public static void main(String[] args) { |
| 67 | + |
| 68 | + int n = 5; |
| 69 | + // for loop |
| 70 | + for (int i = 1; i <= n; ++i) { |
| 71 | + System.out.println("Java is fun"); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +#### Output |
| 78 | + |
| 79 | +```text |
| 80 | +Java is fun |
| 81 | +Java is fun |
| 82 | +Java is fun |
| 83 | +Java is fun |
| 84 | +Java is fun |
| 85 | +``` |
| 86 | + |
| 87 | +Here is how this program works. |
| 88 | + |
| 89 | +| Iteration | Variable | Condition: i <= n | Action | |
| 90 | +| :-------: | :--------------: | :---------------: | :-------------------------------------------------: | |
| 91 | +| 1st | `i = 1`, `n = 5` | `true` | `Java is fun` is printed `i` is increased to **2**. | |
| 92 | +| 2nd | `i = 2`, `n = 5` | `true` | `Java is fun` is printed `i` is increased to **3**. | |
| 93 | +| 3rd | `i = 3`, `n = 5` | `true` | `Java is fun` is printed `i` is increased to **4**. | |
| 94 | +| 4th | `i = 4`, `n = 5` | `true` | `Java is fun` is printed `i` is increased to **5**. | |
| 95 | +| 5th | `i = 5`, `n = 5` | `true` | `Java is fun` is printed `i` is increased to **6**. | |
| 96 | +| 6th | `i = 6`, `n = 5` | `false` | The loop is terminated. | |
| 97 | + |
| 98 | +### Example 2: Display numbers from 1 to 5 |
| 99 | + |
| 100 | +#### Input |
| 101 | + |
| 102 | +```java |
| 103 | +// Program to print numbers from 1 to 5 |
| 104 | + |
| 105 | +class Main { |
| 106 | + public static void main(String[] args) { |
| 107 | + |
| 108 | + int n = 5; |
| 109 | + // for loop |
| 110 | + for (int i = 1; i <= n; ++i) { |
| 111 | + System.out.println(i); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +#### Output |
| 118 | + |
| 119 | +```text |
| 120 | +1 |
| 121 | +2 |
| 122 | +3 |
| 123 | +4 |
| 124 | +5 |
| 125 | +``` |
| 126 | + |
| 127 | +Here is how the program works. |
| 128 | + |
| 129 | +| Iteration | Variable | Condition: i <= n | Action | |
| 130 | +| :-------: | :--------------: | :---------------: | :---------------------------------------: | |
| 131 | +| 1st | `i = 1`, `n = 5` | `true` | `1` is printed `i` is increased to **2**. | |
| 132 | +| 2nd | `i = 2`, `n = 5` | `true` | `2` is printed `i` is increased to **3**. | |
| 133 | +| 3rd | `i = 3`, `n = 5` | `true` | `3` is printed `i` is increased to **4**. | |
| 134 | +| 4th | `i = 4`, `n = 5` | `true` | `4` is printed `i` is increased to **5**. | |
| 135 | +| 5th | `i = 5`, `n = 5` | `true` | `5` is printed `i` is increased to **6**. | |
| 136 | +| 6th | `i = 6`, `n = 5` | `false` | The loop is terminated. | |
| 137 | + |
| 138 | +### Example 3: Display Sum of n Natural Numbers |
| 139 | + |
| 140 | +```java |
| 141 | +// Program to find the sum of natural numbers from 1 to 1000. |
| 142 | + |
| 143 | +class Main { |
| 144 | + public static void main(String[] args) { |
| 145 | + |
| 146 | + int sum = 0; |
| 147 | + int n = 1000; |
| 148 | + |
| 149 | + // for loop |
| 150 | + for (int i = 1; i <= n; ++i) { |
| 151 | + // body inside for loop |
| 152 | + sum += i; // sum = sum + i |
| 153 | + } |
| 154 | + |
| 155 | + System.out.println("Sum = " + sum); |
| 156 | + } |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +#### Output: |
| 161 | + |
| 162 | +```text |
| 163 | +Sum = 500500 |
| 164 | +``` |
| 165 | + |
| 166 | +Here, the value of `sum` is **0** initially. Then, the for loop is iterated from `i = 1 to 1000`. In each iteration, `i` is added to `sum`and its value is increased by **1**. |
| 167 | + |
| 168 | +When `i` becomes **1001**, the test condition is `false` and `sum` will be equal to `0 + 1 + 2 + .... + 1000`. |
| 169 | + |
| 170 | +The above program to add the sum of natural numbers can also be written as |
| 171 | + |
| 172 | +```java |
| 173 | +// Program to find the sum of natural numbers from 1 to 1000. |
| 174 | + |
| 175 | +class Main { |
| 176 | + public static void main(String[] args) { |
| 177 | + |
| 178 | + int sum = 0; |
| 179 | + int n = 1000; |
| 180 | + |
| 181 | + // for loop |
| 182 | + for (int i = n; i >= 1; --i) { |
| 183 | + // body inside for loop |
| 184 | + sum += i; // sum = sum + i |
| 185 | + } |
| 186 | + |
| 187 | + System.out.println("Sum = " + sum); |
| 188 | + } |
| 189 | +} |
| 190 | +``` |
| 191 | + |
| 192 | +The output of this program is the same as the **Example 3**. |
| 193 | + |
| 194 | +## Java for-each Loop |
| 195 | + |
| 196 | +The Java for loop has an alternative syntax that makes it easy to iterate through [arrays](arrays) and collections. For example, |
| 197 | + |
| 198 | +#### Input |
| 199 | + |
| 200 | +```java |
| 201 | +// print array elements |
| 202 | + |
| 203 | +class Main { |
| 204 | + public static void main(String[] args) { |
| 205 | + |
| 206 | + // create an array |
| 207 | + int[] numbers = {3, 7, 5, -5}; |
| 208 | + |
| 209 | + // iterating through the array |
| 210 | + for (int number: numbers) { |
| 211 | + System.out.println(number); |
| 212 | + } |
| 213 | + } |
| 214 | +} |
| 215 | +``` |
| 216 | + |
| 217 | +#### Output |
| 218 | + |
| 219 | +```text |
| 220 | +3 |
| 221 | +7 |
| 222 | +5 |
| 223 | +-5 |
| 224 | +``` |
| 225 | + |
| 226 | +Here, we have used the **for-each loop** to print each element of the `numbers` array one by one. |
| 227 | + |
| 228 | +In the first iteration of the loop, `number` will be 3, `number` will be 7 in second iteration and so on. |
| 229 | + |
| 230 | +To learn more, visit [Java for-each Loop](enhanced-for-loop). |
| 231 | + |
| 232 | +## Java Infinite for Loop |
| 233 | + |
| 234 | +If we set the test expression in such a way that it never evaluates to false, the for loop will run forever. This is called infinite for loop. For example, |
| 235 | + |
| 236 | +#### Input |
| 237 | + |
| 238 | +```java |
| 239 | +// Infinite for Loop |
| 240 | + |
| 241 | +class Infinite { |
| 242 | + public static void main(String[] args) { |
| 243 | + |
| 244 | + int sum = 0; |
| 245 | + |
| 246 | + for (int i = 1; i <= 10; --i) { |
| 247 | + System.out.println("Hello"); |
| 248 | + } |
| 249 | + } |
| 250 | +} |
| 251 | +``` |
| 252 | + |
| 253 | +Here, the test expression ,`i <= 10`, is never `false` and Hello is printed repeatedly until the memory runs out. |
0 commit comments