21
OBJECT: TO
DECISION
BECOME FAMILIAR WITH
DECISION
MAKING STATEMENTS.
MAKING STATEMENTS
Decision making statements are the statements which take the decisions to make the conditions satisfied and execute their body. If the condition is satisfied (True) then the statement will cause its body to be executed and if the condition is not satisfied (False) then the control will be transferred to the next condition or to the first statement after the body of the conditional statement. There are three types of decision making statement also known as conditional statements. These are so called conditional statements because they depend on certain conditions. They are,
1) 2) 3)
The if Statement The if-else Statement The switch Statement
1)
The if Statement
It is the simplest decision making/conditional statement. The if statement contains the keyword if which is predefined in C++ followed by the condition in the parentheses () with some relational and logical operators. If the condition is satisfied then the body of the if statement will be executed else the control will be transferred to the first existing statement after the if statement. If the body of the if statement contains only single statement then you dont have to enclose that statement in the braces but in case of multiple statements you should enclose the block of code in the curly braces. The syntax of the if statement is given below;
Test Expression
if (a>b && a>c) statement;
Test Expression
Single Statement if body
if (a>b && a>c) { statement; statement; statement;
Multiple Statement if body
22
FLOW
}
CHART OF IF
STATEMENT
False Test Expression
True
Body of if
Exit
Program (If.cpp)
#include <iostream.h> #include <conio.h> void main() { int Age1=20, Age2=15, Age3=7; if(Age1>Age2&&Age1>Age3) { cout<<Person with Age1 is elder<<endl; cout<<Persons with Age2 and Age3 are younger; } getch(); }
2) The if-else Statement The if-else statement is another decision making statement. The if-else statement contains the keywords if and else which are predefined in C++. The condition is contained in the parentheses () after the keyword if. If the condition is satisfied then the block of code after the keyword if is executed while if the condition is not satisfied then the control is transferred to the else and the block of code contained after it is executed. If you want to execute the single statement if the condition is either satisfied or not then you dont have to enclose it in the curly braces but if you want to execute certain block of code (multiple statements) then you have to enclose the block of code in the curly braces. The syntax of the if-else statement is given below;
Test Expression
if (a>b && a>c) statement; else statement;
Single Statement else body Single Statement if body
Test Expression
if (a>b && a>c) { statement; statement; statement; } else { statement; statement; statement;
Multiple Statement else body Multiple Statement if body
23
}
FLOW
CHART OF IF-ELSE
STATEMENT
False Test Expression
True
Body of if
Body of else
Exit
Program (IfElse.cpp)
#include <iostream.h> #include <conio.h> void main() { int num1=100, num2=50; if(num1>num2) { cout<<First number is greater<<endl; cout<<Second number is smaller; } else { cout<<First number is smaller<<endl; cout<<Second number is greater; } getch(); }
24
3) The switch Statement The switch statement is also the decision making statement like if statement and if-else statement. The switch statement is used when the bunch of decisions depend on the same variable. It works as if and if-else statements but it is some what clearer and easily understandable at glance. The switch statement starts with the keyword switch (lower case) followed by the variable in the parentheses (). The switch statement takes the decision according to the values of the variable in the parenthesis. The whole body of the switch statement is enclosed by curly braces. Then the switch statement matches the values of the variable with the cases in its body. The keyword case followed by the choice and the colon makes the block of code below it to be executed if the value of the switch variable matches with the choice in the case. You can give multiple cases in the switch statement and the switch statement will try to match the value of the variable with the cases and if no case is matched the control is returned to the default. The key word default followed by the colon causes the block of code below it to be executed if the switch statement does not match any of the case. The break statement is used after the block of code in each case, which causes the control to be transferred out of the switch statement when ever the case executes its block of code. The syntax of the switch statement is given below;
Integer or character variable Note: no semicolon here switch (ch) Integer or character constant { case 1: statement; First case body statement; Causes exit from switch break; case 2: statement; Second case body statement; break; case 3: statement; Third case body statement; break; default: statement; Default case body statement; Note: no semicolon here
25
}
FLOW
CHART OF SWITCH STATEMENT
Switch variable equals 1st case constant
True
1st case body
False
Switch variable equals 2nd case constant
True
2nd case body
False
Switch variable equals 3rd case constant
True
3rd case body
False
Default body
Exit
26
Program (Switch.cpp)
#include <iostream.h> #include <conio.h> void main() { int a, b; char ch; cout<<Enter first number: ; cin>>a; cout<<Enter second number: ; cin>>b; cout<<Enter the operator: ; ch=getche(); cout<<endl; switch(ch) { case +: cout<<a + b = <<a+b; break; case -: cout<<a - b = <<a-b; break; case *: cout<<a * b = <<a*b; break; case /: cout<<a / b = <<a/b; break; default: cout<<You have entered the wrong operator; } getch(); }
27
EXERCISE
1) Write a program that determines the year entered by user is leap year or not? 2) Write a program which gives the following output Enter age>> And prints the following: If Age is greater than 45 then prints the message You are old stay at home and wait for call If Age is greater than 30 and less than 45 then prints Hey! man enjoy your life with your kids If Age is greater than 20 and less than 30 then prints Cool! have a search for your best suit If Age is greater than 10 and less than 20 then prints Work hard! Your days to study If Age is less than 10 then prints the message Oh! Kid your days to cry Use if-else statement.
3) Write the above program #02 by using the switch statement?
4) Develop a C++ program which will compute the power dissipation of a resistor,
when the user inputs current value and the resistance. Program warns the user if power dissipation is above 1 watt. The power dissipation is measured by P=I2R.
28
EXERCISE SOLUTIONS
===================================================
Program #01
#include <iostream.h> #include <conio.h> void main() { int year; cout<<Enter the year: ; cin>>year; if(year%4==0) cout<<year<< is the leap year; else cout<<year<< is not the leap year; getch(); }
===================================================
Program #02
#include <iostream.h> #include <conio.h> void main() { int Age; cout<<Enter the age: ; cin>>Age; if(Age>45) cout<<You are old stay at home and wait for call;
29
else if(Age>30&&Age<45) cout<<Hey! man enjoy your life with your kids; else if(Age>20&&Age<30) cout<<Cool! Have a search for your best suit; else if(Age>10&&Age<20) cout<<Work hard! Your days to study; else if(Age<10) cout<<Oh! Kid your days to cry; getch(); }
===================================================
Program #03
#include <iostream.h> #include <conio.h> void main() { int Age; cout<<"Enter the age: "; cin>>Age; switch(Age>45) { case 1: cout<<"You are old stay at home and wait for call"; break; } switch(Age>30&&Age<45) { case 1: cout<<"Hey! man enjoy your life with your kids"; break; } switch(Age>20&&Age<30)
30
{ case 1: cout<<"Cool! Have a search for your best suit"; break; } switch(Age>10&&Age<20) { case 1: cout<<"Work hard! Your days to study"; break; } switch(Age<10) { case 1: cout<<"Oh! Kid your days to cry"; } getch(); }
===================================================
Program #04
#include <iostream.h> #include <conio.h> void main() { float P, I, R; cout<<Enter the resistance: ; cin>>R; cout<<Enter the current through the resistor: ; cin>>I; P=(I*I)*R; cout<<the power dissipation is <<P<< watts<<endl; if(P>1) cout<<Alert! There is too much power dissipation; getch(); }
31
===================================================