Professional Documents
Culture Documents
Lab 41
Lab 41
2011
One-way if Statements:
(if (Boolean Expression } ;(statement(s {
Example:
if (x != 0) { y = 10/x ; cout << "The value of y= 10/x = " << y<< endl; }
Notes:
If there is one single statement I there Is no need for the brackets
Outer parentheses required Braces can be omitted if the block contains a single statement Equivalent
if ((i > 0) && (i < 10)) { cout << "i is an " << "integer between 0 and 10"; }
(a)
if ((i > 0) && (i < 10)) cout << "i is an " << "integer between 0 and 10";
(b)
if (radius >= 0); { area = radius * radius * PI; cout << "The area " 3 << " is " << area; }
(a)
Equivalent
if (radius >= 0) { }; { area = radius * radius * PI; cout << "The area " << " is " << area; }
(b)
Exercise 2:
The LeVan Car Rental company charges $0.25/mile if the total mileage does not exceed 100. If the total mileage is over 100, the company charges $0.25/mile for the first 100 miles, then it charges $0.15/mile for any additional mileage over 100. Write a program so that if the clerk enters the number of miles, the program would display the total price owed.
Nested if Statements:
if (i > k) { if (j > k) cout << "i and j are greater than k"; } else cout << "i is less than or equal to k";
Note:
int i = 1; int j = 2; int k = 3;
The else clause matches the most recent if clause in the same block.
Equivalent
Example !(1 > 2) is true, because (1 > 2) is false. !(1 > 0) is false, because (1 > 0) is true.
Example (3 > 2) && (5 >= 5) is true, because (3 > 2) and (5 >= 5) are both true. (3 > 2) && (5 > 5) is false, because (5 > 5) is false.
Example (2 > 3) || (5 > 5) is false, because (2 > 3) and (5 > 5) are both false. (3 > 2) || (5 > 5) is true, because (3 > 2) is true.
The (&&) is referred to as the conditional or short-circuit AND operator, and ( ||) is referred to as the conditional or short-circuit OR operator.
Note:
if (number % 2 == 0) even = true; else even = false;
(a)
Equivalent
This is better
if (radius >= 0) area = radius * radius * PI; cout << "The area " << " is " << area;
(a) Wrong
Conditional Operator:
(booleanExp) ? exp1 : exp2 Example:
8
Exercise 5:
The following program outputs the ratio of two doubles that are input from the keyboard. The calculation yields a value that is always a fraction. The code:
Switch Statements:
switch (status) { case 0: ;
status is 0
break
Compute tax for married file jointly status is 2 Compute tax for married file separatly
break
break
break
// switch-expression must yield a value of char, byte, short, or int type and must always be enclosed in parentheses case value1: statement(s)1; break; // If the break statement is not present, the next case statement will be executed. // The value1, ..., and valueN must have the same data type as the value of the switchexpression. case value2: statement(s)2; break; . default: statement(s)-for-default; }
Exercise 6:
Cola machine code:
:Lab work
Apply the programming exercises practically on DEV C++ Program , and show the results to your instructor.
:Home work
10
write a program that reads in two integers and determines and prints if the first is a multiple of the second ?
writing a program that evaluates the roots of a quadratic equation ax2 + bx + c = 0. Here the
real coefficients a, b and c are inputs to the program. Basically, the real roots can be calculated from the equation:
You will enter three values for a,b,c ,and check the value (b2 4ac) ,if the value is positive (greater than or equal zero ) then you can calculate the roots and print them ,otherwise you will print "No real root exists" . Hint : there is two roots ,because there is two equations once with +ve and the other with ve.
The output :
use if ,else if and else statements to implement the following function, enter the value of x from keyboard, and print the value of f :
writing a program that reads two numbers and an operator, and then applies one of the four basic operations ( + , - , * , / ) to the numbers. five sample runs are given in the screen output column. Hint : use switch Statements:
11
The output :
When you enter invalid operator, the following message will appear :
12