Lab 3
Lab 3
Example:
LOCATION POP.
Portcity 2425785
Hightown 47
Lowville 9761
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
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;
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:
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.
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
|| (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
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).
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.
if (expression)
{ statement_1;
}
else
if(expression)
{
Statement_2;
}
else
if(expression)
{
statement_3;
}
else
{
Statement_4
}
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".
if (x <= 10)
cout << "small";
else
cout << "big";
x = 30;
y = 20;
if (y != (x - 10))
x = x - 5;
else
x = y;
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.
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.
if(x)
cout<<" Yes"<<endl;
else
cout<<"NO"<<endl;
getch();