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.