Skip to content

Commit

Permalink
conditional and looping in java
Browse files Browse the repository at this point in the history
  • Loading branch information
riteshgaigawali committed Sep 18, 2023
1 parent b18ef5f commit 66d19d8
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
64 changes: 64 additions & 0 deletions JavaConditionalStatements.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
class JavaConditionalStatements {
public static void main(String args[]) {
int a = 3, b = 20, c = 30;
// Simple if-else statement:
if (a < b) {
System.out.printf("%d is less than %d", a, b);
} else {
System.out.printf("%d is greater than %d", a, b);
}

System.out.println(" ");

// Ladder if-else or if-else if
if (a > b && a > c) {
System.out.printf("%d is the greatest number.", a);
} else if (b > a && b > c) {
System.out.printf("%d is the greatest number", b);
} else {
System.out.printf("%d is the greatest number", c);
}

System.out.println(" ");

// Switch case

int day = 8;

switch (day) {

case 1:
System.out.println("Monday");
break;

case 2:
System.out.println("Tuesday");
break;

case 3:
System.out.println("Wednesday");
break;

case 4:
System.out.println("Thrusday");
break;

case 5:
System.out.println("Friday");
break;

case 6:
System.out.println("Saturday");
break;

case 7:
System.out.println("Sunday");
break;

default:
System.out.println("Please make a valid choice..");
}

}

}
51 changes: 51 additions & 0 deletions JavaLooping.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
public class JavaLooping {
public static void main(String[] args) {
// for loops : program to print all even numbers between 1-50

for (int i = 0; i <= 50; i++) {
if (i % 2 == 0) {
System.out.println(i);
}
}

// while loop :

int num = 0;

while (num <= 5) {
System.out.println("Hello World !");
num++;
}

// do-while loop:

do {
System.out.println("Hello from do-while loop");
num++;
} while (num > 10);

// Nested loop:

for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
System.out.println(i);
}
System.out.println();
}

int weeks = 3;
int days = 7;
int i = 1;
// outer loop
while (i <= weeks) {
System.out.println("Week: " + i);

// inner loop
for (int j = 1; j <= days; ++j) {
System.out.println(" Days: " + j);
}
++i;
}

}
}

0 comments on commit 66d19d8

Please sign in to comment.