File tree Expand file tree Collapse file tree 5 files changed +119
-0
lines changed
src/loopingORIterativeStatements Expand file tree Collapse file tree 5 files changed +119
-0
lines changed Original file line number Diff line number Diff line change
1
+ package loopingORIterativeStatements ;
2
+
3
+ public class WhileLoop {
4
+
5
+ public static void main (String [] args ) {
6
+
7
+ int i =1 ;
8
+
9
+ while (i <=10 ) // condition
10
+ {
11
+ System .out .println (i ); //statement
12
+ i ++; // increment
13
+ }
14
+
15
+
16
+ }
17
+
18
+ }
19
+
20
+ /* condition is true that while loop is executed , condition is false loop will be exited
21
+ * if we are not increment the condition i value always 1 is printed in infinite loop
22
+ *
23
+ *
24
+ *
25
+ * */
26
+
27
+
28
+
Original file line number Diff line number Diff line change
1
+ package loopingORIterativeStatements ;
2
+
3
+ public class WhileLoop1 {
4
+ public static void main (String [] args ) {
5
+ // Hello message 10 times
6
+ int i =1 ;
7
+
8
+ while (i <=10 )
9
+ {
10
+ System .out .println ("Hello" );
11
+ i ++;
12
+ }
13
+ }
14
+
15
+ }
16
+
Original file line number Diff line number Diff line change
1
+ package loopingORIterativeStatements ;
2
+
3
+ public class WhileLoop2 {
4
+
5
+ public static void main (String [] args ) {
6
+
7
+ // print even numbers between 1...10
8
+
9
+ //Approach1:
10
+
11
+ /* int i=2;
12
+ while(i<=10)
13
+ {
14
+ System.out.println(i);
15
+ i+=2; // i=i+2;
16
+ } */
17
+
18
+ //Approach2:
19
+
20
+ int i =2 ;
21
+ while (i <=10 ) // how many times we need to run the loop
22
+ {
23
+ if (i %2 ==0 ) // it is filter the while condition value
24
+ {
25
+ System .out .println (i );
26
+ }
27
+ i ++;
28
+ }
29
+
30
+
31
+ }
32
+
33
+ }
Original file line number Diff line number Diff line change
1
+ package loopingORIterativeStatements ;
2
+
3
+ public class Whileloop4 {
4
+
5
+ public static void main (String args []) {
6
+
7
+ // print 10.......1 in descending order
8
+
9
+ int i =10 ;
10
+ while (i >0 )// (i>=1)
11
+ {
12
+ System .out .println (i );
13
+ i --;
14
+ }
15
+
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ package loopingORIterativeStatements ;
2
+
3
+ public class Whilelopp3 {
4
+
5
+ public static void main (String [] args ) {
6
+ /* odd ..even..odd..even..odd */
7
+
8
+ int i =1 ;
9
+ while (i <=10 )
10
+ {
11
+ if (i %2 ==0 )
12
+ {
13
+ System .out .println (i + "Even" );
14
+ }
15
+ else
16
+ {
17
+ System .out .println (i + "Odd" );
18
+ }
19
+ i ++;
20
+ }
21
+
22
+
23
+ }
24
+
25
+ }
You can’t perform that action at this time.
0 commit comments