Flow control in java is a key part as these are widely used in source code of any of the programs that are written in Java. In java, flow controls include if statement , switch statement, loops like for, while, do - while loop. These flow controls are described as following with it syntax and sample of source code.

These are oftenly called as decision statements. This is because whenever you use decision statements, you need to provide some mathematical expression that gets executed and decide which sort of action is to be taken.

if statement

Syntax for if statement is as follows:

if (expression) {
// any source code
}

In above syntax, expression is any mathematical expression that returns boolean value. Here, expression needs to be mandatorily boolean.

This expression can be anything like be "x" any variable whose value is to be compared with 1.

for example,         if(x == 1) {
System.out.println("In if statement");
}

Above example shows that "x == 1" is checked and if value of "x" is "1" , then the expression will return "true" as value and then the control will move inside the if statement and print "In if statement" because "System.out.println()" is used to display any text on screen.

if statement also gets attached with else block. Its syntax would be

if (expression) {
// source code
} else {
// source code
}

In above syntax, if value of expression becomes "true" then only the control will enter into "if" block else control will enter into "else" block.

For example,    if(x == 3) {
System.out.println("Value is 3.");
} else {
System.out.println("Value is not 3.");
}

In above example, if value of "x" is 3 then the control will enter inside "if" block and print "Value is 3." on screen and then control will not enter else block because "if" block has got executed. Similarly, if expression value has come out to be "false" then the control have directly entered into "else" block and would have printed "Value is not 3." on screen.

Furthermore, there is one more block which java provides and it is "else if " block. Its syntax would be something like this

if (expression1) {
// source code
} else if(expression2) {
// source code
} else {
// source code
}

In above syntax, if "expression1" gets satisfied then the control will enter into "if" block , execute the code written in it and come out of the block. Control will enter neither into "else if" block nor into "else" block.

If "expression1" is not satisfied, then the control will execute "else if" bloack and validate the expression. If "expression2" becomes true then the control will enter into "else if" block and execute the code written in "else if" block. But, if "expression1" and "expression2" are not satisfied too, then the control will enter directly into "else" block and execute the code written in it.

for example,      if(x == 1) {
System.out.println("Value is 1.")
} else if( x == 2) {
System.out.println("Value is 2.")
} else {
System.out.println("Value is neither 1 nor 2.");
}

In above example, if "value" is 1 then "Value is 1." will get printed into screen. If value is 2 then "Value is 2." will get printed and if value is neither 1 nor 2, then "Value is neither 1 nor 2" will get printed or displayed.

There are following rules for using "else" and "else if" :

1: Zero or one "else" for a given "if" can be used and it must come after any else ifs.
2: We can have zero to many else ifs for a given "if" and they must come before the "else". "else" is optional.
3: If one "else if" gets executed then none of the remaining elses or else ifs will be tested.

Furthermore, two expressions can be given in one "if" statement.

Its syntax will be like this

if(expression1 && expression2) {
// Source code
}

Here, in above syntax, to make control of the program enter into "if" block, both "expression1" and "expression2" needs to be satisfied i.e. values of both expressions must be "true".

For example, consider value of x=1, y=2 then

if(x == 1 && y == 2) {
System.out.println("Values are 1 and 2.");
}

Here, in above example, if values of "x" and "y" are 1 and 2 respectively then only control of the program will enter into "if" block and print "Values are 1 and 2." on the screen. If either of the two values fails then control will not enter into "if" block and that's because of the use of logical AND operator " && " which demands both expressions to be mandatorily be "true" for execution.

We can use other operators as well like ! (NOT operator, which requires only one expression), !! (OR operator, which gets executed even if either of the expressions succeeds).

Syntax for using NOT operator will be something like this

if( !expression) {
// source code
}

Here, it will make the value of expression exactly the opposite i.e. if value of expression is "true" then by use of NOT (!) operator, value will get changed to "false" and vice-versa.

Similarly, with OR operator, its syntax will be

if(expression1 !! expression2) {
// source code
}

Here, if either "expression1" or "expression2" or both succeeds then the control will enter into "if" block but if neither of the expressions succeeds, "if" block will not get executed.


Like it on Facebook, Tweet it or share this article on other bookmarking websites.

Comments (0)

There are no comments posted here yet

Who is the best internet service provider?

According to you who is the most trustworthy internet service provider(broadband)?
No answer selected. Please try again.
Please select either existing option or enter your own, however not both.
Please select minimum 0 answer(s) and maximum 5 answer(s).
/polls/computers-and-technology/6443-who-is-the-best-internet-service-provider.json?task=poll.vote
6443
5d1ingR07Sv8PRZj
radio
[{"id":"22294","title":"Airtel","votes":"29","type":"x","order":"1","pct":43.94,"resources":[]},{"id":"22295","title":"Reliance","votes":"6","type":"x","order":"2","pct":9.09,"resources":[]},{"id":"22296","title":"BSNL","votes":"18","type":"x","order":"3","pct":27.27,"resources":[]},{"id":"22297","title":"MTNL","votes":"2","type":"x","order":"4","pct":3.03,"resources":[]},{"id":"22298","title":"Others","votes":"11","type":"x","order":"5","pct":16.67,"resources":[]}] ["#ff5b00","#4ac0f2","#b80028","#eef66c","#60bb22","#b96a9a","#62c2cc"] ["rgba(255,91,0,0.7)","rgba(74,192,242,0.7)","rgba(184,0,40,0.7)","rgba(238,246,108,0.7)","rgba(96,187,34,0.7)","rgba(185,106,154,0.7)","rgba(98,194,204,0.7)"] 350
bottom 200
No married couple wants to end up getting divorced. It is not like they have planned for it. They try to put up with their partners for as long as they
Due to our modern lifestyle, we feel that digestion related disorders are a common problem. Thus, we neither give importance to them nor seek any help
The bond of marriage brings the two people together. Initially, everything may seem okay and both of them slowly start discovering each other in the journey.
Why to choose Java? In the computer market, being able to use Java is a crucial knowledge. Java is a development terminology used to build compelling
DATABASE CONNECTIVITY This articles explains the need of database connectivity, the way it is achieved through Java using the JDBC(java database connectivity)
  Write your first simple java program.   First of all we need is a placewhere we can write your Java code. All Java programs are written using
Array of objects in Java Arrays are defined as block of continous memory locations which are arranged in one after another. Elements of the Array can
Synchronized block is alternative mechanism for "synchronized" methods.If we inherihate the non synchronized blocks either from base class or interface