Programs Using Control Statements

Control Statements

Control Statements: These are used to modify the flow of execution and give better control over the program.

Types of control Statements :
  1. Conditional Statements
  2. Looping Statements
  3. Branching Statements 
  4. Break Statements.
  5. Return Statements.
1.Conditional Statements : Depending on the condition the corresponding statements will be executed .

Types Of Conditional Statements:
  1. Simple if..
  2. if ....else...
  3. else...if....
  4. Nested If...
Simple If: Even we have "n" conditions each and every condition will be mentioned in separate if  Blocks. Each and every condition will be checked during the execution even though if any condition is satisfied all the conditions will be checked by compiler .

In this conditions will returns a boolean values (true/false). If condition is true statements are executed else check for the other conditions .

Syntax:

if(Condition 1)
{
Statements;
}

if(Condition 2)
{
Statements;
}

Program Using Simple-If 

import java.util.*;

class SimpleIf

{

public static void main(String args[])

{

int a;

int b;


Scanner sc=new Scanner(System.in);


System.out.println("enter the 1st number:");
a=sc.nextInt();

System.out.println("enter the 2nd number:");

b=sc.nextInt();

if(a<b)
{
System.out.println("a is smaller and b is larger number");
}
if(a>b)
{
System.out.println("b is smaller and a is larger number");
}
if(a==b)
{
System.out.println("a and b are equal numbers");
}
}
}

Save As:

SimpleIf.java

Compile:

javac SimpleIf.java

Run:

java SimpleIf

Input:

Enter the 1st number:
10
Enter the 2nd number:
20

Output:

a is smaller and b is larger number

Note :

Simple if  takes more processing time because each and every If block conditions must be checked by the compiler .


if .... else.... Statement: This Statements works depending on the condition . If the condition is true it will be executed otherwise else Statements will be executed.

In this conditions will returns a boolean values (true/false). If condition is true statements are executed else check for the other conditions . 
At last if no condition is true then an else condition which acts as a default block will be executed.

Syntax:

if(condition 1)
       Statements;
}
if(condition 2)
{
     Statements;
}
else
{
       Statements;
}

Programs Using if-else




import java.util.*;

class IfElse
{
public static void main(String args[])
{
int a;
int b;
Scanner sc=new Scanner(System.in);

System.out.println("enter the 1st number:");
a=sc.nextInt();

System.out.println("enter the 2nd number:");

b=sc.nextInt();

if(a<b)
{
System.out.println("a is smaller and b is larger number");
}
else
{
System.out.println("b is smaller and a is larger number or both may be equal");
}
}
}


Save As:

IfElse.java

Compile:

javac IfElse.java

Run:

java IfElse

Input:

Enter the 1st number:
10
Enter the 2nd number:
2

Output:

b is smaller and a is larger number or both may be equal.

Note :

 if else takes less processing time compared to Simple-If , because one condition is reduced to check that is else block which is a default block.

Else-if Statements: Here the conditions will be checked sequentially until one condition is true and remaining blocks will not be processed if any condition is Satisfied(true) .
If no condition is true then it will execute the statements in else block(default block).

Syntax:

if(Condition 1)
{
Statements;
}

else if(Condition 2)
{
Statements;
}
else if(condition 3)
{
  Statements;
}
else
{
  Statements;
}
   

Programs Using else-

if

 


import java.util.*;

class ElseIf

{

public static void main(String args[])

{

int a;

int b;


Scanner sc=new Scanner(System.in);

System.out.println("enter the 1st number:");
a=sc.nextInt();

System.out.println("enter the 2nd number:");

b=sc.nextInt();

if(a<b)
{
System.out.println("a is smaller and b is larger number");
}
else if(a>b)
{
System.out.println("b is smaller and a is larger number ");
}
else
{
System.out.println("a and b are equal");
}
}
}

Save As:

ElseIf.java

Compile:

javac ElseIf.java

Run:

java ElseIf

Input:

Enter the 1st number:
10
Enter the 2nd number:
10

Output:

a and b both are equal.

Note :

else if takes less processing time compared to Simple-If and if -else because if one condition is true it will neglect all the other statements . If all the conditions are false then it will execute the else Block which is a default block. 

We can write any number of else-If blocks based on the conditions.

Nested If: If block in another if block is called as Nested if . If the condition in 1st block is true then it move and execute the inner blocks otherwise move out of the block .

Syntax:

if(condition 1)
{
    statements;
     if(condition 2)
     {
      Statements;
}
else               //else block for inner if block// 
 {
   statements;
}
}
else           /*else block for outer if block*/ 
{
   statements;
}


Programs Using Nested If 

import java.util.*;
class NestedIf
{
public static void main(String args[])
{
int a=0;
int b=0;

Scanner sc=new Scanner(System.in);

System.out.println("enter the 1st number:");

a=sc.nextInt();

System.out.println("enter the 2nd number:");

b=sc.nextInt();

if(a==10)
{
if(a<b)
{
System.out.println("a is smaller and b is larger number");
}
else if(a>b)
{
System.out.println("b is smaller and a is larger number ");
}
else
{
System.out.println("a and b are equal");
}
}
else
{
System.out.println("a is not equal to value 10");
}
}
}

Save As:

NestedIf.java

Compile:

javac NestedIf.java

Run:

java NestedIf

Input:

Enter the 1st number:
10
Enter the 2nd number:
20

Output:


a is smaller and b is larger number.

Note:

Nested-if can be used when one condition is depending on the other condition. 

2.Looping Statements: These Statements are used , when we want to execute set of statements for large number of times then we must use this looping statements .

we have different types of looping statements they are :
  1. While loops
  2. do-While loop
  3. For loop
Each and every looping statements must contain 3 things i.e... Initialization,condition, Incrementation / decrementation 

While loop : A condition Statement is enough to iterate the set of Statements in while loops . This loop depends only on condition.

Syntax:

initialization ;

while(condition )
{
   Statements;
   increment/decrement operation;
}

Programs Using While loop



/*program for sum of numbers*/
class WhileLoop 
{
public static void main(String[] args) 
{
int i=0;

int sum=0;

while(i<10)
{
sum=sum+i;

i++;
}

System.out.println("Sum="+sum);
}
}

SaveAs:


WhileLoop.java

Compile:

javac WhileLoop.java

Run:

java WhileLoop

Output:

Sum=45.

Note:

The while condition must be satisfied otherwise compilation error (the statements wont be executed which are present below while loop).

Do-While:It is also same as while loop but the statements will execute atleast once even the condition is false . Do Statements will be executed first then checks the condition otherwise it will execute other statements present below the loop.

Syntax:

initialization;
do

     Statements ;
      increment /decrement operations;
}
while (condition);




Programs Using While loop

/*program for sum of numbers*/
class DoWhileLoop 
{
      public static void main(String[] args) 
{
int i=0;

int sum=0;

do
{
sum=sum+i;

i++;
}

while(i<10);
System.out.println("Sum="+sum);
}
}


SaveAs:

DoWhileLoop.java

Compile:

javac DoWhileLoop.java

Run:

java DoWhileLoop

Output:

Sum=45.

Note:

Do-while is used when we want to execute some set of statements before checking the condition atleast once.It is also dependent on the condition same as while loop after executing once .

Problems caused by while and do-while loops :


  1. The whole control will not be in the hands of loop.
  2. Initialization value may be changed.
  3. Leads to wrong way of execution.
  4. To solve these problems we come across FOR loop.
For Loop:

Initialization , condition , Increment/ Decrement will be within the loop.Whole control will be in the hands of FOR loop.

Syntax: 

for(initialization ; condition ; increment/decrement)
{
        statements;
}



Programs Using For loop


class ForLoop 
{
public static void main(String[] args) 
{
int sum=0;

for(int i=0;i<10;i++)
{
sum=sum+i;
}

System.out.println("Sum="+sum);
}

}



SaveAs:

ForLoop.java

Compile:

javac ForLoop.java

Run:

java ForLoop

Output:

Sum=45.

INFINITE LOOP


It can be defined as the number of statements can execute infinite times and does not comes out of the loop . Such loops are called as infinite loop .

Syntax:

for(;;)
{
    Statements;
}

while(;;)
{
    Statements;
}

do
{
    Statements;
}
while(;;);

all the above syntax's are for the infinite loops.

Programs Using "infinite  for" loop

class InfiniteForLoop 
{
public static void main(String[] args) 
{
for(;;)
{
System.out.println("void Main Technologies");
}
}
}

SaveAs:

InfiniteForLoop.java

Compile:

javac InfiniteForLoop.java

Run:

java InfiniteForLoop

Output:

void Main Technologies
void Main Technologies
void Main Technologies
void Main Technologies
void Main Technologies
.................................... infinite times it will be executed .

Note:

same functionality will exist for all the loops . Used rarely.

Branching Statements: when have number options used select and execute based on the user options then we come across Branching statements. 

In this when the user enters the choice then an appropriate case must be executed based on the user  . Sometimes there is a chance of enter the cases that are not present in the program then a notification or a message must be displayed to the user such cases are called as default statements.

Switch is the Branching statements which contains case statements and Default Statements by using the keywords "case" and "default".

Syntax :

switch(choice)
{
    case 1:
               statements;

      case 2:
                  statements;
        ................
        ................
        case n:
                    statements;
         default :
                       statements;
}


Programs Using Switch 

import java.util.*;

class Switch 
{
public static void main(String[] args) 
{

System.out.println("1.JAVA\n2.C\n3..NET\n");
           
                 System.out.println("Enter your choice :");

Scanner sc=new Scanner(System.in);

int choice=sc.nextInt();

switch(choice)
{
case 1:

System.out.println("You have selected for JAVA");
case 2:

System.out.println("You have selected for C");
case 3:

System.out.println("You have selected for .NET");
default:
System.out.println("you have entered wrong choice");

}
}
}



SaveAs:

Switch.java

Compile:

javac Switch.java

Run:

java Switch

Input:

1.JAVA
2.C
3..NET

Enter your choice:

2

Output:

You have selected for C
You have selected for .NET
You have entered wrong choice.

Note :
The problem in the above program is all the cases which are below the selected cases are executing along with default statements . So to avoid this we come across the statements called Break Statements.

Break Statements:This statements are used to terminate the execution of statements which are present below the break statement .
This statements can be given by keyword called "break".
Break keyword can be used anywhere in the program when we want to terminate the execution of statments.

Syntax:

{
statements;
break;
}

Programs Using BREAK statement in Switch 



import java.util.*;

class Switch 
{
public static void main(String[] args) 
{

System.out.println("1.JAVA\n2.C\n3..NET\n");
           
                 System.out.println("Enter your choice :");

Scanner sc=new Scanner(System.in);

int choice=sc.nextInt();

switch(choice)
{
case 1:

System.out.println("You have selected for JAVA");
                                         break;

case 2:

System.out.println("You have selected for C");
                                        break;

case 3:

System.out.println("You have selected for .NET");
                                        break;
default:
System.out.println("you have entered wrong choice");

}
}
}


SaveAs:

Switch.java

Compile:

javac Switch.java

Run:

java Switch

Input:

1.JAVA
2.C
3..NET

Enter your choice:

2

Output:

You have selected for C


Note :In the above program we have used a keyword called "break" keyword so the case we have selected will be executed and whenever it reaches the break statement the execution will be terminated and comes out of switch statements only by executing the given choice .

Return Statements: This Statements are used in the methods to return the value of a method to the caller .
when we use the datatype other than void type  we must return the value from such methods to the caller.
Return statements can be used by using a keyword called "return".
 Return value depends on type of datatype used for method (eg: for integer return value=zero). 
Syntax:

<datatype> MethodName()
{
     Statements;
     return value;        
}


Programs Using Return statement in Method. 



class MethodDefination
{
String a;
String b;

int returnMethod(String a,String b)
{
  this.a=a;
  this.b=b;

  System.out.println("InstituteName:"+a);
  
  System.out.println("Location:"+b);

  return 0;
}
}

class Return 
{
public static void main(String[] args) 
{
MethodDefination md=new MethodDefination();

md.returnMethod("VoidMain Technologies","Dilsuknagar");
}
}

SaveAs:

Return.java

Compile:

javac Return.java

Run:

java Return

Output:

InstituteName:void Main Technologies
Location: Dilsuknagar.




Comments

Popular posts from this blog

Programs on Inheritance

Arrays In Java

Java Inner Classes