Lest's Start JAVA Programming
Part 8
What is flow control?
When JAVA code execute it get execute from top to bottom of the program. And that is the flow of the code. By doing flow control we can alternate the code flow. Can stop some command execution.
If condition is one thing we use to control the code flow.
With this code you can see it print A to F from top to bottom. so that the flow of this code.
In this code you can see it use If condition to control the flow of the code. In the example you can see If condition have some code inside it (d > 30) that's call the condition. When this condition if fulfil code line next to the if condition will executed other wise that code line will skip.
With that example you can see 'If' condition is only effect the line next to if condition.
Let's take look at these examples in flow chats.
Flow Chats are the diagrams we use to visualise the flow of the code.
First example in flow chart.
Second example in flow chart.
Example 03
Example 04
After both of these codes executed we can see only "C" is skipped and "D" will print in the output. reason is if condition always effect to the line after if statement.
Can we make "IF" statement effect multiple lines?
This is question now we have. because in our programs we need condition effect to multiple lines, depend on our program logic. But as we check in previous example "IF" only effect to the line after it.
There is a way we can do this. it is using Curly Brackets {}. Like how we use them to defined class scope, we can use them to defined the scope of "IF" statement.
Let's check the example
This example in flow chart
In this example we can see "IF" statement of "d > 30" is effecting the scope defined by curly brackets.
Comments
Post a Comment