Scanner class Methods in Java

Java-Decision Control Statements

 Decision Control Statements

Decision Control Statements are used to take decisions.While writing a program several situation occurs and you have to decide what you want your program to do in a particular situation.Here, you have to take a decision.

For example:  Reeta wants to write a program where she can extract odd and even number. And she wants to print even numbers only.Here, she need a program which can take decision for printing the correct even numbers.
To take this decision, she'll use the decision control statements.

Types of Decision Control Statements

There are main three types of control statement:
  1. if statement
  2. if-else statement
  3. switch statement
Lets understand each in detail,

1) if Statement: The if statements is the simplest statement to make decisions. It is frequently used. It is used to check the condition using relational operators.It relays on the result of relational conditions that we pass in its syntax. If the relational condition return true then it execute the statements enclosed withing the if block.If condition is false it does not do anything and control moves to next line of code.
syntax of if statement

In the above syntax, the exp1 and exp2 are the operands and relational operator is used to compare these operands, you can use any relational operand like, >,<,<=,>=,== and  relational operators always give result in boolean value.

Working of if statement: 

working of if statement flowchart

Example:   Write a program to print smallest from two integers.

program to print smallest number using if statement in java


See the comments to understand code. Console is displaying output. If you want to use your console like in this program. Go to Console> Right Click> Detach. Now you can adjust its with and height by dragging.
  • Nested if Statement: Nested if means to write if statement inside another if statement. It makes a nest. It contains an outer if statement and an inner if statement. It can be used to check multiple conditions. Lets have a look at syntax and example for better understanding.
Nested if statement syntax

In the syntax, the outer if checks first condition when true it moves to inner if statement which checks other condition when true then it executes its block. We can have as many as inner statements But it is not recommended as it will make program too complex.The outer loop can have its block of statements.

Example:  Write a program to print smallest from three integers using nested if statement.

program to print smallest of three numbers using nested if statement.

It works same as if statement.If the condition becomes false it will not print anything.

2) if-else Statement: The if statement works in same way. The else is used to execute some code lines when the condition is false. As we know, if only executes its block when the condition is true.The if statement had limitation that when condition is false it doesn't do anything.To overcome this else statement came in to existence.
The if executes its block on correct condition and else execute its block on wrong or false condition.

Working of if -else statement: 

working of i-else statement flowchart


Example: This will contain two example to understand the if-else concept clearly.
  •  Write a program to print smallest from two integers using if-else statement.
program to print  the smallest number using if-else
In  the  example above, num1 =12 and num2= 5 which means num1 is not smallest . The smallest number is 2. So the if condition becomes false and control gets transferred to else statement which executes the else block and print num2 as the smallest number.

  • Write a program to print number is odd or even on screen.The number is 19.
program to print  the smallest number using if-else

An even number is always divisible by 2 and has 0 remainder when divided by 2.Try some other numbers as 8,13,10 etc.
  • Nested if-else Statement: Nested if-else is similar to nested if statement. It also have else blocks to handle false conditions.We can use nested statement to check multiple conditions.There are three different ways to write nested if-else statement.
nested if-else syntax
 There are three ways to write nested if-else,
  1. Method1: write inner if-else statement in outer if block.
  2. Method2: write inner if-else statement in outer else block.
  3. Method3: write inner if-else statements in both outer if block and else block.
Example:  Write a program to print smallest from three integers using nested if-else statement.

Smallest number using Nested if-else example in java

Always read comments in green color in the example because I explain code in comments.When first condition becomes false then it goes to outer else and perform the execution on inner if-else in its block.
  • if-else-if ladder: It works like a ladder.It is used to check many conditions simultaneously.Nested if-else becomes very complex on deeper level.To overcome the issue of complexity we can used if-else-if ladder to check multiple conditions.

if-else-if ladder syntax

Working of if-else-if ladder

  1. The first if statement checks the condition, if true execute its block and jump out of ladder and move to next statement of program and on false condition goes to next else-if.
  2. In case the first condition is false,the control moves to next else if(condition), which checks for other condition that can be true. If true it will execute its block and control gets transferred out of ladder to next statement.
  3. In case the previous else if condition is false,the control moves to next else if(condition), which checks condition that can be true. If true it will execute its block and control gets transferred out of ladder to next statement.
  4. This process goes on for all conditions in the ladder.
  5. In case, no condition matches your need and each condition becomes false then the default else block executed,this block will have a default statement which is used to tell user that program does not have such kind of condition to match your input.
Example: Let us understand if-else-if ladder with an example. Write program to calculate the grades of student using marks of three subject.
Programs to calculate grades using if-else-if ladder in java

Experiment: Practice all examples by yourself.

We will discuss switch Statement in next post.
Stay Tuned!Happy Coding!

Comments