0% found this document useful (0 votes)
3 views13 pages

5 - Operators

The document is a lab report on Operators in Computer Programming, detailing various types of operators including arithmetic, assignment, comparison, and logical operators. It provides examples and exercises to illustrate their usage in programming, particularly in the C language. Additionally, it covers Boolean values and their application in decision-making processes within programming.

Uploaded by

eishaahmed17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views13 pages

5 - Operators

The document is a lab report on Operators in Computer Programming, detailing various types of operators including arithmetic, assignment, comparison, and logical operators. It provides examples and exercises to illustrate their usage in programming, particularly in the C language. Additionally, it covers Boolean values and their application in decision-making processes within programming.

Uploaded by

eishaahmed17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

DEPARTMENT OF AVIONICS

ENGINEERING

SUBJECT : Computer Programming Lab

LAB NO : 05

TITLE : Operators
SUBMITTED TO : Ms. Maha Intakhab Alam
SUBMITTED BY :
BATCH : Avionics 10
SECTION :
Marks Obtained :

Remarks:

DEADLINE:

DATE OF SUBMISSION:
TABLE OF CONTENTS
1 Operators............................................................................................................. 3
2 Arithmetic Operators............................................................................................ 3
3 Assignment Operators.......................................................................................... 4
4 Comparison Operators......................................................................................... 5
5 Logical Operators................................................................................................. 6
6 Booleans.............................................................................................................. 7
Boolean Variables.................................................................................................... 7
7 Comparing Values and Variables.........................................................................8
8 Real Life Example................................................................................................. 9
9 Logical Operators
10 Practice
Exercises…………………………………………………………………………………………………
…12
1 OPERATORS
Operators are used to perform operations on variables and values.

In the example below, we use the + operator to add together two values:

EXAMPLE
int myNum = 100 + 50;

Although the + operator is often used to add together two values, like in the
example above, it can also be used to add together a variable and a value, or a
variable and another variable:

EXAMPLE
int sum1 = 100 + 50; // 150 (100 + 50)
int sum2 = sum1 + 250; // 400 (150 + 250)
int sum3 = sum2 + sum2; // 800 (400 + 400)

C divides the operators into the following groups:

 Arithmetic operators
 Assignment operators
 Comparison operators
 Logical operators
 Bitwise operators

2 ARITHMETIC OPERATORS
Arithmetic operators are used to perform common mathematical operations.

Operator Name Description Example Try it

+ Addition Adds together two x+y Try it »


values

- Subtraction Subtracts one x-y Try it »


value from
another

* Multiplication Multiplies two x*y Try it »


values

/ Division Divides one value x/y Try it »


by another

% Modulus Returns the x%y Try it »


division
remainder

++ Increment Increases the ++x Try it »


value of a variable
by 1

-- Decrement Decreases the --x Try it »


value of a variable
by 1

3 ASSIGNMENT OPERATORS
Assignment operators are used to assign values to variables.

In the example below, we use the assignment operator (=) to assign the
value 10 to a variable called x:

EXAMPLE
int x = 10;

The addition assignment operator (+=) adds a value to a variable:

EXAMPLE
int x = 10;
x += 5;

A list of all assignment operators:

Operator Example Same As Try it

= x=5 x=5 Try it »

+= x += 3 x=x+3 Try it »

-= x -= 3 x=x-3 Try it »

*= x *= 3 x=x*3 Try it »

/= x /= 3 x=x/3 Try it »

%= x %= 3 x=x%3 Try it »
&= x &= 3 x=x&3 Try it »

|= x |= 3 x=x|3 Try it »

^= x ^= 3 x=x^3 Try it »

>>= x >>= 3 x = x >> 3 Try it »

<<= x <<= 3 x = x << 3 Try it »

4 COMPARISON OPERATORS
Comparison operators are used to compare two values (or variables). This is
important in programming, because it helps us to find answers and make
decisions.

The return value of a comparison is either 1 or 0, which means true (1)


or false (0). These values are known as Boolean values, and you will learn
more about them in the Booleans and If..Else chapter.

In the following example, we use the greater than operator (>) to find out if 5
is greater than 3:

EXAMPLE
int x = 5;
int y = 3;
printf("%d", x > y); // returns 1 (true) because 5 is greater than
3

A list of all comparison operators:

z Name Example Description Try it

== Equal to x == y Returns 1 if the Try it »


values are equal

!= Not equal x != y Returns 1 if the Try it »


values are not
equal

> Greater than x>y Returns 1 if the Try it »


first value is
greater than the
second value
< Less than x<y Returns 1 if the Try it »
first value is less
than the second
value

>= Greater than or x >= y Returns 1 if the Try it »


equal to first value is
greater than, or
equal to, the
second value

<= Less than or x <= y Returns 1 if the Try it »


equal to first value is less
than, or equal to,
the second value

5 LOGICAL OPERATORS
You can also test for true or false values with logical operators.

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

Operator Name Example Description Try it

&& Logical and x < 5 && x < 10 Returns 1 if both Try it »


statements are true

|| Logical or x < 5 || x < 4 Returns 1 if one of the Try it »


statements is true

! Logical not !(x < 5 && x < 10) Reverse the result, Try it »
returns 0 if the result is 1
EXERCISE:
Fill in the blanks to multiply 10 with 5, and print the result:

int x = 10;
int y = 5;
printf("%d", x >= y);

CREATE A PROGRAM THAT TAKES INPUT 3 NUMBERS FROM THE USER AND
OUTPUT THE AVERAGE.

6 BOOLEANS
Very often, in programming, you will need a data type that can only have one of
two values, like:

 YES / NO
 ON / OFF
 TRUE / FALSE

For this, C has a bool data type, which is known as booleans.

Booleans represent values that are either true or false.

Boolean Variables

In C, the bool type is not a built-in data type, like int or char.

It was introduced in C99, and you must import the following header file to use
it:

#include <stdbool.h>

A boolean variable is declared with the bool keyword and can only take the
values true or false:

bool isProgrammingFun = true;


bool isFishTasty = false;

Before trying to print the boolean variables, you should know that boolean
values are returned as integers:

 1 (or any other number that is not 0) represents true


 0 represents false
Therefore, you must use the %d format specifier to print a boolean value:

EXAMPLE
// Create boolean variables
bool isProgrammingFun = true;
bool isFishTasty = false;

// Return boolean values


printf("%d", isProgrammingFun); // Returns 1 (true)
printf("%d", isFishTasty); // Returns 0 (false)

However, it is more common to return a boolean value by comparing values


and variables.

7 COMPARING VALUES AND VARIABLES


Comparing values are useful in programming, because it helps us to find
answers and make decisions.

For example, you can use a comparison operator, such as the greater than (>)
operator, to compare two values:

EXAMPLE
printf("%d", 10 > 9); // Returns 1 (true) because 10 is greater
than 9
From the example above, you can see that the return value is a boolean value
(1).

You can also compare two variables:

EXAMPLE
int x = 10;
int y = 9;
printf("%d", x > y);

In the example below, we use the equal to (==) operator to compare different
values:

EXAMPLE
printf("%d", 10 == 10); // Returns 1 (true), because 10 is equal to
10
printf("%d", 10 == 15); // Returns 0 (false), because 10 is not
equal to 15
printf("%d", 5 == 55); // Returns 0 (false) because 5 is not equal
to 55

You are not limited to only compare numbers. You can also compare boolean
variables, or even special structures, like arrays (which you will learn more
about in a later chapter):

EXAMPLE
bool isHamburgerTasty = true;
bool isPizzaTasty = true;

// Find out if both hamburger and pizza is tasty


printf("%d", isHamburgerTasty == isPizzaTasty);
Remember to include the <stdbool.h> header file when working
with bool variables.
EXERCISE:

What is the result of the following example?

printf("%d", 15 > 5);

8 REAL LIFE EXAMPLE


Let's think of a "real life example" where we need to find out if a person is old
enough to vote.

In the example below, we use the >= comparison operator to find out if the age
(25) is greater than OR equal to the voting age limit, which is set to 18:

EXAMPLE
int myAge = 25;
int votingAge = 18;

printf("%d", myAge >= votingAge); // Returns 1 (true), meaning 25


year olds are allowed to vote!

Cool, right? An even better approach (since we are on a roll now), would be to
wrap the code above in an if...else statement, so we can perform different
actions depending on the result:

EXAMPLE

Output "Old enough to vote!" if myAge is greater than or equal to 18.


Otherwise output "Not old enough to vote.":
int myAge = 25;
int votingAge = 18;

if (myAge >= votingAge) {


printf("Old enough to vote!");
} else {
printf("Not old enough to vote.");
}
Booleans are the basis for all comparisons and conditions.

You will learn more about conditions (if...else) in the next chapter.

9 LOGICAL OPERATORS
In C, the `&&` operator is used for logical AND, the `||` operator is used for
logical OR, and the `!` operator is used for logical NOT.

Here's how they work:

1. Logical AND (`&&`)

- It returns `true` if both operands are `true`, otherwise `false`.


- Example: `if (x > 0 && y < 10)` evaluates to `true` if both `x` is greater
than `0` and `y` is less than `10`.

2. Logical OR (`||`)

- It returns `true` if at least one of the operands is `true`, otherwise `false`.


- Example: `if (x == 0 || y == 0)` evaluates to `true` if either `x` or `y` (or
both) is equal to `0`.

3. Logical NOT (`!`)

- It returns `true` if the operand is `false`, and `false` if the operand is


`true`.
- Example: `if (!isReady)` evaluates to `true` if `isReady` is `false`.

These operators are often used in conditional statements (`if`, `while`, `for`,
etc.) to control the flow of the program based on certain conditions.

Refer to the following truth tables for ease:


EXAMPLE LOGICAL AND OPERATOR
#include <stdio.h>
int main()
{ int a = 10, b = 20;
if (a > 0 && b > 0) {
printf("Both values are greater than 0\n");
}
else {
printf("Both values are less than 0\n");
}
return 0;
}

EXAMPLE LOGICAL OR OPERATOR


#include <stdio.h>

// Driver code
int main()
{
int a = -1, b = 20;

if (a > 0 || b > 0) {
printf("Any one of the given value is "
"greater than 0\n");
}
else {
printf("Both values are less than 0\n");
}
return 0;
}
EXAMPLE FOR LOGICAL /NOT OPERATOR
#include <stdio.h>

// Driver code
int main()
{
int a = 10, b = 20;

if (!(a > 0 && b > 0)) {


// condition returned true but
// logical NOT operator changed
// it to false
printf("Both values are greater than 0\n");
}
else {
printf("Both values are less than 0\n");
}
return 0;
}

10 PRACTICE EXERCISES

1. A new legislation has passed that restricts the voting age of the people
to be over 18 and under 60. Anyone outside of that age bracket is not
allowed to vote. How would you code such a program? take input from
user. It should Print 1 if user is eligible to vote and 0 if not.

2. An engineer designed a faulty switch that is accidentally turning on


when the user presses off and turns off when the user presses on.
Write a program to be added to his design that flips the user input such
that when user presses on it turns On and Off when he presses Off.
Take user input. Use NOT gate.

3. A student can only pass a class if he or she passes both Computer


programming course and Computer programming Lab. Write a program
using AND gate that asks the user to enter his computer programming
course status (Pass/Fail) and then Computer programming lab
status(pass/fail). If the user passes both then he/she is allowed to pass.
In this case print 1. If user fail in even one, the user will not be allowed
to pass, thus the program should print 0.
4. A change in policy was implemented at IST that says student will now
pass the course if he or she is pass in either Lab or Course. A student
will fail if he is fail in both lab and course. Write a program that
implements this policy using OR gate. Take user input as in the
previous question and print 1 for pass and 0 for fail.

You might also like