Are You Feeling any difficulity while writing Core-Java Programs. Send those to me Here.

Control Structure

Control Structure

Unit Structure

4.1 Introduction
4.2 Control structure
4.2.1 Selection Statement
4.2.1.1 if statement
4.2.1.1.1 Simple if statement
4.2.1.1.2 The if…else statement
4.2.1.1.3 Nesting of if-else statement
4.2.1.2 switch statement
4.2.2 Iteration Statement
4.2.2.1 for loop
4.2.2.2 while loop
4.2.2.3 do-while loop
4.2.3 Jump in Statement
4.3 Summary

4.1 Introduction:

In Java, program is a set of statements and which are executed sequentially in order in which they appear. In that statements, some calculation have need of executing with some conditions and for that we have to provide control to that statements. In other words, Control statements are used to provide the flow of execution with condition.
In this unit, we will learn the control structure in detail.

4.2 Control Structure:

In java program, control structure is can divide in three parts:


  • Selection statement 
  • Iteration statement 
  • Jumps in statement


4.2.1 Selection Statement:

Selection statement is also called as Decision making statements because it provides the decision making capabilities to the statements.
In selection statement, there are two types:


  • if statement 
  • switch statement

These two statements are allows you to control the flow of a program with their conditions.

4.2.1.1 if Statement:

The “if statement” is also called as conditional branch statement. It is used to program execution through two paths. The syntax of “if statement” is as follows:
Syntax:
if (condition)
{
Statement 1; Statement 2;
...
}
else
{
Statement 3;
Statement 4;
... }
The “if statement” is a commanding decision making statement and is used to manage the flow of execution of statements. The “if statement” is the simplest one in decision statements. Above syntax is shows two ways decision statement and is used in combination with statements.
Following figure shows the “if statement”


4.2.1.1.1Simple if statement:

Syntax:
If (condition)
{
Statement block;
}
Condition ?
Statement-a;
In statement block, there may be single statement or multiple statements. If the condition is true then statement block will be executed. If the condition is false then statement block will omit and statement-a will be executed.
Following figure shows the flow of statement.

4.2.1.1.2 The if…else statement:

Syntax:
If (condition)
{
True - Statement block;
}
else
{
False - Statement block;
}
Statement-a;
If the condition is true then True - statement block will be executed. If the condition is false then False - statement block will be executed. In both cases the statement-a will always executed.
Following figure shows the flow of statement.


Following program shows the use of if statement.

Program: write a program to check whether the number is positive or negative.
import java.io.*;
class NumTest
{
public static void main (String[] args) throws IOException
{
int Result=11;
System.out.println("Number is"+Result);
if ( Result < 0 )
{
System.out.println("The number "+ Result +" is negative");
}
else
{
System.out.println("The number "+ Result +" is positive");
}
System.out.println("------- * ---------");
}
}
Output:
C:\MCA>java NumTest
Number is 11
The number 11 is positive
------- * ---------
(All conditional statements in Java require boolean values, and that's what the ==, <, >, <=, and >= operators all return. A boolean is a value that is either true or false. If you need to set a boolean variable in a Java program, you have to use the constants true and false. Boolean values are no more integers than are strings).
For example: write a program to check whether the number is divisible by 2 or not.
import java.io.*;
class divisorDemo
{
public static void main(String[] args)
{
int a =11;
if(a%2==0)
{
System.out.println(a +" is divisible by 2");
}
else
{
System.out.println(a+" is not divisible by 2");
}
}
}
Output:
C:\MCA>java divisorDemo
11 is not divisible by 2

4.2.1.1.3 Nesting of if-else statement:

Syntax:
if (condition1)
{
If(condition2)
{
Statement block1;
}
else
{
Statement block2;
}
}
else
{
Statement block3;
}
Statement 4;
If the condition1 is true then it will be goes for condition2. If the condition2 is true then statement block1 will be executed otherwise statement2 will be executed. If the condition1 is false then statement block3 will be executed. In both cases the statement4 will always executed.

For example:Write a program to find out greatest number from three numbers.

class greatest
{
public static void main(String args[])
{
int a=10;
int b=20;
int c=3;
if(a>b)
{
if(a>c)
Statement3
Statement2
Statement1
Statement4
Condition1
Condition2
false
false
true
true
{
System.out.println("a is greater number");
}
else
{
System.out.println("c is greater number");
}
}
else
{
if(c>b)
{
System.out.println("c is greater number");
}
else
{
System.out.println("b is greater number");
}
}
}
}
Output:
C:\MCA>java greatest
b is greater number

4.2.1.2 switch statement:

In Java, switch statement check the value of given variable or statement against a list of case values and when the match is found a statement-block of that case is executed. Switch statement is also called as multiway decision statement.
Syntax:
switch(condition)// condition means case value
{
case value-1:statement block1;break;
case value-2:statement block2;break;
case value-3:statement block3;break;

default:statement block-default;break;
}
statement a;
The condition is byte, short, character or an integer. value-1,value-2,value-3,…are constant and is called as labels. Each of these values be matchless or unique with the statement. Statement block1, Statement block2, Statement block3,..are list of statements which contain one statement or more than one statements. Case label is always end with “:” (colon).
Program:write a program for bank account to perform following operations.
-Check balance
-withdraw amount
-deposit amount
For example:
import java.io.*;
class bankac
{
public static void main(String args[]) throws Exception
{
int bal=20000;
int ch=Integer.parseInt(args[0]);
System.out.println("Menu");
System.out.println("1:check balance");
System.out.println("2:withdraw amount... plz enter choice and amount");
System.out.println("3:deposit amount... plz enter choice and amount");
System.out.println("4:exit");
switch(ch)
{
case 1:System.out.println("Balance is:"+bal);
break;
case 2:int w=Integer.parseInt(args[1]);
if(w>bal)
{
System.out.println("Not sufficient balance");
}
bal=bal-w;
System.out.println("Balance is"+bal);
break;
case 3:int d=Integer.parseInt(args[1]);
bal=bal+d;
System.out.println("Balance is"+bal);
break;
default:break;
}
}
}
Output:
C:\MCA>javac bankac.java
C:\MCA>java bankac 1
Menu
1:check balance
2:withdraw amount... plz enter choice and amount
3:deposit amount... plz enter choice and amount
4:exit
Balance is:20000
C:\MCA>java bankac 2 2000
Menu
1:check balance
2:withdraw amount... plz enter choice and amount
3:deposit amount... plz enter choice and amount
4:exit
Balance is18000
C:\MCA>java bankac 3 2000
Menu
1:check balance
2:withdraw amount... plz enter choice and amount
3:deposit amount... plz enter choice and amount
4:exit
Balance is22000
C:\MCA>java bankac 4
Menu
1:check balance
2:withdraw amount... plz enter choice and amount
3:deposit amount... plz enter choice and amount
4:exit
C:\MCA>java bankac

4.2.2 Iteration Statement:

The process of repeatedly executing a statements and is called as looping. The statements may be executed multiple times (from zero to infinite number). If a loop executing continuous then it is called as Infinite loop. Looping is also called as iterations.
In Iteration statement, there are three types of operation:


  • for loop 
  • while loop 
  • do-while loop

4.2.2.1 for loop:

The for loop is entry controlled loop. It means that it provide a more concious loop control structure.
Syntax:
for(initialization;condition;iteration)//iteration means increment/decrement
{
Statement block;
}
When the loop is starts, first part(i.e. initialization) is execute. It is just like a counter and provides the initial value of loop. But the thing is, I nitialization is executed only once. The next part( i.e. condition) is executed after the initialization. The important thing is, this part provide the condition for looping. If the condition will satisfying then loop will execute otherwise it will terminate.
Third part(i.e. iteration) is executed after the condition. The statements that incremented or decremented the loop control variables.
For example:
import java.io.*;
class number
{
public static void main(String args[]) throws Exception
{
int i;
System.out.println("list of 1 to 10 numbers");
for(i=1;i<=10;i++)
{
System.out.println(i);
}
}
}
Output:
C:\MCA>javac number.java
C:\MCA>java number
list of 1 to 10 numbers
1
2
3
4
5
6
7
8
9
10
Here we declare i=1 and then it check the condition that if i<10 then only loop will be executed. After first iteration the value of i will print and it will incremented by 1. Now the value of i=2 and again we have to check the condition and value of i will print and then increment I by 1 and so on.

4.2.2.2 while loop:

The while loop is entry controlled loop statement. The condition is evaluated, if the condition is true then the block of statements or statement block is executed otherwise the block of statement is not executed.
Syntax:
While(condition)
{
Statement block;
}
For example:Write a program to display 1 to 10 numbers using while loop.
import java.io.*;
class number
{
public static void main(String args[]) throws Exception
{
int i=1;
System.out.println("list of 1 to 10 numbers");
while(i<=10)
{
System.out.println(i);
i++;
}
}
}
Output:
C:\MCA>javac number.java
C:\MCA>java number
list of 1 to 10 numbers
1
2
3
4
5
6
7
8
9
10

4.2.2.3 do-while loop:

In do-while loop, first attempt of loop should be execute then it check the condition.
The benefit of do-while loop/statement is that we get entry in loop and then condition will check for very first time. In while loop, condition will check first and if condition will not satisfied then the loop will not execute.
Syntax:
do
{
Statement block;
}
While(condition);
In program,when we use the do-while loop, then in very first attempt, it allows us to get enter in loop and execute that loop and then check the condition.
Following program show the use of do-while loop.
For example:Write a program to display 1 to 10 numbers using do-while loop.
import java.io.*;
class number
{
public static void main(String args[]) throws Exception
{
int i=1;
System.out.println("list of 1 to 10 numbers");
do
{
System.out.println(i);
i++;
}while(i<=10);
}
}
Output:
list of 1 to 10 numbers
1
2
3
4
5
6
7
8
9
10

4.2.3 Jumps in statement:

Statements or loops perform a set of operartions continually until the control variable will not satisfy the condition. but if we want to break the loop when condition will satisy then Java give a permission to jump from one statement to end of loop or beginning of loop as well as jump out of a loop.
“break” keyword use for exiting from loop and “continue” keyword use for continuing the loop.
Following statements shows the exiting from loop by using “break” statement.
do-while loop:
do
{
………………
………………
if(condition)
{
break;//exit from if loop and do-while loop
}
……………..
……………..
}
While(condition);
………..
………..
For loop:
for(…………)
{
……………
…………..
if(…………..)
break; ;//exit from if loop and for loop
……………
……………
}
……………
…………..
While loop:
while(…………)
{
……………
…………..
if(…………..)
break; ;//exit from if loop and while loop
……………
……………
}
Following statements shows the continuing the loop by using “continue” statement.
do-while loop:
do
{
………………
………………
if(condition)
{
continue;//continue the do-while loop
}
……………..
……………..
}
While(condition);
………..
………..
For loop:
for(…………)
{
……………
…………..
if(…………..)
continue ;// continue the for loop
……………
……………
}
……………
…………..
While loop:
while(…………)
{
……………
…………..
if(…………..)
continue ;// continue the while loop
……………
……………
}
…………….
…………….
Labelled loop:
We can give label to a block of statements with any valid name.following example shows the use of label, break and continue.
For example:
Import java.io.*;
class Demo
{
public static void main(String args[]) throws Exception
{
int j,i;
LOOP1: for(i=1;i<100;i++)
{
System.out.println(““);
if(i>=10)
{
break;
}
for(j=1;j<100;j++)
{
System.out.println(“$ ”);
if(i==j)
{
continue LOOP1;
}
}
}
System.out.println(“ End of program “);
}
}
Output:
$
$ $
$ $ $
$ $ $ $
$ $ $ $ $
$ $ $ $ $ $
$ $ $ $ $ $ $
$ $ $ $ $ $ $ $
$ $ $ $ $ $ $ $ $
End of program

4.3 Summary:

In this unit, we covered Selection Statement, Iteration Statement and Jump in Statement.


  • In Selection statement, we covered if statement and switch statement with example. 
  • In Iteration Statement, we covered for loop, while loop and do-while loop with example.
  • In Jump in Statement, we covered break, continue and label with example.


No comments:

Post a Comment