ROAD TO ANDROID DEVELOPER-Season 1-JAVA-Conditional Statements | by Tharani balan SK | Jan, 2024

Tharani balan SK
Java Logo

Hi all, I am back again with a new article in the Android series, we are going to see about conditional statements. If you haven’t checked my previous article on Operators, Please, I urge you to check it out before coming here especially if you are new to programming. I have provided the link at the end of this article, check it out.

Conditional Statements are one of the most significant parts of any programming language. If you see a code of any major application you will find atleast one If or If-else statements there. They are necessary to maintain the control flow a program. Basically its like saying…
If you have this, then do this else do something else or If something is there, go to that place else remain in the sameplace.

I know this sounds kinda lame right? lol fret not, let me explain with a real time scenario. Let’s say you lost your keys and you are so sure that it will either be in the cupboard or in your computer desk or in your dress pockets. Now let’s try to write this scenario in the form of a code shall we?

// let's say the keys are in your pockets and you are searching for it in all these 
// three places. Let's see how your search flow goes...

Class Lostkeys
public static void main(String[] args)
String searchtarget = "Keys";
String cupboard = "Dresses and books";
String computerdesk = "computer and penstand";
String dresspockets = "Keys";

if(cupboard==searchtarget)
System.out.println("Yay!! my keys are in the cupboard!!");

else if(computerdesk==searchtarget)
System.out.println("Yay!! my keys are on my computerdesk!!");

else if(dresspockets==searchtarget)
System.out.println("Yay!! my keys are in my pockets...Silly me!!");

else
System.out.println("MAAMA!! I LOST MY KEYS!!!(crying)");


// In this code first you are checking if the things stored in your cupboard is
// same as the one you are searching, If yes, then "Yay!! my keys are in the cupboard!!"
// is printed, if not, you are continuing your check in the computerdesk in similar
// manner if not in computerdesk then you are going to your pockets and yay you have
// got your keys. If the keys are not found in any of these places, then at last the
// else part will get executed and "MAAMA!! I LOST MY KEYS!!!(crying)" will get printed

I hope the above code is pretty much straightforward. What we did above is known as control flow i.e. we are controlling in which way the code flow should go using different types of conditional statements. The example above is just one type there are other types as well.

TYPES OF CONDITIONAL STATEMENTS :

  • If- statement : If statement functions in a way such that ‘If’ a certain condition is true, then the code under it will get executed.
class ifstatement

public static void main(String[] args)

int num = 5;
if (num>0) //checks if number stored in variable "num" is greater than 0,
// iF so it executes the code under it.

System.out.println(num + "is a positive number");//5 is greater than 0 so

//"5 is a positive number" will get printed


  • If-else statement : In the above code we used only the if statement i.e. we did not mention what the code should do when the if condition is false…so that’s where else comes in. When if condition is false, then else part gets executed.
class ifelsestatement

public static void main(String[] args)

int num = 5;
if (num>10) //checks if number stored in variable "num" is greater than 0,
// iF so it executes the code under it.

System.out.println(num + "is a positive number");//5 is not greater than 10 so

//this part of code under if condition will not get executed and
//the control goes to else statement and else part will get executed

else
System.out.println(num+"is not greater than 10");
//"5 is not greater than 10" will get printed


  • If…elseif…else Statement : This is exactly what we saw above in the lost keys example. This statement is used when there is more than one condition to be checked. In the lost keys example there were three conditions to be checked they were whether the keys were in the cupboard or computerdesk or the pockets but if all the three conditions are false i.e. if the keys were not found in any of the three places, then the else part will get executed.
  • Switch-case : This conditional statement is like a cutdown or you could even say, an easier version of the if- elseif-else statement. This is particularly useful when we are unsure of what value is stored in a particular variable. When you are working in large-scale applications, you will see that there are lots and lots of data getting processed every second. If it’s a small 20 to 50-line code then you might be able to find the value stored in a variable but if it’s more than 100, and you are looking through 1000s of lines of code and you want to process a variable but you don’t know what value is coming to it via API response(Don’t worry this is a topic for another day another time, for now just understand that it’s a response that comes via internet and gets stored in a variable), then switch case will be very much helpful.
class switchcasestatement
public static void main(String[] args)

int num = 3;
switch (num)

case 1: //checking if value stored in num is 1. If so "Number is 1" is
//printed
System.out.println("Number is 1");
break;
case 2:
System.out.println("Number is 2"); //similarly for 2
break;
case 3:
System.out.println("Number is 3"); //since value stored in num is 3.
//"Number is 3" gets printed
break;
default:
System.out.println("No numbers Matched!");// If no numbers match, this
//part gets executed


What is break in the above code?
break
is used to stop the execution and come out of the switch statement. Let’s say there are 10 cases to be checked and the cortrect one matches at case 5, then we should just stop checking right? If we found the right case then obviously the rest of the cases are going to be false, then why waste time? That’s where break comes in.

  • > default is like else part.
    -> case is like if-elseif part and switch is used for switching through the cases.

So this pretty much sums up the conditional statements, hope it will be useful for you guys. In the next article, we will discuss looping statements, so until then CIAO !! 🙂

If you missed the previous article on Java Operators, check out the link below

https://medium.com/@TechTiger/road-to-android-developer-season-1-java-operators-dd55de5a90b3

Incase of any queries, reach out to me on,

Email : [email protected]
Instagram : name_is_tharani_

Disclaimer : The technical stuffs I am mentioning in this series might be factually wrong at some places or it might be short on content so I would recommend to read my stuff just as a refresher or for getting started as a beginner. For more detailed stuff follow other sites out there on the internet. Thank you

Next Post

How To Scale Your Amazon Ads

Amazon Ads have become a vital aspect of online advertising for businesses of all sizes. With the increasing competition on the platform, it is crucial for sellers to understand how to effectively scale their Amazon Ads for maximum results. In this article, we will explore the basics of Amazon Ads, […]
How To Scale Your Amazon Ads

You May Like