5 - Operators
5 - Operators
ENGINEERING
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)
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Bitwise operators
2 ARITHMETIC OPERATORS
Arithmetic operators are used to perform common mathematical operations.
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;
EXAMPLE
int x = 10;
x += 5;
+= 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.
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
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:
! 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
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:
Before trying to print the boolean variables, you should know that boolean
values are returned as integers:
EXAMPLE
// Create boolean variables
bool isProgrammingFun = true;
bool isFishTasty = false;
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).
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;
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;
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
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.
2. Logical OR (`||`)
These operators are often used in conditional statements (`if`, `while`, `for`,
etc.) to control the flow of the program based on certain conditions.
// 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;
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.