COURSES
MBAData Science & AnalyticsDoctorate Software & Tech AI | ML MarketingManagement
Professional Certificate Programme in HR Management and AnalyticsPost Graduate Certificate in Product ManagementExecutive Post Graduate Program in Healthcare ManagementExecutive PG Programme in Human Resource ManagementMBA in International Finance (integrated with ACCA, UK)Global Master Certificate in Integrated Supply Chain ManagementAdvanced General Management ProgramManagement EssentialsLeadership and Management in New Age BusinessProduct Management Online Certificate ProgramStrategic Human Resources Leadership Cornell Certificate ProgramHuman Resources Management Certificate Program for Indian ExecutivesGlobal Professional Certificate in Effective Leadership and ManagementCSM® Certification TrainingCSPO® Certification TrainingLeading SAFe® 5.1 Training (SAFe® Agilist Certification)SAFe® 5.1 POPM CertificationSAFe® 5.1 Scrum Master Certification (SSM)Implementing SAFe® 5.1 with SPC CertificationSAFe® 5 Release Train Engineer (RTE) CertificationPMP® Certification TrainingPRINCE2® Foundation and Practitioner Certification
Law
Job Linked
Bootcamps
Study Abroad
Master of Business Administration (90 ECTS)Master of Business Administration (60 ECTS)Master in Computer Science (120 ECTS)Master in International Management (120 ECTS)Bachelor of Business Administration (180 ECTS)B.Sc. Computer Science (180 ECTS)MS in Data AnalyticsMS in Project ManagementMS in Information TechnologyMasters Degree in Data Analytics and VisualizationMasters Degree in Artificial IntelligenceMBS in Entrepreneurship and MarketingMSc in Data AnalyticsMS in Data AnalyticsMaster of Science in AccountancyMS in Computer ScienceMaster of Science in Business AnalyticsMaster of Business Administration MS in Data ScienceMS in Information TechnologyMaster of Business AdministrationMS in Applied Data ScienceMaster of Business AdministrationMS in Data AnalyticsM.Sc. Data Science (60 ECTS)Master of Business AdministrationMS in Information Technology and Administrative Management MS in Computer Science Master of Business Administration MBA General Management-90 ECTSMSc International Business ManagementMS Data Science MBA Business Technologies MBA Leading Business Transformation Master of Business Administration MSc Business Intelligence and Data ScienceMS Data Analytics MS in Management Information SystemsMSc International Business and ManagementMS Engineering ManagementMS in Machine Learning EngineeringMS in Engineering ManagementMSc Data EngineeringMSc Artificial Intelligence EngineeringMPS in InformaticsMPS in Applied Machine IntelligenceMS in Project ManagementMPS in AnalyticsMS in Project ManagementMS in Organizational LeadershipMPS in Analytics - NEU CanadaMBA with specializationMPS in Informatics - NEU Canada Master in Business AdministrationMS in Digital Marketing and MediaMS in Project ManagementMaster in Logistics and Supply Chain ManagementMSc Sustainable Tourism and Event ManagementMSc in Circular Economy and Sustainable InnovationMSc in Impact Finance and Fintech ManagementMS Computer ScienceMS in Applied StatisticsMS in Computer Information SystemsMBA in Technology, Innovation and EntrepreneurshipMSc Data Science with Work PlacementMSc Global Business Management with Work Placement MBA with Work PlacementMS in Robotics and Autonomous SystemsMS in Civil EngineeringMS in Internet of ThingsMSc International Logistics and Supply Chain ManagementMBA- Business InformaticsMSc International ManagementMS Computer Science with AIML ConcentrationMBA in Strategic Data Driven ManagementMaster of Business AdministrationMSc Digital MarketingMBA Business and MarketingMaster of Business AdministrationMSc Digital MarketingMSc in Sustainable Luxury and Creative IndustriesMSc in Sustainable Global Supply Chain ManagementMSc in International Corporate FinanceMSc Digital Business Analytics MSc in International HospitalityMSc Luxury and Innovation ManagementMaster of Business Administration-International Business ManagementMS in Computer EngineeringMS in Industrial and Systems EngineeringMSc International Business ManagementMaster in ManagementMSc MarketingMSc Business ManagementMSc Global Supply Chain ManagementMS in Information Systems and Technology with Business Intelligence and Analytics ConcentrationMSc Corporate FinanceMSc Data Analytics for BusinessMaster of Business AdministrationMaster of Business Administration 60 ECTSMaster of Business Administration 90 ECTSMaster of Business Administration 60 ECTSMaster of Business Administration 90 ECTSBachelors in International Management
For College Students

Conditional Statements and If Else - Know the Difference

$$/$$

Welcome to the segment on 'Conditional Statements and If Else'. In the previous session, you saw boolean operations that returned TRUE or FALSE. These logical operations are relevant in everyday life as they are often used to make decisions. For example, if a condition is true — your favorite book is available in a bookstore — you become happy. If it is not, you are not so happy. So, your happiness is dependent on the condition being true or not. In this session, you will learn how to frame and use logical statements in a program.

 

Let us first learn about If Else Statements in Java and also look at some examples where they will be useful.

$$/$$

Video Transcript

 

In real life you might encounter a lot of situations where if you take a certain action then something happens and if you take some other kind of action, then some other thing happens. So for your understanding, just see the switch on placed on you, the switchboard of your room. So what happens when you switch on the switch? Well, the light bulb lights up and otherwise if the switch is off, the light bulb remains off. Similarly, in real life there are several occasions where you need to decide and based on your decision one of the two of the either things might happen. For example, if it is raining outside then you decide to take an umbrella or if it is sunny outside then you don't need to take an umbrella. Moreover, there might be some technical aspects to this as well. For example, when you are trying to log into your email account then either of the two things might happen. Either the password which you provide matches with the password of the email and so you successfully log in. In case you write the wrong password, it throws up an indication and says that you input the wrong password. So in real life there are a lot of situations where one action can have one or two consequences and based on what kind of action is triggered, one or two of those consequences might go ahead. So we can see that if you try to represent this diagrammatically you would say that suppose I have a switchboard with the switch. Now either of these two things might happen when you put it on or you put it off. In case the switch on, the switchboard is on so the bulb lights up and in case it is off, the bulb remains off, the bulb remains dim. So this is a case where one action which is the switch can assume two types of states either the on state or the off state and based on it the bulb lights or the bulb remains dim. Similarly, in case of the email password example you can say that I have a single action here which is my password and it can be of two types. Either it is the correct password or it is the wrong password. In case the password which I provide is the correct password then we say that login is successful or if we provide a wrong password then we say no login. So in this way you can see here that there is one object here which is the switch which can take in two states and based on it two things might happen. Similarly, there is one object here which is the password object and based on whether it is correct or incorrect, two different kinds of things might happen. Now just think about it, how you can write or frame a simple code for this. You will write a code which says that if my switch is on then do something and if my switch is off, then do something else. So is there a way to write these kind of statements in Java? Let us look about. Let us take a look at them and see how it works. So in Java we have a technique to perform these kind of actions. They are known as the if else statements in Java.

 

As the name suggests. You can perhaps intuitively feel that these statements would help us to write conditions like this that if something happens, then do something or else do something else. So the general way to write these statements is that if some condition is true, then do something. I say action one or else I say take some other action.So is there a way to convert these real life examples into this kind of Java code? Well, yes, let us try and do that. So in the first case, it was based on the situation of a switch whether the switch is on or the switch is off. So when I'm converting it into my Java code, I can say something like this that if switch is on, then bulb lights up or else bulb is off. So this is a simple way of writing this kind of if else statement in Java. This is not the exact syntax of writing the IFL statements and we are going to take a look at the exact syntax when we try and run this program. For now, just remember that whenever you have to write these kind of conditions in Java, you first say if and then within curved brackets you write the condition which you want to check. In this case, my condition that if the switch is on so I say that if the switch is on then the bulb lights up or else the bulb is off. Notice that here when I'm writing this else, I don't need to write any other supplementary condition within my brackets. And why is that? Because this else covers every other case besides this case which is switches on. So basically meaning that if the switch is on then the bulb lights up or else if anything else happens besides the switch being on, then the bulb always remains off. So this was a simple way to translate this kind of real life situation into Java code. Similarly, can you think and write about this real life scenario and try to write a simple pseudocode like this for the password example? So just think about it and try to write similar pseudocode for this example. In the question that follows.

 

Video Recap

  • Real-life situations involve actions that lead to certain consequences

  • An example is a switch on a switchboard where turning it on lights up the bulb and turning it off keeps it dim

  • Technical situations such as logging into an email account also have consequences depending on the password entered

  • These situations can be diagrammatically represented with an action leading to two possible states and consequences

  • Java has the if-else statements for writing code that performs actions based on conditions

  • The syntax for writing if-else statements involves checking a condition and performing an action if true, otherwise performing another action

  • An example of writing a switchboard situation in Java code is if the switch is on, then the bulb lights up, otherwise, the bulb remains off

  • Real-life scenarios can be translated into pseudocode in a similar manner for Java programming.

So basically in the video, you saw how If Else Statements are helpful in many everyday situations. Usually in situations in which there are two choices, and only one of those choices need to be executed at any given point of time If Else Statements are used. One of the condition is mentioned inside the If block, and the other condition is mentioned inside the Else block.

 

$$/$$

 

Now that you’ve seen how if else statements work in Java and where they can be utilised, let us now learn how to write simple codes for them.

$$/$$

Now you’ve also seen how to write codes for if..else conditionals. You can download the files provided here and use it for your own better understanding.

 

$$/$$

 

So in the above video, you saw how you can write code for simple If Else statements in Java. The general points to keep in mind are:-

  • If Else statements are used in those cases where only one out of the two code blocks needs to be executed at any given point of time.

  • One of the condition and action is mentioned in the If block

  • The other condition and action are mentioned in the Else block.

     

     

  • In the next lecture, you will learn how to check multiple conditions using something which we call as “Else If” conditionals.