Lecture 2
Lecture 2
JJJ
TYPICAL OPERATOR RESULTS
The Unary Operators
• A unary operator operates on, or affects, a single value
• E.G
Division and Modulus
• The forward slash (/) always divides. However, it produces an integer
divide if integer values (constants, variables, or a combination of
both) appear on both sides of the slash. If there is a remainder, C
discards it.
• The percent sign (%) produces a modulus, or a remainder, of an
integer division. It requires that integers be on both sides of the
symbol, or it does not work. E.G
The Order of Precedence
E.G
2 + 3 * 2=??
LEFT TO RIGHT.
Using Parentheses
• The use of parentheses as the highest precedence level with lines
indicating precedence determines the operator order.
• Multiple Assignments
a=b=c=d=e=100;
To C, the equals sign always means:Assign
the value on the right to the variable on the
left. This is called the right-to left order
Compound Assignments
• Many times in programming, you might want to
update the value of a variable. That is, you need to
take a variable's current value, add or multiply that
value by an expression, and then assign it back into
the original variable. The following assignment
statement demonstrates this:
salary=salary*1.2;
Compound Assignments (C’TD)
• C's compound operators.
Type Casting
• In C programming, type casting, also known as type conversion, is the
process of explicitly converting a value from one data type to another.
This is done to ensure that an operation or assignment involving
variables of different data types is carried out correctly.
• E.G
int a = 5;
double b = 2.5;
double result = a + b; // 'a' is implicitly cast to a double before the addition.
int x = 10;
float y = (float)x; // Explicitly casting 'x' to a float.
Relational Operators
• used to compare two values and determine the relationship between them.
• 2. Create a program that prompts the user to enter their age. Check if
the age is less than 18 or greater than 65, and print "You are either
too young or too old to participate.“
• 3. Write a program that takes an integer input and checks if it's not
equal to 0. If it's not equal to 0, print "The number is not zero.“