Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 55562f1

Browse filesBrowse files
authored
New Loops Addition and Output Formatting
1. changed code for better-formatted output. 2. added do-while and forEach loop example. 3. entry and exit controlled loop specified.
1 parent bd90ed0 commit 55562f1
Copy full SHA for 55562f1

File tree

Expand file treeCollapse file tree

1 file changed

+24
-2
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+24
-2
lines changed

‎JavaLoops.java

Copy file name to clipboardExpand all lines: JavaLoops.java
+24-2Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,45 @@ class JavaLoops {
33

44
public static void main (String args[]) {
55

6-
int a = 1; // Assign a starting value to variable 'a'
6+
int a = 1; // Assign a starting value to variable 'a'
77

88
// Demonstrating Usage of While Loop in Java
9+
// While loop is an entry controlled loop
910
System.out.print("***** Using While Loop ****** ");
10-
11+
System.out.println();
1112
// While the value of Variable a < = 10 do the condition in the loop body { do this }
1213
while( a <= 10) {
1314
System.out.println("Value of a : " + a); // While the loop condition is satisfied print value of 'a'
1415
a++; // Increment the value of 'a' for every iteration
1516
}
1617

1718
// Demonstrating Usage of For Loop in Java
19+
// for loop is an entry controlled loop
1820
// Print the values that are in the range of the condition given in the loop.
1921
System.out.print("***** Using For Loop ****** ");
22+
System.out.println();
2023
for( a = 0 ; a<=10 ; a++ ) {
2124
System.out.println("Value of a : " + a); // While the loop condition is satisfied print value of 'a'
2225
}
2326

27+
// adding do while loop
28+
// exit controlled loop
29+
System.out.print("***** Using Do While Loop ****** ");
30+
a=0;
31+
do{
32+
System.out.println("Value of a : " + a);
33+
a++;
34+
}while (a<=10);
35+
36+
// for each loop example in java
37+
// majorly used with Maps and Sets
38+
System.out.print("***** Output Using forEach Loop ****** ");
39+
System.out.println();
40+
int inp[] = {1,2,3,4,5};
41+
// prints each element in the inp array
42+
for(int i : inp){
43+
System.out.println(i+" ");
44+
}
45+
2446
}
2547
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.