[Go to site: main page, start]

0% found this document useful (0 votes)
7 views2 pages

Java Conditional and Loop Examples

The document contains multiple Java programs demonstrating various programming concepts. It includes examples of checking multiple conditions with if statements, determining if a number is even or odd, using switch-case with characters, displaying numbers using different loops, and utilizing logical operators in a do-while loop. Additionally, it showcases the use of various methods from the String class.

Uploaded by

sanadeprajwal94
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Java Conditional and Loop Examples

The document contains multiple Java programs demonstrating various programming concepts. It includes examples of checking multiple conditions with if statements, determining if a number is even or odd, using switch-case with characters, displaying numbers using different loops, and utilizing logical operators in a do-while loop. Additionally, it showcases the use of various methods from the String class.

Uploaded by

sanadeprajwal94
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Q: Write a program to check multiple conditions using if statement along with logical

operators.

public class MultipleConditions {


public static void main(String[] args) {
int a = 10, b = 20, c = 30;
if (a < b && b < c) {
[Link]("a is less than b AND b is less than c");
} else {
[Link]("Conditions not satisfied");
}
}
}

Q: Write a program to check no is even or odd.

public class EvenOdd {


public static void main(String[] args) {
int number = 7;
if (number % 2 == 0) {
[Link](number + " is even.");
} else {
[Link](number + " is odd.");
}
}
}

Q: Write a program to check switch-case using character datatype.

public class SwitchChar {


public static void main(String[] args) {
char grade = 'B';
switch (grade) {
case 'A':
[Link]("Excellent!");
break;
case 'B':
[Link]("Well done");
break;
case 'C':
[Link]("Good");
break;
default:
[Link]("Invalid grade");
}
}
}

Q: Write a program to display 1 to 20 numbers using for, while and do-while loop.

public class LoopDemo {


public static void main(String[] args) {
[Link]("For Loop:");
for (int i = 1; i <= 20; i++) {
[Link](i + " ");
}

[Link]("\nWhile Loop:");
int j = 1;
while (j <= 20) {
[Link](j + " ");
j++;
}

[Link]("\nDo-While Loop:");
int k = 1;
do {
[Link](k + " ");
k++;
} while (k <= 20);
}
}

Q: Develop a program to use logical operators in do-while loop.

public class DoWhileLogical {


public static void main(String[] args) {
int x = 0;
do {
if (x % 2 == 0 && x < 10) {
[Link](x + " is even and less than 10");
}
x++;
} while (x <= 15);
}
}

Q: Write a program to show the use of all methods of String class.

public class StringMethods {


public static void main(String[] args) {
String s = " Hello Java ";
[Link]("Length: " + [Link]());
[Link]("Trim: " + [Link]());
[Link]("To Upper: " + [Link]());
[Link]("To Lower: " + [Link]());
[Link]("CharAt 1: " + [Link](1));
[Link]("Substring: " + [Link](1, 5));
[Link]("Replace: " + [Link]("Java", "World"));
[Link]("Contains 'Java': " + [Link]("Java"));
[Link]("Starts with H: " + [Link]().startsWith("H"));
[Link]("Ends with a: " + [Link]().endsWith("a"));
}
}

You might also like