Java Branching Statements

Home   »    Java   »    Java Branching Statemtns


We know that Instruction those are written in java Language are Executed in Sequence wise or Step Wise as they are Written in Program. or these are Executed in Sequence Order. Decision Making statements are used when we wants to execute the statements as according to the user needs. A User can Change the Sequence of the Statements for Execution. Many Times we Wants to Execute the Set of Instructions to be Executed in one Situation and other Statements in other Situations For Executing the Statements in Specific Situation there are Some Decision Making Statements or Decision or Control Statements those are provided by the java Language. In The Decision First a Condition is checked if it is true then it Executes the next Instruction otherwise it Executes another Statements.


Contents



Decision Making With IF Statement

An if statement tests a particular condition; if the condition evaluates to true, a course-of-action is followed i.e. a statement or set-of-statements is executed. Otherwise (if the condition evaluates to false), the course-of-action is ignored.

The if statement may be implemented in different forms depending on the complexity of condition to be tested.

  1. Simple If Statement
  2. if...else Statement
  3. Nested if...else Statement
  4. else if ladder




Simple If Statement

The Keyword if tells the compiler what follows The Condition Following the Keyword if is always enclosed within a pair or Parentheses if The Condition id True Then the Statements is Executed if the Condition is not true then the Statement is not Executed For Checking a condition Relational Operators are Used Like > ,< ,==, >=,<= etc.
   The if statement is widely used for checking a particular condition Suppose you want to check the condition and then execute the condition if your condition is met

for ex:
if(a>b)
then print a is greater then here we first check the condition
that either a is greater than b if yes then execute the condition

if...else Statement

The if Statement is used for Executing a Single Statement or a Group of Statements When the Condition Following is true . It does nothing when the Condition is False , For Executing the group of Statements either a Condition is False we uses else The if-else is similar to if but the difference is that is also provides us the alternative to execute the other statement if a condition is not true for ex:

if(a>b)
print a
else
print b

Here if first checks the condition either the a is greater than b if yes then it will print a suppose if a is not greater than b then it will be print b because we specify the b in the else statement.

Write A Program to check that given year is leap year or not using if-else...




Output of "LeapYear.java"


Download Complete Program

  -  







Nested if...else Statement

When an if statement occurs within another if statement, then such type of is called nested if statement.





Output of "NestedIf.java"


Download Complete Program

  -  







else if ladder

The if-elseif is similar to the if-else but here the first if is used for checking a condition and the other elseif is used for checking a one more condition suppose if we wants to check the two or more conditions then we can use the if-elseif.





Output of "TaxCaluate_elseif.java"


Download Complete Program

  -  







Switch Statement

When number of conditions (multiple conditions) occurs in a problem and it is very difficult to solve such type of complex problem with the help of ladder if statement, then there is need of such type of statement which should have different alternatives or different cases to solve the problem in simple and easy way. For this purpose switch statement is used.





Output of "SwitchStatement.java"


Download Complete Program

  -  







The ? : Operator

Java has an operator that can be used an alternative to if statement. You are familiar with this operator, the conditional operator ?: This operator can be used to replace if-else statement of the general form:

if(expression !) expression 2; else expression 3; The above form if can be alternatively written using ?: as follows:

expression 1 ? expression 2 : expression 3;

It works in the same way as given form of if does i.e. expression 1 is evaluated, if it ture, expression 2 gets executed (i.e its becomes the value of entire expression). For instance, the following if statement,

int c;
if (a > b)
c = a;
else
c = b;

Can be alternatively written as

int c = a > b ? a : b;

See how simple and compact your code has become.





Output of "Con_Operator.java"


Download Complete Program

  -  













<< Previous Topic
Next Topic >>

1 comment: