0% found this document useful (0 votes)
3 views

Code3

The document contains various code snippets demonstrating different control flow structures in Java, including simple if statements, if-else statements, ternary operators, and nested if-else statements. It also covers loops such as while, for, and do-while, along with loop control statements like break and continue. Each code snippet is designed to illustrate specific programming concepts related to decision-making and iteration.

Uploaded by

Muhammad Daniyal
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)
3 views

Code3

The document contains various code snippets demonstrating different control flow structures in Java, including simple if statements, if-else statements, ternary operators, and nested if-else statements. It also covers loops such as while, for, and do-while, along with loop control statements like break and continue. Each code snippet is designed to illustrate specific programming concepts related to decision-making and iteration.

Uploaded by

Muhammad Daniyal
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
You are on page 1/ 4

Code: C1: simple if statement

1. int number = 10;

2. if (number % 2 == 0) {
3. System.out.println(number + " is even.");
4. }
Code: C2
1. int number = 10;
2. if (number % 2 == 0) {
3. System.out.println(number + " is even.");
4. }
5. if (number % 2 != 0) {
6. System.out.println(number + " is odd.");
7. }
Code: C3: if-else
1. int number = 10;

2. if (number % 2 == 0) {
3. System.out.println(number + " is even.");
4. } else {
5. System.out.println(number + " is odd.");
6. }
Code: C4: ? Operator
1. int number = 10;
2. System.out.println(number + ((number % 2 == 0) ? " is even." : " is odd."));
Code: C5: ? operator examples
1. int y = 20;
2. System.out.println("The number is " + ((y > 15) ? "greater than 15" : "not greater than 15"));
3. // Output: The number is greater than 15
Code: C6: nested if-else
1. import java.util.Scanner;

2. public class GradeCalculator {


3. public static void main(String[] args) {
4. Scanner scanner = new Scanner(System.in);

5. System.out.print("Enter your score: ");


6. int score = scanner.nextInt();

7. if (score >= 90) {


8. System.out.println("Your grade is A.");
9. } else if (score >= 80) {
10. System.out.println("Your grade is B.");
11. } else if (score >= 70) {
12. System.out.println("Your grade is C.");
13. } else if (score >= 60) {
14. System.out.println("Your grade is D.");
15. } else {
16. System.out.println("Your grade is F.");
17. }

18. scanner.close();
19. }
20. }
Code: C7: previous code without using if-else
1. import java.util.Scanner;

2. public class GradeCalculator {


3. public static void main(String[] args) {
4. Scanner scanner = new Scanner(System.in);

5. System.out.print("Enter your score: ");


6. int score = scanner.nextInt();

7. if (score >= 90) {


8. System.out.println("Your grade is A.");
9. }
10. if (score >= 80 && score < 90) {
11. System.out.println("Your grade is B.");
12. }
13. if (score >= 70 && score < 80) {
14. System.out.println("Your grade is C.");
15. }
16. if (score >= 60 && score < 70) {
17. System.out.println("Your grade is D.");
18. }
19. if (score < 60) {
20. System.out.println("Your grade is F.");
21. }

22. scanner.close();
23. }
24. }
Code: C8: nested if-else
1. import java.util.Scanner;

2. public class TicketPriceCalculator {


3. public static void main(String[] args) {
4. Scanner scanner = new Scanner(System.in);

5. System.out.print("Enter your age: ");


6. int age = scanner.nextInt();

7. System.out.print("Do you have a student ID? (yes/no): ");


8. String studentID = scanner.next();

9. double ticketPrice;

10. if (age < 5) {


11. ticketPrice = 0; // Children under 5 years old can enter for free
12. }
13. else if (age >= 65) {
14. ticketPrice = 5; // Senior citizens receive a discounted ticket price
15. }
16. else if (age >= 12 && age <= 18) {
17. if (studentID.equalsIgnoreCase("yes")) {
18. ticketPrice = 5; // Students aged 12-18 with a student ID receive a discounted price
19. }
20. else {
21. ticketPrice = 10; // Students aged 12-18 without a student ID pay regular price
22. }
23. } else {
24. ticketPrice = 15; // Regular ticket price for adults
25. }
26. System.out.println("Your ticket price is: $" + ticketPrice);

27. scanner.close();
28. }
29. }
Code: C9: Loops: ‘while’
1. // Print numbers from 1 to 5 using while loop
2. int j = 1;
3. while (j <= 5) {
4. System.out.println(j);
5. j++;
6. }
Code: C10: Loops: ‘for’
1. // Print numbers from 1 to 5
2. for (int i = 1; i <= 5; i++) {
3. System.out.println(i);
4. }
Code: C11: Loops: ‘do while’
1. int k = 1;
2. do {
3. System.out.println(k);
4. k++;
5. } while (k <= 5);
Code: C12: Loop Control Statements
1. // Using break and continue in a loop
2. for (int i = 1; i <= 10; i++) {
3. if (i == 5) {
4. continue; // Skips the current iteration when i equals 5
5. }
6. if (i == 8) {
7. break; // Exits the loop when i equals 8
8. }
9. System.out.println(i);
10. }

You might also like