image
If Else Statement
If Else Statement
Difficulty
Beginner
Duration
3h 15m
Students
118
Ratings
4.6/5
Description

In this course, we look at control flow statements in the Java programming language. 

Learning Objectives

  • Decision-making statements
  • Looping statements
  • Branching statements 

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

Prerequisites

  • No prior knowledge is required about the Java programming language.
  • Basic computer knowledge
Transcript

Decision-making structures have one or more conditions to be evaluated or tested by the program. The diagram shows if some condition occurs, check the condition. If the condition is true, execute conditional code. If the condition is false,  pass conditional code and continue. There are two types of decision making statements in Java. They are if statements and switch statements. The if statement is used to test the condition, it checks Boolean conditions, true or false. There are various types of the if statement in java, we can classify these statements as if statements, if-else statements, if-else if latter statements and nested if statements. Let's take a close look at the if statement and if-else statement syntax. 

First, Java if statement tests the condition. If the condition is true, it executes the if block. Second, the Java if-else statement also tests the condition. If the condition is true, it executes the if block, otherwise else block is executed. Let's make some examples with the if-else statement. In exercise project, right click on the source folder and select new class, specify package name as decisionmaking and class name as IfElseStatement and select the checkbox for the main method. We'll use the scanner class for getting input from the user in Java. First, we need to import the scanner class. If you place the mouse pointer on the scanner word, import options are shown. 

We choose the import scanner class in Java.util package to import the scanner class. We show a message to enter a number by using the print method and we declare a variable number with int type and assign the next int method of scanner class to this number. Let's start to write if statement. In the if statement we check whether this number is even or odd. If a number is divisible by two, then it is even otherwise it's odd. We use the modulus operator to find the remainder. So, I will write number percent sign two is equal to zero. If the remainder is zero then the number is even. Let's print this statement to the console, the number is even. Finally, we have to close the scanner object by using the close method. 

Let's run the code. In the console screen, we enter eight. First, it divides the eight by two, then it looks at the remaining number. Since the remainder is zero, the if condition here is satisfied, so the condition is true and the codes inside the body of if are executed. Let's run the code again. On the console screen this time let's enter seven. First it divides the seven by two then it looks at the remaining number, since the remaining number is one, the if condition here is not satisfied. So, the condition is false, and codes inside the body of if are not executed. We can also write the condition here as we can change the comparison operator here from equal to to not equal to. In this case, if the number entered by the user in the console will be divided by two and the remaining number is not zero, the if block will work, in that case we have to change the message here, I'm writing odd here. If the remaining number is zero, this time, the condition here will not be executed and the code in the if block will be skipped. 

Let's test it now. I enter seven on the console screen. First it divides the seven by two, then it looks at the remaining number, since the remainder is one. The if condition here is satisfied, so the condition is true and codes inside the body of if are executed. Let's run the code again. On the console screen this time, let's enter eight. First it divides eight by two. Then it looks at the remaining number since the remainder is zero, the if condition here is not satisfied. So, the condition is false and codes inside the body of if are not executed. Let's add the else statement to the code. If the condition is evaluated to false, the else statement will execute, we add a print method that includes the number is even text in the else block. In this case, if the number entered by the user in the console will be divided by two and the remaining number is not zero, the if block will work, so the condition is true. 

If the remaining number is zero, this time the condition will be false and this time the if block will be skipped and the code inside the else block will be executed. Let's run the code. On the console screen we enter 13. We get the 13 is odd message from the if statement. Let's run the code again. We enter 12. We get the 12 is even message from the else statement. Now let's change the condition. I changed the if condition here from the not equal two to equal two. In this case, if the number entered by the user in the console will be divided by two and the remaining number is zero, the if block will work, so the condition is true. If the remaining number is one, this time the condition will be false and this time the if block will be skipped and the code inside the else block will be executed. Let's run the code, on the console screen, we enter 12. 

We get the 12 is even message from the if statement. Let's run the code again. We enter 13. We get the 13 is odd message from the else statement. Let's do another example. I'll create a variable of type Boolean. Boolean check = true. Now let's check this with the if-else statement. If the check is equal to true let's print, check is true to the console. Else, let's print, check is false to the console. Before running this code, I'd like to give you information about the break point. If you remember, we have written some code so far and when we click the run button here, our code is compiled in milliseconds and the relevant results are printed to the console. This process is happening so fast that we cannot see which steps the compiler runs, which steps it skips or if there's a problem or in which step the problem is caused. That is where the break point comes into play. We can control the compilation process with the break point, although breakpoint is mostly used for debugging purposes. 

In this lesson we'll use break points to keep track of which condition is provided by the compiler and which condition is skipped. So, how to use the break point. First of all, if you want to follow the steps of the compiler from which line, it's enough to double click on this field on the left side of the line with the mouse. If you notice when you double click here, a dark point is formed. If you double click on the same point again, this time you will remove the break point. I'm adding a breakpoint here to follow how the compiler works from this line where we define the variable. Now let's run the code, but before running I will convert these codes to the comment line. Now we can test it. Of course, this time we do not click on the green play icon here. If you pay attention, there is an icon that looks like an insect or bug. This is the debug icon. We'll click on the debug icon to use the break point. I have already said that the main purpose of the breakpoint is to debug. 

Now I click on this icon, if you notice, two warning windows popped up. The first is asking if we want to convert the perspective in Eclipse to the debug perspective and the other is asking if we want to allow debugging by the Windows operating system. I'll confirm both. Yes, we can continue now. As you can see, the line where I put the break point has become darker. So, currently the compiler is at this step. Now let's go step by step. For this, you can click on the 'Step Over' icon here or you can use the F6 key on the keyboard. Notice the compiler went to the next line when I clicked here. So, currently it is checking the condition inside the if statement, if the condition here is true, it will go inside of the if block. 

If the condition is false, it will skip to the else block. Let's click on the 'Step Over' icon one more time. As you can see, the condition was true and went inside the if block. Let's click the 'Step Over' button once again, as you can see, this time the code inside the if block was executed, Check is true was printed to the console and if I click the 'Step Over' button one more time, the program was terminated without entering the else block. As you can see, it skipped the else block. I also want to show this, if the condition you are checking is Boolean, you can just write the variable name here. For example, simply typing check here is the same as typing check equals to true. If you want, let's check it this way. 

Again, I click on the 'debug' icon. The compiler is currently on line 21. I press the F6. Currently, the compiler checks the if condition. I press the F6 key once again As you can see, because the check variable is true it is again in the if block, let's press the F6 key once again. This time it ran the code in the if block and check is true was printed to the console. Now, let's terminate the program. I'll click the 'Step Over' button two more times. Of course, you can also check for false inside the if block. For this, you can put an exclamation mark at the beginning of the check variable. If you remember, this sign was inverting the Boolean state or you can also write this here, check not equal to true or check equal to false. All three spellings are the same. Let's continue now. Notice that the value of the check variable is currently true. 

Here, we will check the if not true condition. So, if it's not true then the if block will work. If true then the else block will run. So, let's change the expressions here, first I'm typing false here and this will be true. Now let's run the application. I pressed the F6. Now it checks the condition. If the condition is met, it will enter to the if block, otherwise it will go to the else block. Let's press the F6 key, as you can see, this time it did not enter the if block and jumped directly to the else block because the check variable is true. And pressing the F6 key once again, prints the statement in the else block to the console. I'll terminate the program. Now, let's set the value of the check variable to false. 

In this case, the condition that the check variable is not true in the if block will be met and the if block will work. Let's run the application and test it. I press the F6. The condition is being checked and as you can see, this time it entered the if block because the value of the check variable is false. Yes, friends, let's do another example now. First, I will convert these codes to the comment line. Let's define a variable of type integer, int score = 70. Now, let's create a check that consists of only ifs. In the first condition, let's check if the score is greater than 50. In this case, let's print, score is greater than 50 to the console. Now, let's copy this condition. Let's paste it three times. Let's make the second condition 60. Let's make the third condition equal to 70. Let's print score is 70 as a message. Let's make the last condition 80. Notice that we only used if here. 

So, each if condition is on a block of it's own and every block here will work. Pay attention to this. This time I'm adding a break point to this line. Now, let's run the application. Currently, the compiler is on this line. I press the F6 key, as you can see, it checks for the first condition, since this condition is true, the statement in the first If block will be printed to the console. Let's press F6. Yes, the statement in the first if block is printed to the console and notice that the program was not terminated, it went to the second if block. Normally, if the first condition is met in the if-else statement, it does not look at the else block at all. But here we just created the if condition and each if is a block by itself. Now, it will check the second condition. 

Since this is also true, the statement in the second block is also printed to the console. Now, it has moved to the third if block, this condition is also true. Pressing F6 entered this condition. Pressing F6 once again, this statement is printed to the console and the last condition is passed. Notice that the last condition is not met because the score is 70, it is less than 80. Therefore, the statement inside this condition will not be executed. I end the program by pressing F6. Yes friends, let's talk about one more topic and end the lesson. Notice that we only wrote one line of code in each if block here. If the code you will write in the if or else block consists of one line, then you cannot use curly braces. 

Although this use leads to complexity, it's useful to know that there is such a use. Now, let's remove all the curly brackets here. Write the expression inside the block either right next to the condition or just on the subline, it doesn't matter, but only a single line is evaluated in the corresponding block. So, if I create a second line here, it will not be evaluated in the if block. Therefore, I'll rewrite the expression in the last condition. If this line is accepted in the if block, it should not be printed to the console because the if condition here is not met. If not accepted in the if block, then this statement should also be printed on the console. Let's run it and see. I will not put a break point this time, I will run it directly. 

And as you can see, the relevant statements are printed even though we don't use curly braces. Also, it printed the statement in the last line because only the first line after the if condition is considered defined inside the if block, the other line is considered outside of the if block. Therefore the last line is also printed. If the last line was accepted in the if block, we would not be able to see this statement in the console this time. Yes, friends. I think that if else statement is understood. Let's take a short break here. See you in the next lesson.

 

About the Author
Students
3995
Courses
64
Learning Paths
5

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics