Conditional

The programming we are doing now is sequential programming, that meaning the code is executed from top to bottom.

It is very linear, in which the code of each line will be read, which begins with the first line of code that we write and end in the last line.

But we do not always want to work our programs like this. Often, we want to execute the code only when certain conditions are met.

For example, we want to display a If a user is under 18 and has a different message 18 years of age or older, we want to display a message.

We want to control the flow of the program for ourselves. We can do this with the conditional structures provided by Java.

A conditional statement is an expression that produces a real or false result. We can use that result as our requirement. To create the expression, we use the Boolean operators, we saw only how to perform the operations and how to get the results, not how to use them.

To use the result of a Boolean operation, the Java programming language provides two specific conditional constructs.

  • if…else
  • switch…case

If…else

If-then and if-then-else Conditional Statement Make the simple decision about what the Java program has to do next. They work in the same logical way as we do during real-life decisions.

For example, if a person is 18 years or older then he can cast his vote. Which determines whether a person can cast his vote, the situation will either be true or false. It works exactly the same in Java.

If statement

In its most basic form is, an if statement executes a single statement or a block of statements if a boolean expression evaluates to true.

Here’s the syntax:


if (boolean-expression) statement

The Boolean expression should be attached to the bracket. If you use only one statement, then it should end with a semicolon. However, there may be a statement block attached by statement braces.

In that case, each statement within the block requires a semicolon, but the block itself does not.

Here’s an example:


double commissionRate = 0.0;
if (salesTotal > 1000.0) {
	commissionRate = 0.05;
}

In this example, a variable named commissionRate is initialized to 0.0 and then set to 0.05 if salesTotal is greater than 1000.0.

Here’s an example that uses a block rather than a single statement:


double commissionRate = 0.0;
if (salesTotal > 1000.0) {
	commissionRate = 0.05;
	commission = salesTotal * commissionRate;
}

In this example, the two statements within the braces are executed if salesTotal is greater than $1,000. Otherwise, neither statement is executed.

If-then-else :

if statement can include an else clause that executes a statement or block if the boolean expression is not true.

Its basic syntax is :


if (boolean - expression) {
	// statement
}
else {
	// statement
}

Here’s an example:


double commissionRate;
if (salesTotal < = 1000.0) 
	commissionRate = 0.03;

else 
	commissionRate = 0.06;

In this example, the commission rate is set to 2% if the sales total is less than or equal to $1000. If the sales total is greater than $1000, the commission rate is set to 5%.

Here’s an if statement with an else clause that uses a block instead of a single statement:


double commissionRate;
if (salesTotal < = 1000.0) {
	commissionRate = 0.03;
	level1Count++;
}
else {
	commissionRate = 0.06;
	level2Count++;
}

If-else-if ladder :

If this form of statement is used when we have many situations to evaluate and only one statement or statement block should be executed based on the result

Syntax:


if (boolean-expression1)     statement1 
else if(boolean-expression2)     statement2 
else if(boolean-expression3) statement3 
else  otherwise statement. 


Example:
We have to display name of the weekday on the basis of given number considering Monday is the first day of week.


int nday=2; 
if(nday==1) 
 System.out.println("Its Monday"); 
else if(nday==2) 
 System.out.println("Its Tuesday"); 
. 
. 
. 
. 
else if(nday==7) 
 System.out.println("Its Sunday"); 
else 
 	System.out.println("There are only seven days in a week"); 

Output in the above example would be "It's Tuesday" because NDE has a value of 2, so the second will evaluate the BlueNexpress truth.

Similarly, as Anne's value will change, evaluation of one of the above situation will be accurate and the related statement will be executed.

If neither of condition evaluates to true (nday<1 or nday>7) the statement in the last else block will executed "There are only seven days in a week".

Nested if-else :

We can add many if / if-else / if-else- if a series of stairs is involved then the ladder is included. Therefore, we can ensure that our programs execute certain conditions when a series of conditions is completed. If at some other level then the nest is being done

Syntax:


If(boolean - expression) {
	If(boolean - expression) {
		Statements to be executed
	}
	else {
		Statements to be executed
	}
}
else {
	Statements to be executed
}

Example:
A valid employee should have age between 25 and 60.


If(age >= 25) {
	If(age < = 60) {
		System.out.println("Valid Employee");
	}
	else {
		System.out.println("Age above 60 not allowed");
	}
}
else {
	System.out.println("Age below 25 not allowed");
}

The Conditional Operator (? :)

The conditional operator is sometimes called a ternary operator because it involves three operands.


Syntax: logical_expression ? expression1 : expression2

If logical_expression evaluates as true, the result of the operation is the value of expression 1, and if logical_expression evaluates on the wrong, the result is the value of expression 2. Note that if expression 1 is evaluated because logical_expression is true, then expression 2 is not, and vice versa.

You can use conditional operators in many situations, and a general application of it is to control the output based on the result of the expression or the value of the variable.

Depending on the specified condition, you can change a message by selecting a text string or some other.

For example, you can have two variables of type int with eAge and myAge names, and you want to assign values stored in your AJ and myAge to a maximum of three variables, old, which also type in int.

You can do this with following statement:


older = yourAge > myAge ? yourAge : myAge; 

Similarly we have to store smaller value between a and b into c.


c=(a<b)?a:b;  

Switch-case

Another way to control the flow of our programs is with a thing called switch statement. A switch statement gives us the option of testing for a series of values for our variable.

Their use can be long, complicated rather than ... and if the statement. If this is the option for the ladder. If and if-then-unlike other statements, switch statement can have many possible performance paths. A switch works byte, short, four, and int primitive data types.

It also works with math type (discussion in nim types), string class, and some special classes which wrap some primitive types: character, byte, short, and integer (discussion in numbers and strings). It is originally used to create menu-driven programs

Syntax:


switch (variable_to_test) {
case value1:
	statement to be executed.
	break;
case value2:
	statement to be executed.
	break;….
case valuen:
	statement to be executed.
	break;
default:
	statement to be executed.
	break;
}

We start with the keyword switch, then there is a pair of round brackets. The variables that we want to check goes between the round bracket of the switch. Then we have a pair of curly brackets. The other part of the switch statement goes between all the curly brackets. For every price that we want to check, we need keyword case.

case value:

After the case price comes a colonel. Then we believe what the value is, what we want to do. This is our code that we want to execute. Keyword breaks are required to break each case of the switch statement.

Finally the default value is optional. It may be included if we have other values in our variable but we have not seen anywhere else in the switch statement.

Example: Again taking the same example of displaying name of the weekday on the basis of given number considering Monday is the first day of week.


int nday = 3;
switch (nday) {
case 1:
	System.out.println("this is Monday");
	break;
case 2:
	System.out.println("this is Tuesday");
	break;
case 3:
	System.out.println("this is Wednesday");
	break;
case 4:
	System.out.println("this is Thursday");
	break;
case 5:
	System.out.println("this is Friday");
	break;
case 6:
	System.out.println("this is Saturday");
	break;
case 7:
	System.out.println("this is Sunday");
	break;
default:
	System.out.println("there are only seven days in a week");
	break;

}

//Output: 
This is Wednesday. 

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Protractor Online Training : We have closed registration for training

Please do email to chercher.tech@gmail.com for any queries

If you already registered then please do check your email for the Webex link for the training starting from 15th Nov 2018
 
Join My Facebook Group
Join Group