If I have a statement block like this:
if (/*condition here*/){ }
else{ }
or like this:
if (/*condition here*/)
else if (/
import java.util.*;
public class JavaApplication21 {
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
System.out.println("You are watching an example of if & else if statements");
int choice, a, b, c, d;
System.out.println(" Enter 1-Addition & 2-Substraction");
int option = obj.nextInt();
switch (option) {
case (1):
System.out.println("how many numbers you want to add.... it can add up to 3 numbers only");
choice = obj.nextInt();
if (choice == 2) {
System.out.println("Enter 1st number");
a = obj.nextInt();
System.out.println("Enter 2nd number");
b = obj.nextInt();
c = a + b;
System.out.println("Answer of adding " + a + " & " + b + " is= " + c);
} else if (choice == 3) {
System.out.println("Enter 1st number");
a = obj.nextInt();
System.out.println("Enter 2nd number");
b = obj.nextInt();
System.out.println("Enter 3rd number");
c = obj.nextInt();
d = a + b + c;
System.out.println("Answer of adding " + a + " , " + b + " & " + c + " is= " + d);
}
case (2):
System.out.println("how many numbers you want to substract.... it can substract up to 3 numbers only");
choice = obj.nextInt();
if (choice == 2) {
System.out.println("Enter 1st number");
a = obj.nextInt();
System.out.println("Enter 2nd number");
b = obj.nextInt();
c = a - b;
System.out.println("Answer of substracting " + a + " & " + b + " is= " + c);
} else if (choice == 3) {
System.out.println("Enter 1st number");
a = obj.nextInt();
System.out.println("Enter 2nd number");
b = obj.nextInt();
System.out.println("Enter 3rd number");
c = obj.nextInt();
d = a - b - c;
System.out.println("Answer of substracting " + a + " , " + b + " & " + c + " is= " + d);
}
default:
System.out.println("no option you have chosen" + option);
}
}
}