0% found this document useful (0 votes)
4 views

Lab 3

Uploaded by

degag64086
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab 3

Uploaded by

degag64086
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Introduction To Programming

Relational Operators and Decision Statements

Some important Data Types are

Data Type Space Occupied Comments


Bool 1 bit or a Byte Value is true or false.
Used in comparison
statements
Unsigned Char 1 Byte Range 0 --- 255
Unsigned Short 2 Bytes Range 0 --- 65,535
Unsigned int 4 Byte Range 4,294,967,295
Unsigned long 4 Bytes Range 4,294,967,295

The Setw Manipulator:


Manipulators are operators used with the insertion operator (<<) to modify or
manipulate the way data is displayed. endl is one such manipulator. Setw changes
the field width of the output.

Example:

long pop1=2425785, pop2=47, pop3=9761;

cout << “LOCATION “ << “POP.” << endl


<< “Portcity “ << pop1 << endl
<< “Hightown “ << pop2 << endl
<< “Lowville “ << pop3 << endl;

Here’s the output from this program:

LOCATION POP.
Portcity 2425785
Hightown 47
Lowville 9761

Muhammad Hammad Page 1


Introduction To Programming

The setw manipulator causes the number (or string) that follows it in the stream to
be printed with in a field n characters wide while n is the argument to setw(n). The
value is right justified within the field

long pop1=2425785, pop2=47, pop3=9761;

cout << setw(8) << “LOCATION” << setw(12)<< “POPULATION” << endl

<< setw(8) << “Portcity” << setw(12) << pop1 << endl

<< setw(8) << “Hightown” << setw(12) << pop2 << endl

<< setw(8) << “Lowville” << setw(12) << pop3 << endl;

Muhammad Hammad Page 2


Introduction To Programming

Fig: Steps involved in program axecution

Relational Operators:
A Relational Operator compares two values. The values can be any C++ data types
such as char, int and float. The comparison involves such relationships as equal to,
less than and greater than. The result of comparison is true or false.

Example:

cout << “numb<10 is “ << (numb < 10) << endl;


cout << “numb>10 is “ << (numb > 10) << endl;
cout << “numb==10 is “ << (numb == 10) << endl;

Muhammad Hammad Page 3


Introduction To Programming

Some relational operators are


> Greater than

< Less than


== Equal to
!= Not equal to
>= Greater than or equal to
<= Less than or equal to

Logical Operators:
C++ allows 3 types of logical operators

&&

||

! (Logical NOT)

The operator! In C++ is used to perform the Boolean operation NOT. It has only
one operand and is located at its right.

! (5==5), !true evaluates to false

! (6<=4), !false evaluates to true

&& (logical AND)

The logical AND i.e. && combines the two expressions and evaluates to true when
the both expression are true.

A B A&&B
True True True
True False False
False True False
False false False

Muhammad Hammad Page 4


Introduction To Programming

|| (logical OR)

The logical OR i.e. && combines the two expressions and evaluates to true when
the any of the expression is true.

A B A&&B
True True True
True False True
False True True
False false False

Decision Statements:
Normally, the program flows along line by line in the order in which it appears in
the source code. But, it is sometimes required to execute a particular portion of
code only if certain condition is true; or false i.e. you have to make decision in
your program. There are three major decision making structures. The ‘if’
statement, the if-else statement, and the switch statement. Another less commonly
used structure is the conditional operator. Program begins execution at the main()
function. - Statements within the main () function are then executed from top to
down style. - The first statement, then the second and so forth, until the end of the
main () function is reached. - However, this order is rarely encountered in real
C/C++ program. - The order of the execution of the statements within the main()
body may be redirected, not in sequence anymore. - This concept of changing the
order in which statements are executed is called program control and is
accomplished by using program control statements. This is how we can control the
program flows.
1. THE IF STATEMENT

The if statement enables to test for a condition (such as whether two


variables are equal) and branch to different parts of the code, depending on
the result or the conditions with relational and logical operators are also
included.

Muhammad Hammad Page 5


Introduction To Programming

The simplest form of an

if (expression)
{
statement;
}
next_statement;

1. (Expression) is evaluated.
2. If TRUE (non-zero) the statement is executed.
3. If FALSE (zero) the next statement following the if statement
block is executed.
4. So, during the execution based on some condition, some
codes not executed (skipped).

2. THE IF-ELSE STATEMENT

Often the program will want to take one branch if the condition is true,
another if it is false. If only one statement is to be followed by the if or else
condition then there is no need of parenthesis. The keyword else can be used
to perform this functionality:

if (expression)
{
statement_1;
}
else
{
Statement_2;
}
The (expression) is evaluated.
2. If it evaluates to non-zero (TRUE), statement_1 is executed,
otherwise, if it evaluates to zero (FALSE), statement_2 is executed.
3. They are mutually exclusive, meaning, either statement_1 is
executed or statement_2, but not both.
4. The statements_ 1 and statements_ 2 can take the form of block and
must be put in curly braces.

Muhammad Hammad Page 6


Introduction To Programming

3. Multiple If’s or Else if structure:

if (expression)
{ statement_1;
}
else
if(expression)
{
Statement_2;
}
else
if(expression)
{
statement_3;
}
else
{
Statement_4
}

The exit () library Function:


The function causes the program to terminate no matter where it is in the listing.

Lab Tasks

1. If hours are greater than 18, print a message saying "This is an overload";
otherwise print a message saying "Not an overload".

2. If classification is equal to 5 then set the character variable Type equal to 'G',
and print a message saying "graduate student"; otherwise set Type equal to 'U'
and print a message saying "undergraduate".

Muhammad Hammad Page 7


Introduction To Programming

3. After execution of the following code, what will be printed if x is 10?

if (x <= 10)
cout << "small";
else
cout << "big";

4. After execution of the following code, what will be the value of x?

x = 30;
y = 20;
if (y != (x - 10))
x = x - 5;
else
x = y;

5. Write a program that will display a menu

Enter C for Cash Withdrawal


Enter D for Depositing cash in account
Enter B for balance inquiry
Enter I to see the interested amount

Program should initially request the user to enter an amount in his/her account
and then it will show the account status. Then a menu will be displayed asking
user for transaction. After any choice that user enters, your program should
display the current status (amount in your account). Interest rate is 10 % of the
total.

Muhammad Hammad Page 8


Introduction To Programming

6. Write a program that takes two values of temperature in Celsius and Fahrenheit
and do the following:
a. If temp in C is greater than 40, display “It is hot today”.
b. If temp in C is between 25 and 35 , display “Its Normal weather”
c. If temp in C is less than 25 ,display “ It is cold today”
d. If temp in F is greater than 60 , display “it is hot today”
e. If temp in F is less than 60 than display “It is moderate” if and only if
temp in C is greater than 30.
f. If temp in F is in between 30 and 50, display “ It is Normal weather”
g. If temp in F is less than 28, display “it is cold today” regardless of the
value of C.
h. If temp in C is between 20 and 22 and temp in F is in between 25 and 27
, display “It is Ideal weather”

Use the Multiple If structure to do this. Your program should have minimum
number of if statement, don’t hard code for every condition.

7. What will be the output


int x;
cout<<"Enter value of x"<<endl;
cin>>x;

if(x)
cout<<" Yes"<<endl;
else
cout<<"NO"<<endl;

getch();

Muhammad Hammad Page 9

You might also like