timerring

02 Conditional Statements

January 4, 2024 · 2 min read
Tutorial
Java
If you have any questions, feel free to comment below. Click the block can copy the code.
And if you think it's helpful to you, just click on the ads which can support this site. Thanks!

If Statements #

if-else statements can also contain other if-else statements.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();

        if (a > b) {
            if (a > c)
                System.out.println(a);
            else
                System.out.println(c);
        } else {
            if (b > c)
                System.out.println(b);
            else
                System.out.println(c);
        }
    }
}

Chained if-else statements

Enter a score between 0 and 100.
If it is at least 85, output A;
if it is at least 70 but less than 85, output B;
if it is at least 60 but less than 70, output C;
if it is less than 60, output D.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int s = sc.nextInt();

        if (s >= 85) {
            System.out.println("A");
        } else if (s >= 70) {
            System.out.println("B");
        } else if (s >= 60) {
            System.out.println("C");
        } else {
            System.out.println("D");
        }
    }
}

Common Comparison Operators #

(1) Greater than >
(2) Less than <
(3) Greater than or equal to >=
(4) Less than or equal to <=
(5) Equal to ==
(6) Not equal to !=

All six comparisons can be constructed using just a < b.

  • a > b is equivalent to b < a
  • a>=b is equivalent to !(a<b), meaning a is not less than b
  • a<=b is equivalent to !(b<a), meaning b is not less than a
  • a==b is equivalent to !(a<b)&&!(b<a), similar to the squeeze theorem in calculus
  • a!=b is equivalent to (a<b)||(b<a)

This is also why, when overloading operators in C++, it is enough to overload only <.

Conditional Expressions #

(1) And &&
(2) Or || (3) Not !

Determine whether a year is a leap year with one if statement.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int year = sc.nextInt();

        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
            System.out.println("yes");
        else
            System.out.println("no");
    }
}

switch Statements #

Without a break statement in a switch statement, once the first matching case is found, the statements in each subsequent case execute in order.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int day = sc.nextInt();
        String name;

        switch(day) {
            case 1:
                name = "Monday";
                break;
            case 2:
                name = "Tuesday";
                break;
            case 3:
                name = "Wednesday";
                break;
            case 4:
                name = "Thursday";
                break;
            case 5:
                name = "Friday";
                break;
            case 6:
                name = "Saturday";
                break;
            case 7:
                name = "Sunday";
                break;
            default:
                name = "not valid";
        }
        System.out.println(name);
    }
}

Related readings


<< prev | 01 Variables,... Continue strolling 03 Loops | next >>

If you want to follow my updates, or have a coffee chat with me, feel free to connect with me: