Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 12

Faculty of Engineering Computer Engineering Department Islamic University of Gaza

2011

C++ Programming Language Lab # 4

C++ Programming Language Lab # 4


Selections
:Objective
.To be familiar with C++ Programming Language

Introduction: Comparison Operators:


Operator Name < <= > >= == != less than less than or equal to greater than greater than or equal to equal to not equal to Example 1 < 2 1 <= 2 1 > 2 1 >= 2 1 == 2 1 != 2 Result true true false false false true

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; }

Exercise 1: Compare two numbers:

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)

Adding a semicolon at the end of an if clause is a common mistake


Logic Error Empty Body

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)

The if...else Statement:


if (booleanExpression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; }
true Boolean Expression false

Statement(s) for the true case

Statement(s) for the false case

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

int i = 1; int j = 2; int k = 3;

if (i > j) if (i > k) cout << "A"; 5 else cout << "B";


(a)

This is better with correct indentation

if (i > j) if (i > k) cout << "A"; else cout << "B";


(b)

Multiple Alternative if Statements: Exercise 3:Grading code :

Logical Boolean operators:


Operator ! && || Name not and or

Truth Tables (!)


p true false !p false true
6

Example !(1 > 2) is true, because (1 > 2) is false. !(1 > 0) is false, because (1 > 0) is true.

Truth Table (&&)

p1 false false true true

p2 false true false true

p1 && p2 false false false 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.

Truth Table ( ||)

p1 false false true true

p2 false true false true

p1 || p2 false true true true

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.

Exercise 4: A year is a leap year?


(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)

Note:
if (number % 2 == 0) even = true; else even = false;
(a)
Equivalent

Equivalent This is better

bool even = number % 2 == 0;


(b)

if (even == true) cout <<"It is even.";


(a)

if (even) cout << "It is even.";


(b)
if (radius >= 0) { area = radius * radius * PI; cout << "The area " << " is " << area; }
(b) Correct

This is better

if (radius >= 0) area = radius * radius * PI; cout << "The area " << " is " << area;

(a) Wrong

Conditional Operator:
(booleanExp) ? exp1 : exp2 Example:
8

cout << ((num % 2 == 0) ? "num is even" : "num is odd"); y = (x > 0) ? 1 : -1;

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; case 1: ; break; case 2: ; break; case 3: ; break; default: ; System.exit(0); }


Next Statement status is 3 status is 1

Compute tax for single filers

break

Compute tax for married file jointly status is 2 Compute tax for married file separatly

break

break

Compute tax for head of household default Default actions

break

Switch Flow Chart:


switch (switch-expression ) {

// 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

You might also like