Untitled

 avatar
unknown
plain_text
5 months ago
3.8 kB
2
Indexable
import java.util.Scanner;

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

        // 1. The if statement
        System.out.println("Enter an integer:");
        int num = scanner.nextInt();
        if (num > 0) {
            System.out.println("The number is positive.");
        }

        // 2. The if-else statement
        if (num % 2 == 0) {
            System.out.println("The number is even.");
        } else {
            System.out.println("The number is odd.");
        }

        // 3. Relational operators
        if (num > 10) {
            System.out.println("The number is greater than 10.");
        }

        // 4. Logical operators
        if (num > 5 && num < 20) {
            System.out.println("The number is between 5 and 20.");
        }

        // 5. Compound statements
        if (num < 0) {
            num = -num;
            System.out.println("Converted to positive: " + num);
        }

        // 6. Nested if statements
        if (num > 0) {
            if (num < 50) {
                System.out.println("The number is positive and less than 50.");
            } else {
                System.out.println("The number is positive and 50 or greater.");
            }
        }

        // 7. The switch statement
        System.out.println("Enter a grade (A, B, C, D, F):");
        char grade = scanner.next().charAt(0);
        switch (grade) {
            case 'A':
                System.out.println("Excellent!");
                break;
            case 'B':
                System.out.println("Good job!");
                break;
            case 'C':
                System.out.println("Fair effort.");
                break;
            case 'D':
                System.out.println("Needs improvement.");
                break;
            case 'F':
                System.out.println("Fail.");
                break;
            default:
                System.out.println("Invalid grade.");
        }

        // 8. The conditional operator
        String result = (num % 2 == 0) ? "Even number" : "Odd number";
        System.out.println("Result: " + result);

        // 9. The while loop
        System.out.println("While loop: Counting down from " + num);
        int countdown = num;
        while (countdown > 0) {
            System.out.print(countdown + " ");
            countdown--;
        }
        System.out.println();

        // 10. The do-while loop
        System.out.println("Do-while loop: Counting up to " + num);
        int countUp = 0;
        do {
            System.out.print(countUp + " ");
            countUp++;
        } while (countUp <= num);
        System.out.println();

        // 11. The for loop
        System.out.println("For loop: Counting from 0 to " + num);
        for (int i = 0; i <= num; i++) {
            System.out.print(i + " ");
        }
        System.out.println();

        // 12. Nested loops
        System.out.println("Nested loops: Multiplication table up to " + num);
        for (int i = 1; i <= num; i++) {
            for (int j = 1; j <= num; j++) {
                System.out.print(i * j + "\t");
            }
            System.out.println();
        }

        // 13. Break, continue & exit
        System.out.println("Demonstrating break, continue, and exit.");
        for (int i = 1; i <= 10; i++) {
            if (i == 5) {
                System.out.println("Skipping 5 using continue.");
                continue;
            }
            if (i == 8) {
                System.out.println("Breaking loop at 8.");
                break;
            }
            System.out.println("Number: " + i);
        }

        System.out.println("Exiting the program.");
        System.exit(0); // Terminates the program
    }
}
Editor is loading...
Leave a Comment