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

Lab 3

The lab manual for Fall 2021 at UMT Lahore focuses on teaching students about conditional and logical operators in C/C++, including operator precedence and variable declaration. It provides examples of various operators, such as assignment, mathematical, relational, and logical operators, along with sample programs to demonstrate their usage. Additionally, the manual includes tasks for students to practice their understanding of these concepts.

Uploaded by

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

Lab 3

The lab manual for Fall 2021 at UMT Lahore focuses on teaching students about conditional and logical operators in C/C++, including operator precedence and variable declaration. It provides examples of various operators, such as assignment, mathematical, relational, and logical operators, along with sample programs to demonstrate their usage. Additionally, the manual includes tasks for students to practice their understanding of these concepts.

Uploaded by

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

Lab Manual (Lab 03)

Session: Fall 2021

School of Systems and Technology


UMT Lahore Pakistan

LAB INSTRUCTOR: AYESHA MAJID ALI


Contact: ayesha.majid@umt.edu.pk Room #: 504 Last cabin 4th floor SST
Objective
The objective of this lab is to learn about conditional and logical operators, and we will get
familiar with operator precedence. At the end of this lab students will be able to solve any kind
of expression.

Operators & Operands

● Operator is a symbol that instructs C/C++ to perform some operation, or action, on one
or more operands.
● Operand is something that an operator acts on.
● For example:
x = 2 + 3;
● + and = are operators. 2 and 3 are operands and x are variable.
● Variable are case sensitive a variable name “add” is not same as “ADD”

Operators fall into several categories as follows:

● The assignment operator (=).


● Mathematical operators (+, -, /, *, %).
● Relational operators (>, <, >=, <=, = =,! =)
● Logical operators (AND, OR, NOT, &&, ||).

VARIABLE DECLARATION:
Declaring like this is

1-

#include <iostream>
using namespace std;
int main(){
int a;
int b;
int c;
int d;
}

Same as

2-

#include <iostream>
using namespace std;
main(){
int a,b,c,d;}
We can also assign a same value to different variables within one go.

#include <iostream>
using namespace std;
main(){
int a,b,c,d;
a=b=c=d=100;
cout<<a<<endl<<b<<endl<<c<<endl<<d<<endl;
}

Types of operators
Assignment Operator:

● x = y;
● Assign the value of y to variable x and this is called assignment statement.
● Left side must be a variable name.
● Right side can be any expression, variable, constant.
● The evaluation from right to left.

Program 1:

#include<iostream>
using namespace std;

int main()
{
int x,y,a,b;
a=b=2;
x=10; //right side is a constant value
y=9;

x=y; //right side is a variable


cout<<x<<endl;
x=a+b; //right side is an expression
cout<<x<<endl;
return 0;
}

Mathematical Operator:

Mathematical operators perform mathematical operation such as +, -, *, % and /. C/C++ also has
unary mathematical operators (++ and --) and binary mathematical operators.
Relational Operator:
In C++ Programming, the values stored in two variables can be compared using following
operators and relation between them can be determined.

Various C++ Relational/Comparison Operator available are:

Operator, Meaning

Operator Name Example

== Equal to x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to x <= y

OUTPUT OF RELATIONAL OPERATOR

It is either 0 or 1.

● 0 means FALSE
● 1 means TRUE
Program 5: Demonstrating how relational operators work.

#include <iostream>
using namespace std;

int main() {
int x = 5;

int y = 3;

cout <<"(x == y)="<<(x == y)<<endl; // returns 0 (false) because 5 is not


equal to 3

cout <<"(x != y)="<<(x != y)<<endl; // returns 1 (true) because 5 is not


equal to 3

cout <<"(x > y)="<<(x > y)<<endl; // returns 1 (true) because 5 is greater
than 3

cout <<"(x < y)="<<(x < y)<<endl; // returns 0 (false) because 5 is not less
than 3

cout <<"(x >= y)="<<(x >= y)<<endl; // returns 1 (true) because five is
greater than, or equal, to 3

cout <<"(x <= y)="<<(x <= y)<<endl; // returns 0 (false) because 5 is


neither less than or equal to 3

return 0;
}

Logical Operators:

Logical operators are used to determine the logic between variables or values:

Operator Name Description Example

&& Logical and Returns true if both statements are true x < 5 && x < 10

|| Logical or Returns true if one of the statements is true x < 5 || x < 4


! Logical not Reverse the result, returns false if the result is !(x < 5 && x < 10)
true

OUTPUT OF LOGICAL OPERATOR

It is either 0 or 1.

● 0 means FALSE
● 1 means TRUE

Program 5: Demonstrating how relational operators work.

#include <iostream>
using namespace std;

int main() {
int x = 5;
int y = 3;
cout <<"(x > 3 && x < 10)="<<(x > 3 && x < 10)<<endl; // returns true (1)
because 5 is greater than 3 AND 5 is less than 10

cout <<"(x > 3 || x < 4)="<<(x > 3 || x < 4)<<endl; // returns true (1)
because one of the conditions are true (5 is greater than 3, but 5 is not less
than 4)

cout <<"(!(x > 3 && x < 10))"<<(!(x > 3 && x < 10))<<endl; // returns false
(0) because ! (not) is used to reverse the result

return 0;
}

Operator Precedence:
C++ applies the operators in arithmetic expressions in a precise sequence determined by the
following rules of operator precedence, which are generally the same as those in algebra:

1. Operators in expressions contained within pairs of parentheses are evaluated first. Thus,
parentheses may be used to force the order of evaluation to occur in any sequence you
desire. Parentheses are said to be at the "highest level of precedence." In cases of nested, or
embedded, parentheses, such as ((a + b) + c ) the operators in the innermost pair of
parentheses are applied first.
2. Multiplication, division and remainder operations are applied first. If an expression contains
several multiplication, division and remainder operations, evaluation proceeds from left to
right. Multiplication, division and remainder are said to be on the same level of precedence.
3. Addition and subtraction operations are evaluated next. If an expression contains several
addition and subtraction operations, evaluation proceeds from left to right. Addition and
subtraction also have the same level of precedence, which is lower than the precedence of the
multiplication, division and remainder operations.

Order of Associativity
() Parentheses
Evaluated first. If the parentheses are nested,
the expression in the innermost pair is
evaluated first. If there are several pairs of
parentheses "on the same level" (i.e., not
nested), they're evaluated left to right.

Increment/
++/-- Evaluated second.
decrement

* Multiplication
Evaluated third. If there are several, they're
/ Division
evaluated left to right.
% Remainder

+ Addition Evaluated last. If there are several, they're


- Subtraction evaluated left to right.

If the operators are in the same level, then, the operators are performed from left to right order, referring
to table. For example:
String Data Type:
String Types

The string type is used to store a sequence of characters (text). This is not a built-in type, but it behaves
like one in its most basic usage. String values must be surrounded by double quotes:

Example
string greeting = "Hello";
cout << greeting;

Example
// Include the string library
#include <string>

// Create a string variable


string greeting = "Hello";

// Output string value


cout << greeting;
TASKS
Task 1:

Write a program which takes an input from user and prints whether the number is even or odd,
0as an output means the number is even 1 as an output means number is odd.

Sample Output

Program to find Larger number

*****************************

Enter first number: 2

The output is 1

If the remainder is 0 that means number is even. If the remainder is otherwise that means the
number entered by user is odd.

Task 2:

Write a program that reads two values in variable x and y, print values of x and y on the screen. Then
exchange the value of x and y and print the new values after the exchange. (Hint: use a third identifier)

Task 3:

Write a program to check whether a triangle is valid or not, when the three angles of the triangle
are entered through the keyboard.

Hint: A triangle is valid if the sum of all the three angles is = 180

Sample Output

Program to find triangle

**********************
Enter the first angle: 90

Enter the second angle: 50

Enter the third angle: 40

This is Valid Triangle

You might also like