0% found this document useful (0 votes)
2 views18 pages

Lecture 2

The document covers various C programming operators including unary, division, modulus, and relational operators, as well as concepts like assignment statements, type casting, and logical operators. It also provides syntax for if and else statements and includes programming exercises to reinforce understanding of these concepts. The exercises involve checking conditions and implementing if-else logic based on user input.

Uploaded by

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

Lecture 2

The document covers various C programming operators including unary, division, modulus, and relational operators, as well as concepts like assignment statements, type casting, and logical operators. It also provides syntax for if and else statements and includes programming exercises to reinforce understanding of these concepts. The exercises involve checking conditions and implementing if-else logic based on user input.

Uploaded by

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

C OPERATORS

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.

• parentheses override the regular order of operators


The Assignment Statements

• 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.

• Relational logic always produces a True or False result


• A True relational result evaluates to 1.
• A False relational result evaluates to 0.
THE IF STATEMENT
• SYNTAX
if (condition) {
// code to be executed if the condition is true
}
E.G
The else Statement
• SYNTAX
• if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}.
LOGICAL OPERATORS
TRUTH TABLES
Logical Operators and Their Use
PROGRAMMING EXERCISES
• 1. Write a program that takes two integers as input and checks if both
of them are greater than 10. If both are greater than 10, print "Both
numbers are greater than 10.“

• 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.“

• 4. Write a program that prompts the user to enter an integer. Uses an


if ststement to dertermine if the number is odd or even.
PROGRAMMING EXERCISES C’TD
Create a program that takes a numeric grade as input and
uses if-else statements to print the corresponding letter grade (A, B, C, D, or F)
based on the following grading scale:
A: 80-100
B+: 75-79
B: 70-74
C+:65 -69
C: 60-64
D+:55-69
D :50-54
F : 0-49

You might also like