This lecture is about control flow, so when and why we need control flow, there are many cases when
you have to act differently based on the user input.
Let's take Who Wants to Be a Millionaire as an example.
There is a question on every level with four options and only one right answer is that if the player
chooses the right one, the bottom will start glowing green and can go to the next level.
Otherwise, the bottom will glow orange or red and the player will lose the game.
So based on the decision, different code will be run.
So what contraflow as it is used for decision making and it enables your program to conditionally execute
code blocks.
OK, what we are going to look into in this lecture first, the if then the if our statements with some
examples and lost the switch statement after this lecture, there is going to be according to.
So how and if statement looks like first the key word, then a condition in parentheses, then the curly
braces with the code, the condition can be a boolean variable, a comparison or a complex condition.
Let's see some examples in case of the damaged car, it would look like this.
If the car is damaged, we print out damaged car after car is not damaged, then nothing is printed
out.
Another one, if the car's horsepower is greater than 500, the reaction would be then this car is fast
and let's see a bit more complex.
If the move is rating is greater than six point eight and the movie category is action or comedy, I
say I would watch it tonight.
The movies category is Prentice's because an operator has higher precedence than or operator like in
math, multiplication has higher precedence than addition or subtraction.
Now, let's move on to if else, if else is an extension of if statement.
It gives us a default option.
If the condition is false, if the condition is true, the code belongs to F. will be executed.
And after that the execution continues after the whole.
If our statement if the condition is false, then Java executes the code which belongs to us and continue
executing the code after that.
In the case of is damaged, if the car is damaged, damaged car is printed out, otherwise accident
free text will appear.
In the case of horsepower, one, the cost horsepower is less than 500.
We would get a reaction, something like this.
Well, not that bad.
And if the movie's rating is less than six point eight or it's not an action or comedy, I would say
no way to watch it.
We also have another form of if us in this case, after the first conditional check, we have another
check.
If the first one is false, Thanjavur checks the second one.
If that's false, too, then the execution continues after the if as block.
Let's make it clear within an example in this case, which are the color of the car, and we print out
the text based on that, if the car's color equals to red, we print out it is a red car.
If that's not a red car, which checks, whether it's a green car, if it's true, it is.
A green car is printed out, otherwise nothing is printed out.
We can also add an assumption to this.
We can have a multiple as ifs here and at the end we have analysis.
So in the case of the car color, we can check whether the car is red or green or orange or black.
We print out something that shows what the color of the car and have else for the case.
We cannot identify the color.
If it's not clear, don't worry, we will also cover these in the cording part.
Now let's move on to switch statement.
The switch is to select one of many code blocks to be executed, its form is switch keyword a value
in parentheses.
We have cases with values that can be compared to the first value.
We also have break and the fourth keywords here.
Now let's go into details so the value is compared with the values of each case like case one case to
see if there is a match.
The associated block of code is executed.
When Java reaches a breaking word, it breaks out of the switch block.
This will stop the execution of more code and case testing inside the block before part is executed
when no case match is found.
Let's take the car again.
As an example, we give the car color to the switch statement.
Java compares it to the cases.
If there is a match, the right text is printed out.
If there is no match, then I have no idea what the color of this car is printed out, which is better
in cases when we have a lot of options and a code that belongs to that is quite short.
One line or two lines.
In case of more lines, it would make the code less readable.
That's it.
Let's continue with the coding part.