Home
| | Sitemap
||Page number :14
Loops and Conditional Statements
Look at any program you have created till now. You can see that the execution begins with the main method. Other functions are called sequentially and after the execution of each and every statement, the program ultimately terminates. The “Real World” programs are entirely different. They respond to users actions. Explaining it in a better and technical way, they execute in a conditional manner.
Take an example our world, we interact with our environment and our behavior is determined by the conditions around us. You laugh at a clown’s joke, but don’t even dare to smile in front of your principal! This is what a conditional response. We can also design programs in such a way so that they take a step according to the conditions (Input). Imagine that you have created a tax calculating program. According to the different pay scales, taxes which are to be paid differ. A man earning a meager Rs.4000 a month has a total annual pay which amounts negligible in comparison to the tax paid by a person earning Rs.300000 a month!! Thus your program should be capable enough to compute the tax according to the condition i.e. the basic monthly pay. Like many other languages Java too handles condition using if-else and switch statements.
Conditional Statements---
- if-else statement
- switch
If Statement
If statement’s general format….
If (condition) { //Statements…... }
Let’s study this example---
class cond { pubic static void main(String args[]) { int inp=10; if(inp==06) { System.out.println("Hello Special Number"); } System.out.println("Nothing Special"); } } As the variable “inp” is initialized with 10, and the condition (inp==6) is false. The statements inside the “if block” are not executed. Thus you can see that our program is executing in a conditional manner.
If-Else Statements
Read this statement carefully “If the weather is bad I will not visit you else I will surely come”. After reading this statement you can easily understand the context in which “else” is used.
class cond2 { public static void main(String args[]) { int x; x=Integer.parseInt(args[0]); if(x==1) System.out.println("You are the no. one"); else Sysem.out.println("You are not the no. one"); } }
This program is unable to demonstrate the power of “else”. The reason lies on the fact that this program we have not used else with if. The next example will do this.
If-else If
class cond3 { public static void main(String args[]) { int inp; inp=Integer.parseInt(args[0]);
if(inp==1) System.out.println("You the No. 1");
else if(inp==2) System.out.println("No.------ 2"); else if(inp==3) System.out.println("No.-------3"); else System.out.println("You are not among the top three"); } }
Look at the command prompt, the code executes strictly according to the condition. If the input is 1,2 and 3 the output is unique, but for all other inputs the program prints the same Line "You are not among the top three". You can see the use of “if-else” together.
REMEMBER::: You can even construct the program mentioned above without else. The use of “else” just makes our program readable.
Working with “&’ Operator
Imagine a garments store where you get discount and points for your patronage on your shopping. The more you purchase the more “ discount and points” you are awarded.
Look at a program developed to assist the Shopkeeper
class cond4 { int money; int dis; int points; public cond4(int temp) { money=temp; } public void calc() {
if(money>0 && money<=1000) { dis=3*money/100; points=money/10; System.out.println("Discount Offered is "+dis); System.out.println("Points Earned are "+points); } else if(money>1000 && money<=3000) { dis=8*money/100; points=money/10; System.out.println("Discount Offered is "+dis); System.out.println("Points Earned are "+points); } else if(money>3000 && money<=5000) { dis=15*money/100; points=money/10; System.out.println("Discount Offered is "+dis); System.out.println("Points Earned are "+points); } } }
class store { public static void main(String args[]) { int val; val=Integer.parseInt(args[0]);
cond4 obj=new cond4(val); obj.calc(); } }
By using the “&” operator we can successfully work with a range of values. Similarly if needed, we can even use Or(||) and Not (!) operator.
page 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29