|
| 1 | +--- |
| 2 | +title: Java while & do...while loop |
| 3 | +description: In this tutorial, we will learn how to use while and do while loop in Java with the help of examples. |
| 4 | +--- |
| 5 | + |
| 6 | +In computer programming, loops are used to repeat a block of code. For example, if you want to show a message 100 times, then you can use a loop. It's just a simple example; you can achieve much more with loops. |
| 7 | + |
| 8 | +In the previous tutorial, you learned about [Java for loop](./for-loop). Here, you are going to learn about `while` and `do...while` loops. |
| 9 | + |
| 10 | +import { List, ListItemGood } from '@/components/List' |
| 11 | +import { TipInfo } from '@/components/Tip' |
| 12 | + |
| 13 | + |
| 14 | +## Java while loop |
| 15 | + |
| 16 | +Java while loop is used to run a specific code until a certain condition is met. The syntax of the while loop is: |
| 17 | + |
| 18 | +```java |
| 19 | +while (testExpression) { |
| 20 | + // body of loop |
| 21 | +} |
| 22 | +``` |
| 23 | +Here, |
| 24 | + |
| 25 | +1. A `while` loop evaluates the **textExpression** inside the parenthesis `()`. |
| 26 | +2. If the **textExpression** evaluates to `true`, the code inside the `while` loop is executed. |
| 27 | +3. The **textExpression** is evaluated again. |
| 28 | +4. This process continues until the **textExpression** is `false`. |
| 29 | +5. When the **textExpression** evaluates to `false`, the loop stops. |
| 30 | + |
| 31 | +To learn more about the conditions, visit [Java relational](./operators#3-java-relational-operators) and [logical operators](./operators#4-java-logical-operators). |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +## Flowchart of while loop |
| 36 | + |
| 37 | + |
| 38 | +--- |
| 39 | + |
| 40 | +### Example 1: Display Numbers from 1 to 5 |
| 41 | + |
| 42 | +```java |
| 43 | +// Program to display numbers from 1 to 5 |
| 44 | + |
| 45 | +class Main { |
| 46 | + public static void main(String[] args) { |
| 47 | + |
| 48 | + // declare variables |
| 49 | + int i = 1, n = 5; |
| 50 | + |
| 51 | + // while loop from 1 to 5 |
| 52 | + while(i <= n) { |
| 53 | + System.out.println(i); |
| 54 | + i++; |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | + |
| 61 | +#### Output |
| 62 | + |
| 63 | +```text |
| 64 | +1 |
| 65 | +2 |
| 66 | +3 |
| 67 | +4 |
| 68 | +5 |
| 69 | +``` |
| 70 | + |
| 71 | +Here is how this program works. |
| 72 | + |
| 73 | +| Iteration | Variable | Condition: i <= n | Action | |
| 74 | +|-----------|-------------------|-------------------|--------------------------------------------| |
| 75 | +| 1st | `i = 1` `n = 5` | `true` | `1` is printed. `i` is increased to **2**. | |
| 76 | +| 2nd | `i = 2` `n = 5 ` | `true` | `2` is printed. `i` is increased to **3**. | |
| 77 | +| 3rd | `i = 3` ` n = 5 ` | `true` | `3` is printed.`i` is increased to **4**. | |
| 78 | +| 4th | `i = 4` ` n = 5 ` | `true` | `4` is printed. `i` is increased to **5**. | |
| 79 | +| 5th | `i = 5` ` n = 5` | `true` | `5` is printed. `i` is increased to **6**. | |
| 80 | +| 6th | `i = 6` `n = 5` | `false` | The loop is terminated | |
| 81 | + |
| 82 | +### Example 2: Sum of Positive Numbers Only |
| 83 | + |
| 84 | +```java |
| 85 | +// Java program to find the sum of positive numbers |
| 86 | +import java.util.Scanner; |
| 87 | + |
| 88 | +class Main { |
| 89 | + public static void main(String[] args) { |
| 90 | + |
| 91 | + int sum = 0; |
| 92 | + |
| 93 | + // create an object of Scanner class |
| 94 | + Scanner input = new Scanner(System.in); |
| 95 | + |
| 96 | + // take integer input from the user |
| 97 | + System.out.println("Enter a number"); |
| 98 | + int number = input.nextInt(); |
| 99 | + |
| 100 | + // while loop continues |
| 101 | + // until entered number is positive |
| 102 | + while (number >= 0) { |
| 103 | + // add only positive numbers |
| 104 | + sum += number; |
| 105 | + |
| 106 | + System.out.println("Enter a number"); |
| 107 | + number = input.nextInt(); |
| 108 | + } |
| 109 | + |
| 110 | + System.out.println("Sum = " + sum); |
| 111 | + input.close(); |
| 112 | + } |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +#### Output |
| 117 | + |
| 118 | +```text |
| 119 | +Enter a number |
| 120 | +25 |
| 121 | +Enter a number |
| 122 | +9 |
| 123 | +Enter a number |
| 124 | +5 |
| 125 | +Enter a number |
| 126 | +-3 |
| 127 | +Sum = 39 |
| 128 | +``` |
| 129 | + |
| 130 | +In the above program, we have used the [Scanner class](./scanner) to take input from the user. Here, `nextInt()` takes integer input from the user. |
| 131 | + |
| 132 | +The `while` loop continues until the user enters a negative number. During each iteration, the number entered by the user is added to the `sum` variable. |
| 133 | + |
| 134 | +When the user enters a negative number, the loop terminates. Finally, the total sum is displayed. |
| 135 | + |
| 136 | +## Java do...while loop |
| 137 | + |
| 138 | +The `do...while` loop is similar to while loop. However, the body of `do...while` loop is executed once before the test expression is checked. For example, |
| 139 | +```java |
| 140 | +do { |
| 141 | + // body of loop |
| 142 | +} while(textExpression); |
| 143 | +``` |
| 144 | +Here, |
| 145 | + |
| 146 | +1. The body of the loop is executed at first. Then the **textExpression** is evaluated. |
| 147 | +2. If the **textExpression** evaluates to `true`, the body of the loop inside the `do` statement is executed again. |
| 148 | +3. The **textExpression** is evaluated once again. |
| 149 | +4. If the **textExpression** evaluates to `true`, the body of the loop inside the `do` statement is executed again. |
| 150 | +5. This process continues until the **textExpression** evaluates to false. Then the loop stops. |
| 151 | + |
| 152 | +## Flowchart of do...while loop |
| 153 | + |
| 154 | + |
| 155 | +Let's see the working of `do...while` loop. |
| 156 | + |
| 157 | +--- |
| 158 | + |
| 159 | +### Example 3: Display Numbers from 1 to 5 |
| 160 | + |
| 161 | +```java |
| 162 | +// Java Program to display numbers from 1 to 5 |
| 163 | + |
| 164 | +import java.util.Scanner; |
| 165 | + |
| 166 | +// Program to find the sum of natural numbers from 1 to 100. |
| 167 | + |
| 168 | +class Main { |
| 169 | + public static void main(String[] args) { |
| 170 | + |
| 171 | + int i = 1, n = 5; |
| 172 | + |
| 173 | + // do...while loop from 1 to 5 |
| 174 | + do { |
| 175 | + System.out.println(i); |
| 176 | + i++; |
| 177 | + } while(i <= n); |
| 178 | + } |
| 179 | +} |
| 180 | +``` |
| 181 | +#### Output |
| 182 | + |
| 183 | +```text |
| 184 | +1 |
| 185 | +2 |
| 186 | +3 |
| 187 | +4 |
| 188 | +5 |
| 189 | +``` |
| 190 | + |
| 191 | +Here is how this program works. |
| 192 | + |
| 193 | +| Iteration | Variable | Condition: i <= n | Action | |
| 194 | +|-----------|-------------------|-------------------|--------------------------------------------| |
| 195 | +| | `i = 1` `n = 5` | not checked | `1` is printed. `i` is increased to **2**. | |
| 196 | +| 1st | `i = 2` `n = 5 ` | `true` | `2` is printed. `i` is increased to **3**. | |
| 197 | +| 2nd | `i = 3` ` n = 5 ` | `true` | `3` is printed.`i` is increased to **4**. | |
| 198 | +| 3rd | `i = 4` ` n = 5 ` | `true` | `4` is printed. `i` is increased to **5**. | |
| 199 | +| 4th | `i = 5` ` n = 5` | `true` | `5` is printed. `i` is increased to **6**. | |
| 200 | +| 5th | `i = 6` `n = 5` | `false` | The loop is terminated | |
| 201 | + |
| 202 | +--- |
| 203 | + |
| 204 | +### Example 4: Sum of Positive Numbers |
| 205 | + |
| 206 | +```java |
| 207 | +// Java program to find the sum of positive numbers |
| 208 | +import java.util.Scanner; |
| 209 | + |
| 210 | +class Main { |
| 211 | + public static void main(String[] args) { |
| 212 | + |
| 213 | + int sum = 0; |
| 214 | + int number = 0; |
| 215 | + |
| 216 | + // create an object of Scanner class |
| 217 | + Scanner input = new Scanner(System.in); |
| 218 | + |
| 219 | + // do...while loop continues |
| 220 | + // until entered number is positive |
| 221 | + do { |
| 222 | + // add only positive numbers |
| 223 | + sum += number; |
| 224 | + System.out.println("Enter a number"); |
| 225 | + number = input.nextInt(); |
| 226 | + } while(number >= 0); |
| 227 | + |
| 228 | + System.out.println("Sum = " + sum); |
| 229 | + input.close(); |
| 230 | + } |
| 231 | +} |
| 232 | +``` |
| 233 | +#### Output 1 |
| 234 | + |
| 235 | +```text |
| 236 | +Enter a number |
| 237 | +25 |
| 238 | +Enter a number |
| 239 | +9 |
| 240 | +Enter a number |
| 241 | +5 |
| 242 | +Enter a number |
| 243 | +-3 |
| 244 | +Sum = 39 |
| 245 | +``` |
| 246 | +Here, the user enters a positive number, that number is added to the `sum` variable. And this process continues until the number is negative. When the number is negative, the loop terminates and displays the sum without adding the negative number. |
| 247 | + |
| 248 | +#### Output 2 |
| 249 | + |
| 250 | +```text |
| 251 | +Enter a number |
| 252 | +-8 |
| 253 | +Sum is 0 |
| 254 | +``` |
| 255 | +Here, the user enters a negative number. The test condition will be `false` but the code inside of the loop executes once. |
| 256 | + |
| 257 | +## Infinite while loop |
| 258 | + |
| 259 | +If **the condition** of a loop is always `true`, the loop runs for infinite times (until the memory is full). For example, |
| 260 | + |
| 261 | +```java |
| 262 | +// infinite while loop |
| 263 | +while(true){ |
| 264 | + // body of loop |
| 265 | +} |
| 266 | +``` |
| 267 | +## Infinite do...while loop |
| 268 | + |
| 269 | +Here is an example of an infinite `do...while` loop. |
| 270 | + |
| 271 | +```java |
| 272 | +// infinite do...while loop |
| 273 | +int count = 1; |
| 274 | +do { |
| 275 | + // body of loop |
| 276 | +} while(count == 1) |
| 277 | +``` |
| 278 | +In the above programs, the **textExpression** is always `true`. Hence, the loop body will run for infinite times. |
| 279 | + |
| 280 | +--- |
| 281 | + |
| 282 | +## for and while loops |
| 283 | + |
| 284 | +The `for` loop is used when the number of iterations is known. For example, |
| 285 | + |
| 286 | +```java |
| 287 | +for (let i = 1; i <=5; ++i) { |
| 288 | + // body of loop |
| 289 | +} |
| 290 | +``` |
| 291 | + |
| 292 | +And `while` and `do...while` loops are generally used when the number of iterations is unknown. For example, |
| 293 | + |
| 294 | +```java |
| 295 | +while (condition) { |
| 296 | + // body of loop |
| 297 | +} |
| 298 | +``` |
0 commit comments