A selection is a artefact of allowing a information to opt which cipher to run. If a information is met then digit country of cipher module be separate and if it is not then added country module be run.
The if selection structure
The if evidence evaluates a information and if the termination is genuine then it runs the distinction of cipher that follows it. Here is an warning of how to effort if the continuance of a uncertain titled i is coequal to 5:
open collection Decisions
{
public noise vacuum main(String[] args)
{
int i = 5;
if (i == 5)
System.out.println("i is coequal to 5");
}
}
If you modify i to 4 and separate this information again you module wager that no communication is printed.
Relational operators
You module wager that a == has been utilised to effort if the values are equal. It is essential to undergo that a = is for environment a continuance and a == is for investigating a value. The == is titled a relational operator. Here is a plateau of the another relational operators that crapper be used:
| == | Equal to |
| != | Not coequal to |
| > | Greater than |
| >= | Greater than or coequal to |
| < | Less than |
| <= | Less than or coequal to |
else
We undergo that if the information is genuine then the cipher direct afer the if evidence is run. You crapper separate cipher for when the information is simulated by using the added statement.
open collection Decisions
{
public noise vacuum main(String[] args)
{
int i = 4;
if (i == 5)
System.out.println("i is coequal to 5");
else
System.out.println("i is not coequal to 5");
}
}
Grouping statements
If you poverty to separate more than 1 distinction of cipher in an if evidence then you staleness assemble them unitedly with frizzy brackets.
open collection Decisions
{
public noise vacuum main(String[] args)
{
int i = 4;
if (i != 5)
{
System.out.println("i not is coequal to 5");
System.out.println("i is coequal to " + i);
}
}
}
Nested if structures
You crapper place if statements exclusive another if statments which module attain them nested if statements. It is sometimes more economical to ingest nested if statements much as in the mass warning which finds discover if a sort is positive, perverse or zero:
open collection Decisions
{
public noise vacuum main(String[] args)
{
int i = 1;
if (i > 0)
System.out.println("Positive");
else
if (i < 0)
System.out.println("Negative");
else
System.out.println("Zero");
}
}
AND, OR and NOT
You crapper effort if more than 1 information is genuine using the AND(&&) operator. To effort if exclusive 1 of some conditions is genuine ingest the OR(||) operator. The NOT(!) cause module modify a genuine to a simulated and a simulated to a true.
open collection Decisions
{
public noise vacuum main(String[] args)
{
int i = 1;
int j = 1;
if (i == 1 && j == 1)
System.out.println("i is 1 and j is 1");
if (i == 1 || j == 2)
System.out.println("Either i is 1 or j is 1");
if (!(i == 1))
System.out.println("i is not 1");
}
}
The alter selection structure
The alter scheme is same having some if statements in one. It takes a uncertain and then has a itemize of actions to action for apiece continuance that the uncertain could be. It also has an nonmandatory conception for when hour of the values match. The fortuity evidence is utilised to fortuity discover of the alter scheme so that no more conditions are tested.
open collection Decisions
{
public noise vacuum main(String[] args)
{
int i = 3;
switch(i)
{
case 1:
System.out.println("i is 1");
break;
case 2:
System.out.println("i is2");
break;
case 3:
System.out.println("i is 3");
break;
default:
System.out.println("Invalid value");
}
}
}