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
Hi there. In this video, we'll solve some questions that reinforce the topics we learned in this section and also help us prepare better for the exam. If you're ready, let's get started without wasting time. This question is built on the do while loop. But before, three variables were defined in int data type, and after some operators were applied to these variables in the do block the result was transferred to the result variable. After the loop is complete, the result variable is printed to the console. The question asks us to find out what the output is. It'll be more helpful for you to pause the video here and solve it yourself. I'll be giving the answer on the next slide.
Yes, if you found the answer as -3 it means you gave the correct answer. Let's solve it together now. In line 4, three variables of type int are defined. The initial value of variable A is 7, variable B is 2, and the variable result is 1. Then the do while loop is used. In the do block, the decrement operator is applied to variable A. And the increment operator is applied to variable B. This means that in each iteration, when calculating the value of the variable results in row 6, the value of A will be decremented by 1 and the value of B will be calculated by incrementing 1. So, for the first loop, the value of A is now 6 and the value of B is now 3. Now, let's look at line 6. If you notice, the multiply and assignment operator is used here.
But the main difference is, that the expression A -1 is used after the equal sign. I think most of you are probably stuck here. Here is the long version of this. The result = result * [a - b]. Because the multiplying assignment operator treats the entire expression to the right of the equals expression as a single expression. Therefore, you must enclose [a - b] in parentheses. So, in the first iteration, the value of the variable result will be 1* [6 -3]. So, in the first loop, the value of the result variable becomes 3. Then, the while condition is checked. In the while condition, variable A is required to be greater than variable B. So, as long as A is greater than B the loop will continue. Now that A has a value of 6 and B has a value of 3, the loop will continue once again.
In the second iteration, the value of A will decrease by 1 again, and the value of B will increase by 1 again. So, when calculating the result variable in line 6, the new value of A will be 5 and the new value of B will be 4. The value of the result variable in the second loop will be the previous value of the result variable multiplied by 5 - 4. Currently the value of the result variable is 3. So, 3 * [ 5 - 4], will be the new value of the result variable. So, the value of the result variable at the end of the second loop will be 3 again. Then, it will check the while condition. Since A has a value of 5 and B has a value of 4, the loop will continue once again. In the third iteration, the value of A will decrease by 1 again and the value of B will increase by 1 again.
So, when calculating the result variable, the new value of A will be 4 and the new value of B will be 5. The value of the result variable in the third loop will be the previous value of the result variable multiplied by 4 -5. Currently, the value of the result variable is 3. So, 3 * [ 4 - 5] will be the new value of the result variable. So, the value of the result variable at the end of the third loop will be -3. Then the while condition will be checked once again. Since A has a value of 4 and B has a value of 5, the condition here will now be false and the loop will terminate. Since the final value of the result variable is -3, the -3 value will be printed on the console screen. I think this question is clear.
Now, let's look at the next question. Yes, this question is about the use of the if-else statement. A variable of type boolean is defined and the value is true. Then, a check was made with the if-else statement and some messages were trying to be printed on the console screen according to the result of this check. The question is, what will be the output of this code? Here, stop the video and try to solve it yourself. I'll be giving the answer on the next slide. Yes, your answer is correct if you selected option E. Notice that the if-else statement here does not use any curly braces that specify a scope. Therefore, the print method in line 7 is not included in the if-block. In this case, the print method is inserted between the if-block and the else-block. So, this syntax is incorrect, because the else-block must come right after the if-block.
Otherwise, using else will cause a compilation error. Therefore, this code block cannot be compiled, and this is because of the else-block on line 9. The answer is option E. Now, let's move on to the next question. Yes. This question is about the use of the ternary operator. Two variables named A and B of type int are defined, and increment and decrement operators are applied to these variables. Then, a conditional structure is created using the ternary operator. But if you notice, there is a nested ternary use. This is one of those question types that you can get quite confused with. Then, the result was transferred to a variable named 'result' and the result variable was wanted to be printed to the console.
The question is, what the output will be? If you want, pause the video and try to decode it. We'll solve this question together on the next slide. Yes, if you found the answer as 1 it means you gave the correct answer. Now let's move on to solving the problem. In such questions, you should solve the innermost ternary structure last. To make this easier, let's call the innermost ternary structure, i.e., the statement A++ >-- b? "1": "2" as X. After doing it this way, it will look a little simpler and it'll be easier for you to solve. Let's do the first step now. Since the increment operator is applied to variable A and the increment operator is used before variable A, the new value of A is 4. After the greater than sign the decrement operator is applied to the variable B.
But since the decrement operator is used after the B variable, the variable of B remains 5 until the next operation. So, the condition here is, 4 is less than 5. Because this condition is true, the code after the question mark works. So, the expression we call X will execute. In the second step, we'll solve the X expression, i.e., the innermost ternary expression. Of course, while going to this step the value of variable A is 4 and the value of variable B will now be 4 because it decreases by 1. Now, let's look at the second step. The increment operator is applied to variable A. But the value of A is still 4, because the increment operator is used after variable A. After the greater than sign the value of B will decrease by 1 because the decrement operator is applied to the variable B.
And the decrement operator is used before the variable B. So, the new value of B will now be 3. Hence, the condition here will be, 4 is greater than 3. This condition is true, and the code after the question mark will execute. So, the value 1 will be passed to the result variable. When all this process is finished the value of variable A will also increase by 1, and the last value of A will be 5, and the last value of B will be 3. The value of the result variable, namely 1, will be printed on the console screen. Yes, that's the solution for nested ternary expression. If it sounds confusing, as a second way, you can convert the ternary expression here to an if else statement and solve the question that way.
If you remember, ternary statements were a 1 line version of the if else statement. You can see the ternary expression written as an if else statement on the slide. Here, I suggest you pause the video and review the if else statement. Yes, I think this question is clear, let's move on to the next question. Okay. The switch case statement is used in this equation. A variable of type int has been created and the value 2 is assigned to this variable. Then, this is checked with the switch statement. According to different cases, some messages were printed on the console screen. The question is, what will be the output of this code? Pause the video here and try to solve the question yourself. We'll answer that question together on the next slide. Yes, if you found the answer as C, Tuesday, then your answer is correct. Now, let's move on to solving the problem.
In line 4, the value 2 is assigned to the variable num, and the value of the variable num is checked in a switch statement. So far, everything is normal. But what is different here is that the default statement is used before the case statements. This has probably caused some confusion for many of you, but using the default statement before or after the cases doesn't actually change anything. Therefore, this code will compile successfully. Since the value of the num variable is 2, the message in case 2 is printed to the console. Also, notice that the keyword break is not used after default in case 1. However, since case 2 is running, these two cases are automatically skipped, and the fact that the break keyword is not used here does not change the result.
If the break keyword were not used in case 2, then Tuesday and Wednesday would be printed to the console together. As a result, the code will compile as it is and Tuesday will be printed on the console screen. The answer is option C. Let's move on to the next question. Yes, this question is about the switch case statement. A variable of type int is given and this variable is controlled by the switch case statement. The question is, what will be the output of this code? Pause the video and try to solve it yourself. I'll be sharing the answer with you on the next slide. Yes, your answer is correct if you chose option D, because this code does not compile. So, why doesn't this code compile? Let's look at this now. Notice that the first case in line 7 uses 3 - 1.
So, this actually means case 2. In line 8 case 2 is used again. Therefore, the same case was used twice. Therefore, a compilation error occurs at line 8 and this code cannot be compiled. The answer is option D. Let's move on to the next question. Yes, this question is about the switch case statement. A variable of type int is given and this variable is controlled by the switch case. The question is, what will be the output of this code? Pause the video and try to solve it yourself. I'll be sharing the answer with you on the next slide. Yes, your answer is correct if you chose option D. Because this code does not compile. So, why doesn't this code compile? Let's look at this now.
Notice that a variable of type double has been created, and a double type is checked in the switch parentheses. But this is wrong, because it cannot switch on a value of type double. Only convertible int values, strings or enum variables are permitted. Therefore, we get a compilation error on line 5, and therefore this code cannot compile. The answer is option D. Let's move on to the next question. Yes, this question is about the do-while loop. A variable of type int is created, and its value is incremented by one in the do block. Then, in the print method, the decrement operator is used before this variable, and the last value of the variable is printed to the console. Another print method is used outside the loop. The question is, what will be the output of this code block? Pause the video and try to solve it yourself. I'll be giving the answer on the next slide.
Yes, your answer is correct if you chose option D. Notice that the while condition is always true. So, actually this is an endless or infinite loop. Since it's an infinite loop, the print method in line 8 can never be reached. So, here we get an unreachable code compilation error on line 8. Therefore, this code cannot be compiled. The answer is option D. Let's move on to the next question. Yes, this question includes a do-while loop, an if statement, and a continue keyword. Two variables named num and sum of type int were created. And after some operations were done, the last value of the sum variable was printed to the console. You can pause the video and start solving the question carefully. I'll be giving the answer on the next slide.
Yes, if you found the answer as 12, you gave the correct answer. Now, let's solve the question together. The initial value of the variable num is 6, and the initial value of the sum variable is 0. The if statement is also used in the do block. In the parentheses, the remainder of the division of the num variable by 2 is checked for being equal to 1. If the remainder is one, then the continue keyword in the if block will come into play. So, actually if the value of the variable num is an odd number, the continue keyword will work, otherwise the loop will continue by skipping the if block. Now, let us consider each cycle in turn. At the beginning of the loop, the value of the variable num is 6, the remainder of 6 divided by 2 is 0.
That is, the if condition is not met and the if block is skipped, and the loop continues from line 9. In this line, the sum of the variable sum and num is transferred to the variable sum. So, the value of sum variable will be 6. Then the while condition works. Here, since the decrement operator is used before the num variable, the value of the num variable first decreases by 1. That is, it becomes 5. Then the condition is checked. Since 5 is greater than 0, the loop continues. At the start of the second loop, the num variable is now 5. Since 5 is an odd number, the if condition is met, and this time the continue keyword comes into play. Thanks to the continue keyword, the stream jumps to the end of the loop. So, in the second loop, line 9 is skipped and the while condition is parsed directly. In the while parentheses, the variable num is decremented by 1 and the new value is 4.
Since 4 is greater than 0, the loop continues. At the beginning of the third loop, the value of the variable num is 4 and the variable sum is 6. Since the num variable is an even number, the if condition is not met, and this time line 9 works. Since the variable sum is 6 and the variable num is 4, the new value of the sum variable is now 10. Then the while condition is parsed. The value of the variable num decreases by 1 to become 3. Since three is greater than zero, the loop continues again. As a result, line works as long as the variable num is an even number, and line 9 is skipped if it's an odd number, and continues from the while condition. In this way, there will be a total of seven cycles.
In the last loop, the value of the variable sum now becomes 12 and the value of the variable num is 0. The variable num decreases by 1 and becomes -1 in the while condition. So, the num variable is now less than 0. Therefore, the loop ends and the value of the sum variable, namely 12, is printed to the console with the print method in line 11. The answer is option B. Let's move on to the next question. Yes, this question is about the use of the break keyword. A while loop is used in the do-while loop, and an if statement is used inside the while loop. In addition, labels are used in the do-while loop and the while loop. The question is, what will be the output of this code block? You can pause the video and try to decode it. I'll be giving the answer on the next slide. Yes, if you selected option B then congratulations, you have found the correct answer.
Now, let's look at the solution to the problem. A variable named a with type short is defined, and this variable is initialized in the do block. The initial value of variable a is 10. Then, the while loop continues. Since the condition is true in the while loop, the program continues from the inner while loop. First, the print method works here. Notice that the increment operator is applied to the variable a in the print method. But since the increment operator is applied before variable a, the first value of variable a is incremented by one. Its value is 11, and then this value is printed on the console. Then the if block works.
In the if parentheses, it's checked whether the value of variable a is 11 or not. Since the new value of variable a is 11, this condition is true and the break keyword in the if block works. If you notice, a label named first is used with a break keyword here. So, this label represents the do-while loop. Therefore, the program exits the do-while loop and it terminates. Therefore, only the value 11 appears on the console screen. The answer is option B. Yes, in this video we tried to solve the questions that reinforce the topics we learned and prepare you for the exam. I hope it was useful. Let's take a short break here. See you in the next lesson.
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.