CONTROL STATEMENT

The if Statement

In Java if statement is most widely used. The if statement is control statement which helps programmers in decision making. The if statement uses boolean expression in making decision whether a particular statement would execute or not. Lets look at syntax of if statement :


 Syntax :

if boolean-expression )
 {
   if-code;
 }

The if statement is a type of conditional branching statement. By the conditional branching we mean that branching of flow depends on some conditions. The decision making is done based on true or false. Looking at the above syntax and below diagram we can come to explanation of if statement. If the boolean-expression is true, if-code gets executed. If the boolean-expression is false rest_of_code gets executed.


Example if statement

class Hello 
{
int a=10;
public static void main(String[] args) 
{
if(a<15)
{
System.out.println("Hello good morning!");
}
}
}

Output

Hello good morning

The if-else statements

It is basically extension of if statement. Lets look at the syntax of the if-else statement.

if(boolean-expression)
{
  if-code;
}
else
{
  else-code;
}
If the boolean-expression is true if-code gets executed. If boolean-expression is false else-code gets executed. In this decision making statements generally either of the block gets executed whether its if-code or else-code, after either execution rest-code gets executed.

Example if else

import java.util.Scanner;
class Oddeven 
{
public static void main(String[] args) 
{
int no;
Scanner s=new Scanner(System.in);
System.out.println("Enter any number :");
no=s.nextInt();
if(no%2==0)
{
System.out.println("Even number");
}
else
{
System.out.println("Odd number");
}
}
}

Output

Enter any number : 
10
Even number


The Nested if-else statements


The Nested if-else statements are used where we want to make series of decision making. Thus one if-else is nested within another if-else. Therefore nested if-else give us to make a decision within a decision already taken. Lets look at the syntax of nested if-else statements


nested

 Syntax :

 if(boolean-expression 1)
{
  if(boolean-expression 2)
  {
    statement 1;
  }
  else
  {
     statement 2;
  }
}
else
{
  statement 3;
}


Lets analyze about syntax and flowchart. Nested if-else syntax is very much. Generally if you look at the flowchart you will find that there are two if-else statements. You can nest them to n number. For the sake of simplicity we have taken only two if-else. Logic implemented in above syntax is like if boolean-expression 1 is false, statement - 3 gets executed and then program flows to rest of code. But if the boolean-expression 1 is true the controls flow to another expression called as boolean-expression 2. Then the same code follows if boolean-expression 2 is evaluated as false statement - 2 gets executed and then to rest of the code. But if boolean-expression 2 is true, statement - 1 is executed and then follows to rest of code.



Else-if Ladder Statements


Below diagram shows else-if ladder statements. Else-if ladder is generally used when we want to take multiple decision. These multiple decision are in chains of if followed by else-if statements till n number. Lets us look at flowchart and syntax associated with else-if ladder.



 Syntax :

if(boolean-expression 1)
{
   statement-1;
}
else if(boolean-expression 2)
{
    statement-2;
}
else if(boolean-expression n)
{
    statement-n;
}      
else
{
     default-statement;   
}


Lets analyze the syntax and flowchart of Else-if ladder. The multiple decision making starts from top and flows towards bottom. The ladder works very simple and straight way. Whenever true condition is evaluated the statement below if gets executed and rest-of-code execution follows and whenever false condition is evaluated control flows towards next condition till default statement gets executed followed by rest-of-code.



The Switch Statements


We saw how if-else statement worked when making a multiple decision. But this if-else is limited to use in complex scenarios. Java provides a sensational switching technique when making a multi-way decision. This switch statement evaluates an expression and based on the result of expression chooses a path that matches one of the list values. The list values are called as Cases. The expression value is matched against this cases, and which ever case match the control moves to that switch. Lets look at the syntax and flowchart for the switch statement:


 Syntax :

switch(expression)
{
  case value-1:
              block-1
              break;
  case value-2:
              block-2
              break;
  default:
             default-block
             break;
}
Lets look at how switch statement works in Java. Switch statement first starts with a keyword switch followed by an expression. The Java code for expression must be integer or character expression. The value of expression must be an Integral constant. There are various case labels such as value-1, value-2 etc. These case labels must be evaluable to integral constants. When the switch is executed, value of expression is compared with case labels value starting from top and moving towards bottom. If any of case labels that matches with expression, the block of code that follows case gets executed. After block of code gets executed there is a break statement at the end of block code which breaks the switch statement and transfer control to rest-of-code to execute. If break statement is not written the control starts compairing with next case that follows. At the last default is an optional case which is placed when no case matches with expression. If no expression value matches with case labels value than default case is triggered and code associated with it gets executed.

Example

case k>=20: // not allowed

Example of switch case

import java.util.*;

class switchCase
{
public static void main(String arg[])
{
int ch;
System.out.println("Enter any number (1 to 7) :");
Scanner s=new Scanner(System.in);
ch=s.nextInt();
switch(ch)
{
case  1:
System.out.println("Today is Monday");
break;
case  2:
System.out.println("Today is Tuesday");
break;
case  3:
System.out.println("Today is Wednesday");
break;
case  4:
System.out.println("Today is Thursday");
break;
case  5:
System.out.println("Today is Friday");
break;
case  6:
System.out.println("Today is Saturday");
break;
case  7:
System.out.println("Today is Sunday");
default:
System.out.println("Only enter value 1 to 7");
}
}
}

Output

Enter any number (1 to 7) :
5
Today is Friday

No comments:

Post a Comment