Skip to main content

Introduction to Java Programming

  What is Java? Java is a programming language and a platform. Java is a high-level, robust, object-oriented and secure programming language. Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. Firstly, it was called  Greentalk  and the file extension was  .gt . After that, it was called  Oak . Initially Java was designed for small, embedded systems in electronic appliances like set-top boxes. Platform:  Any hardware or software environment in which a program runs, is known as a platform. Since Java has a runtime environment (JRE) and API(Application Programming Interface), it is called a platform. The principles for creating Java programming were "Simple, Robust, Portable, Platform-independent, Secured, High performance, Multithreaded, Architecture neutral, Object-oriented, Interpreted, and Dynamic". Currently, Java is used in internet programming, mobile devices, g...

9. Java Conditionals (If-else and Switch-case) Statements

 

Java Conditionals

Java conditionals statements execute a particular section of code for a specific condition. If a specific condition is specified then only the particular code under its segment will be executed. Java has the following conditional statements:

  1. Use if to specify a block of code to be executed, if a specified condition is true.
  2. Use else to specify a block of code to be executed, if the same condition is false.
  3. Use else if to specify a new condition to test, if the previous condition is false.
  4. Use switch to specify many alternative blocks of code to be executed.

If-else Statement

  • The if-else statement is used to perform two operations for a single condition.
  • The if-else statement is an extension to the statement using which we can perform two different operations.
  • One operation is for the correctness of that condition and other is for the incorrectness of that condition.

Control flow for if-else statement

import java.util.Scanner;

class IfElse {
    public static void main(String[] arr) {
        System.out.print("Enter your age: ");
        Scanner input = new Scanner(System.in);
        int age = input.nextInt();
        if (age > 18) {
            System.out.println("Yooo!!! You can drive.");
        } else {
            System.out.println("Nooo!!! You can't drive yet.");
        }
    }
}

Outputs :

Enter your age: 20
Yooo!!! You can drive.

Enter your age: 18
Nooo!!! You can't drive yet.

If-else Ladder

  • Instead of using multiple if statements, we can also use else if along with if thus forming an if else-if else ladder.
  • The else-if ladder statement is an extension of the if-else statement.
  • It is used in the scenario where there are multiple cases to be performed for different conditions.
  • Last else is executed only if all the previous conditions fails.
  • The last else block is optional.
  • Using such kind of logic reduces indents.

Control flow for if else-if ladder

import java.util.Scanner;

class IfElseLadder {
    public static void main(String[] arr) {
        System.out.print("Enter your percentage: ");
        Scanner input = new Scanner(System.in);
        double marks = input.nextDouble();
        if (marks >= 90) {
            System.out.println("You got grade A.");
        } else if(marks >= 80){
            System.out.println("You got grade B.");
        } else if(marks >= 65){
            System.out.println("You got grade C.");
        } else if(marks >= 50){
            System.out.println("You got grade D.");
        }else{
            System.out.println("You are fail.");
        }
input.close();
    }
}

Outputs :

Enter your percentage : 95.6
You got grade A.

Enter your percentage : 87.4
You got grade B.

Enter your percentage : 70
You got grade C.

Enter your percentage : 57
You got grade D.

Enter your percentage : 45.9
You are fail.

Switch-Case

  • The switch-case statement is an alternate to if else-if ladder statement.
  • Switch-case statement is allows us to execute multiple operations for the different values of a single variable called switch variable.
  • We can define various statements in the multiple cases for the different values of a single variable.
  • The switch variable can be an integer, character or string type in Java.
  • The case value can be used only inside the switch statement.
  • The break statement in switch case is not necessary. It is optional.
  • If there is no break statement found in the matched case, all the cases will be executed present after the matched case.
  • The default case is also optional. The default case is executed when all the other cases are false.
Control flow for switch-case statement


import java.util.Scanner;

class SwitchCase {
    public static void main(String[] arr) {
        System.out.print("Choose a base among 2, 8, 10, 16: ");
        Scanner input = new Scanner(System.in);
        int base = input.nextInt();
        switch (base) {
            case 2:
                System.out.println("The number system is BINARY.");
                break;
            case 8:
                System.out.println("The number system is OCTADECIMAL.");
                break;
            case 10:
                System.out.println("The number system is DECIMAL.");
                break;
            case 16:
                System.out.println("The number system is HEXADECIMAL.");
                break;
            default:
                System.out.println("The choice is not valid.");
                break;
        }
        System.out.print("Thanks for viewing my Java Code!!!");
input.close();
    }
}

Outputs :

Choose a base among 2, 8, 10, 16 : 2
The number system is BINARY.
Thanks for viewing my Java Code!!!

Choose a base among 2, 8, 10, 16 : 8
The number system is OCTADECIMAL.
Thanks for viewing my Java Code!!!

Choose a base among 2, 8, 10, 16 : 10
The number system is DECIMAL.
Thanks for viewing my Java Code!!!

Choose a base among 2, 8, 10, 16 : 16
The number system is HEXADECIMAL.
Thanks for viewing my Java Code!!!

Choose a base among 2, 8, 10, 16 : 20
The choice is not valid.
Thanks for viewing my Java Code!!!


Previous

Next

Comments