CONDITON
CHECKING
There are three programming construct followed by java
programming language
Ø SEQUENCE
Ø SELECTION/CONDITION
CHECKING
Ø REPETITION/ITERATION
3.1 SEQUENCE: -
Sequence indicates the flow of control passes from one instruction to the next
in sequence. In other word statement executes one after other in an order in
which they are written.
3.2 SELECTION;-It
indicates the flow of control depends upon condition. If one condition is true
then one set of statement will execute otherwise another set of statement will
execute.
There are two type of selection
i.
single selection structure e.g if –else
ii.
multiple
selection structure e.g switch case
3.3 REPETITION; -
It indicates that some set of statements is to be executes till particular
condition is been satisfied. In other words if a statement executed more than
once, the technique of repetition is applied.
There are three iteration statements;-
While loop
do while loop
for loop
3.4 IF STATEMENT; -
the if statement allows branching (decision making) depending upon the value of
the variable.
Syntax :- if(condition)
{
Statement _1(s);
}
else
{
statement_2(s)
}
=>Here condition must be given in such a manner so that
it returns Boolean value.
=>If condition
will be true then statement_1 will execute otherwise statement_2 will execute.
=>else is optional, we can have if without else.
Example 1
// program to find
higher number out of two number.
class check1
{
public static void main(String args[])
{
int x=10,y=30;
if(x>y)
{
System.out.println(“
higher no is ”+x);
}
else
{
System.out.println(“
higher no is ”+y);
}
}
}
// output is
higher no is 30
Example 2
// program to find
absolute value of number
class check2
{
public static void main(String args[])
{
int x=-25;
if(x<0)
{
x=x * -1;
}
System.out.println( “absolute value of x is ”+x);
//Output is
Absolute value of x is 25
Ø here
is x is a positive number the condition will become false and x will remain
positive so no operation needed to take
place that is why else block is not used.
Example 3
// program to find character
ch is alphabet or not.
class check2
{
public static void main(String args[])
{
char ch=’B’
if((ch>=’a’
&& ch<=’z’) && (ch>=’A’ && ch <=’Z’))
System.out.println( ch+” is an alphabet”):
else
System.out.println( ch+” is not an alphabet”):
}
}
//output
B is an alphabet
Ø If
single statement is to be followed by an if or else block then curly brackets
can be omitted.
3.5 NESTED IF:-
Nested if/else structures test for multiple causes by placing if/else selection
inside if/else selection structure. The if/else selection structure uses the
condition till three levels. The following illustrates if format
if(condition)
{
if(conditions)
{
Statement(s)
}
else
{
Statement(s)
}
} //end of if
else
{
// another
if else block
}
Example 4
// program to find
highest of three number using nested if
import java.util.*;
class check2
{
public static void main(String args[])
{
Scanner Sn=new Scanner(System.in));
int x,y,z,m;
Systemout.println(“enter three number”);
x=Sn.nextInt();
y= Sn.nextInt();
z= Sn.nextInt();
if(x>=y)
{
if(x>=z)
m=x;
else
m=z;
}
else
{
if(y>=z)
m=y;
else
m=z;
}
System.out.println(“highest
number is ”+m);
}
}
// above program will input 3 number from user if x is
greater than y then x will be compared
with z if this condition is also true then it will display x otherwise it will
display z else if x is less then y then y will be compared with z and depending
upon condition given in else block y or z will be displayed.
Example 6:-
// program to find
calculate net salary of the employee depending upon basic salary.
Basic DA HRA
Less than 2000 10% 5%
Greater than equal to 2000 25% 15%
But less than 4000
Greater than equal to 4000 30% 25%
But less than 8000
Greater than 8000 40% 30%
import java.util.*;
class check2
{
public
static void main(String args[]) throws Exception
{
Scanner Sn=new Scanner(System.in));
int basic;
double da,hra,net;
Systemout.println(“enter basic salary”);
basic=Sn.nextInt();
if(basic<2000)
{
da=0.10*basic;
hra=0.05*basic;
}
else if(basic<4000)
{
da=0.25*basic;
hra=0.15*basic;
}
else if(basic<8000)
{
da=0.30*basic;
hra=0.25*basic;
}
else
{
da=0.40*basic;
hra=0.30*basic;
}
net=basic+da+hra;
System.out.println(“Basic Salary “+basic);
System.out.println(“Dearness
allowance “ + da);
System.out.println(“House rent “+hra);
System.out.println(“Net Salary “+net);
}
}
Example 7:-
A cloth showroom has announced the following discounts on the purchase
of items, based on the total cost of the item purchased:-
Total Cost Discount(in
percentage)
Less than Rs 2000 5%
Rs 2001 to Rs. 5000 25%
Rs. 5001 to Rs 10000 35%
Above Rs. 10000 50%
Write a program to input the total cost and to compute and display the
amount to be paid by the customer after availing the discount. [2006]
import java.util.*;
class prog2
{
public static void main(String args[]) throws Exception
{
Scanner Sn=new Scanner(System.in));
int cost;
double amt,dis;
Systemout.println(“Enter Cost to the item”);
cost=Sn.nextInt();
if(cost<=2000)
dis=cost*0.05;
else if(cost<=5000)
dis=cost*0.25;
else if(cost<=10000)
dis=cost*0.35;
else
dis=0.50;
amt=cost-dis;
System.out.println(“Amount to be
paid “+amt);
}
}
3.6 SWITCH CASE:- the
switch case is multiple branching statement used to compare one variable with
many constants but only for equality.
Syntax:-
switch (expression)
{
case <value1>
: expression(s)
break;
case <value2>
: expression(s)
break;
:
:
default :
expression(s)
}
Example 8:-
program to input two
numbers and an operator perform arithmetic operation as per operator
import java.util.*;
class check7
{
public
static void main(String args[]) throws Exception
{
Scanner Sn=new Scanner(System.in));
int a,b;
char op;
System.out.println(“enter 2 numbers and one operator”);
a= Sn.nextInt();
b= Sn.nextInt();
op=Sn.next().charAt(0);
switch(op)
{
case ‘+’:
System.out.println(a+b);
break;
case ‘-’:
System.out.println(a-b);
break;
case ‘*’: System.out.println(a*b);
break;
case ‘/’: if(b==0)
System.out.println(“divide by zero”);
else
System.out.println(a/b);
break;
default : System.out.println(“invalid operator”);
break;
}
3.7 When is it better
to use switch statement instead of if ? why?
OR
COMPARE switch WITH
if
The switch statement is java’s multiple branch selection
statement. It provides an easy to dispatch execution to different parts of the
code bases on the value of a expression. It provides a better alternative for a
large series of if-else-if statement. Some important features of switch
statement are listed below:-
- The switch statement can only test for equality, the statement can evaluate any type of Boolean expression.
- The switch only look for a match between the value of the expression and one of its case constants values. The if-else statement uses a series of expression having unrelated variable
- No two case constants in a switch can have identical values.
- switch case can work only upon int and char data type where as if else can work with all data types.
3.8 What is the
effect of absence of break statement in switch case statement?
Break is to terminate the sequence , without break each and
every following case of the switch statement will be executed. This situation
also known as Fall through.
Eg.
switch(ch)
{
case ‘a’ : System.out.println(“A says Apple”);
case ‘e’ : System.out.println(“E says Egg”);
case ‘i’ : System.out.println(“I says Ink”);
}
When user input a into ch the output will be as follows
A says Apple
E says Egg
I says Ink
3.9 Explain with the
help of an example each of the following in a switch-case statement
i.
break
ii.
default [2005]
i.
break:- the keyword break is used with the switch
statement to break the sequential control of execution and brings the control
to the statement immediately following the switch block.
If break is missing then fall
through takes place.
ii.
Default is optional statement, it executed the
statements when no choice is satisfied. .i.e. when data with the switch does
not match with any of the cases.
If default is missing then no
action takes place if all case matches fail.
Example.
switch(var)
{
case 1: System.out.print(“one”);
break;
case 2: System.out.print(“two”);
break
default: System.out.print(“none”);
break;
}
if the value of variable is 1 then it will display one and follow the statement given after
switch.
If value of the variable is other than 1 or 2 then is will
follow default and display none.
3.10 Differentiate between if and switch
statements. [2006]
If can use relational or logical operator
where as switch can compare only for equality.
If can use any type of variable
for comparison where as switch can use only int and char data types.
3.11 Turnary to if else
e.g. 1-> x=(x<0)? X*-1 : x;
Ans:- if(x<0)
x=x*-1;
else
x=x;
e.g.2 -> c=(a!=b)?
a :b;
Ans if(a!=b)
c=a;
else
c=b;
Do yourself
a)
System.out.println(x%2==)?”even”:”odd”);
b)
Tax=value<=10000?0:0.10*value
c)
System.out.println(n%4==0?”leap year”:”not
leap year”);
d)
grade=mark>=80?’A’:mark>=60?’B’:
mark>=40 ’C’:’D’;
3.12 if else to using turnary
e.g. 1 if(x>10)
y=y+1;
else
y=y+5
Ans:-
y+=x>10? 1 : 5 ;
Do your self
A) if(y%4)
flag=1;
else
flag=0;
B) if(mark>=80)
grade=’A’;
else
if(mark>=60)
grade=’B’;
else
grade=’C’;
C) if(per>=40)
System.out.println(“pass”);
else
Systen,out.println(“fail”);
c)
if(sale<=5000)
comm.=0.05*sale;
else if (sale<=10000)
comm.=0.10*sale;
else
comm.=0.15*sale;
3.13 if else to switch case
if(grade==’A’)
System.out.println(“Excellent”);
else if(grade==’B’)
System.out.println(“Well
Done”);
else if(grade==’C’)
System.out.println(“Satisfactory”);
else if(grade==’D’ || grade==’E’)
System.out.println(“Work
Hard”);
else
System.out.println(“Failed”);
Using switch case
switch(grade)
{
case ‘A’: System.out.println(“Excellent”);
break;
case ‘B’:System.out.println(“Well Done”);
break;
case ‘C’: System.out.println(“Satisfactory”);
break;
case ‘D’:
case ‘E’: System.out.println(“Work Hard”);
break;
default: System.out.println(“Failed”);
break;
}
3.14 switch case to if else
EXAMPLE 1
switch(ch)
{
case ‘a’:
case ‘A’: System.out.println(“ AIRTEL”);
break;
case ‘e’:
case ‘E’: System.out.println(“E-Comm”);
break;
case ‘i’:
case ‘I’: System.out.println(“IDEA”);
break;
case ‘o’:
case ‘O’: System.out.println(“ORANGE”);
break;
case ‘u’:
case ‘U’: System.out.println(“U-tube”);
break;
default: System.out.println(“not vowel”);
break;
}
using if else
if(ch==’a’ || ch==’A’)
System.out.println(“ AIRTEL”);
else if(ch==’e’ || ch==’E’)
System.out.println(“E-Comm”);
else if(ch==’i’ || ch==’I’)
System.out.println(“IDEA”);
else if(ch==’o’ || ch==’O’)
System.out.println(“ORANGE”);
else if(ch==’a’ || ch==’A’)
System.out.println(“U-tube”);
else
System.out.println(“Not
vowel”);
EXAMPLE 2
switch(grade)
{
case ‘A’: switch(slab)
{
case 1: sal+=4000;
break;
case 2:
sal+=3000;
break;
}
break;
case ‘B’:switch(slab)
{
case 1:
sal+=2000;
break;
case 2:
sal+=1000;
break;
}
break;
}
using if else
if(grade==’A’)
{
if(slab==1)
sal+=4000;
else if (slab==2)
sal+=3000;
}
else if(grade==’B’)
{
if(slab==1)
sal+=2000;
else if(slab==2)
sal+=1000;
}
No comments:
Post a Comment