-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaWhileLoop.java
More file actions
38 lines (33 loc) · 813 Bytes
/
JavaWhileLoop.java
File metadata and controls
38 lines (33 loc) · 813 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
-------------------The syntax of the while loop is:-----------------------
while (testExpression) {
// body of loop
}
-----------------------------------------------------------------------
import java.util.Scanner;
public class JavaWhileLoop {
public static void main(String[] args) {
int sum = 0;
Scanner input = new Scanner(System.in);
System.out.println("Enter a number");
int number = input.nextInt();
while (number >= 0) {
sum += number;
System.out.println("Enter a number");
number = input.nextInt();
}
System.out.println("Sum = " + sum);
input.close();
}
}
---------------------------------Output------------------------------------
Enter a number
23
Enter a number
1
Enter a number
9
Enter a number
7
Enter a number
-7
Sum = 40