if else conditions of Java

Damitha karunarathne
3 min readApr 1, 2020

Hi readers! I do hope you all read my previous articles about Introduction to Java and Java data types.This is my third article and it is about if else condition in Java.

Following logic conditions are mostly used in if else condition.

  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b
  • Equal to a == b
  • Not Equal to: a != b
  • &&
  • ||

if(codition)

else if(condition)

else

Here i am writing very simple example .

import java.util.Scanner;
public class FirstProgramme{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int num=sc.nextInt();
if(num%2==0)
System.out.println("even");
else
System.out.println("odd");
}
}

Here if the given number is divided by 2 and no any remains it is an even number.Other wise it is an odd number.If we print 4,the answer is given as even.If we print 7 ,the answer is given as odd.Let us see some more examples further.

Let us see how to find the biggest number for given three numbers.

import java.util.Scanner;
public class FirstProgramme{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter number 1");
int num1=sc.nextInt();
System.out.println("Enter number 2");
int num2=sc.nextInt();
System.out.println("Enter number 3");
int num3=sc.nextInt();
if(num1>num2 && num1>num3)
System.out.println("largest number is"+num1);
else if(num2>num3)
System.out.println("largest number is"+num2);
else
System.out.println("largest number is"+num3);
}
}

The answer for above code as bellow.

Let us see how can we do some basic calculations with if else condition.

import java.util.Scanner;
public class FirstProgramme{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter number1");
int num1=sc.nextInt();
System.out.println("Enter number2");
int num2=sc.nextInt();
System.out.println("Enter operator");
char op=sc.next().charAt(0);
if(op=='+')
System.out.println(num1+num2);
else if(op=='-')
System.out.println(num1-num2);
else if(op=='*')
System.out.println(num1*num2);
else if(op=='/')
System.out.println(num1/num2);
else
System.out.println("invalid");
}
}

When we run code,it shows answer as follow.

Usually 3,4,5 months belongs for Spring season.6,7,8 months belongs to summer season.9,10,11 months belongs to autumn.12,1,2 months belong to Winter season.Let us see how can we use these details in to if ,else condition.

import java.util.Scanner;
public class FirstProgramme{
public static void main(String args[]){
Scanner sc=new Scanner(System.in)
System.out.println("Enter month");
int month=sc.nextInt();
if(month==3 || month==4 || month==5)
System.out.println("Spring");
else if(month==6 || month==7 month==8)
System.out.println("Summer");
else if(month==9 || month==10 month==11)
System.out.println("Autumn");
else if(month==12 || month==1 month==2)
System.out.println("Winter");
else
System.out.println("Invalid");
}
}

Now let us see a number guessing game by using if else condition.It is little bit funny,You can also try out.

import java.util.Scanner;
import java.util.Random;
public class FirstProgramme{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
Random rnd=new Random();
int number=rnd.nextInt(10);System.out.println("guess a number");
int guess=sc.nextInt();
if(number==guess)
System.out.println("10 marks .You are lucky winner");
else
System.out.println("0 marks. Loser");
System.out.println("number is"+number);
}
}

When we get random numbers we use

import java.util.Random;(To call library function)

Switch Cases

The switch expression is evaluated once.We use here case to compare the value.break and default are key words.

As examples when we enter the name of the student ,The village of relevant student will come out as result.The code as follow.

import java.util.Scanner;
public class FirstProgramme{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter your name");
String name=sc.nextLine();
switch(name){
case "Damitha":
System.out.println("Bibile");
break;
case "Sepali":
System.out.println("Kandy");
break;
case "Kusala":
System.out.println("Colombo");
break;
default:
System.out.println("Other");
}
}
}

Let us see how can we add, subtract….. 2 numbers with this switch statement.

import java.util.Scanner;
public class FirstProgramme{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter first number");
int num1=sc.nextInt();
System.out.println("Enter second number");
int num2=sc.nextInt();
System.out.println("Enter operator");
char op=sc.next().charAt(0);
swich(op){case'+':
System.out.println(num1+num2);
break;
case'-':
System.out.println(num1-num2);
break;
case'*':
System.out.println(num1*num2);
break;
case'/':
System.out.println(num1/num2);
break;
default:
System.out.println("Other");
}
}
}

Today I just talked little about if,else conditions and switch cases in java programming.In next article I hope to write about operators and about loops,some other basics of Java.If there are any doubts let me know about them.Thank you for your reading.Keep in touch.

--

--