0% found this document useful (0 votes)
23 views29 pages

Chapter 3

This chapter discusses operators in C which are used to process data and connect operands in expressions and equations. The main types of operators covered are mathematical, relational, logical, and compound assignment. Mathematical operators perform arithmetic, relational operators are used for comparisons, and logical operators connect relational expressions. Compound assignment operators combine a binary operator and assignment. The chapter also discusses operator precedence, unary operators, and using printf to output results.

Uploaded by

Jennifer Sanders
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)
23 views29 pages

Chapter 3

This chapter discusses operators in C which are used to process data and connect operands in expressions and equations. The main types of operators covered are mathematical, relational, logical, and compound assignment. Mathematical operators perform arithmetic, relational operators are used for comparisons, and logical operators connect relational expressions. Compound assignment operators combine a binary operator and assignment. The chapter also discusses operator precedence, unary operators, and using printf to output results.

Uploaded by

Jennifer Sanders
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/ 29

CHAPTER 3

ARITHMETIC IN C

OPERATORS
Operators tell the computer how to
process the data.
It connects data in the expression and
equation
Types of operators used in calculation
and problem solving include

Mathematical
Relational
Logical

OPERATORS CONTINUE.

Two concepts related to operator are

Operand
Resultant

Operands are the data being process


Resultant is the answer that result when the
operation is complete.

Example

5 + 7 = 12
Operands are 5 and 7
Operator is +
Resultant is 12

Operands can be constant or variable


Data types of resultant and operands will depend on
the operator

TYPES OF OPERATOR

Mathematical operators
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Modulo division (%)
Functions

TYPES OF OPERATOR

Relational operators

Equal to (==)
Less than (< )
Greater than (>)
Less than or Equal to (<=)
Greater than or equal to (>=)

The resultant of relational operation is logical


data type i.e. TRUE or FALSE
The next set of actions will depend on the
result of the relational expression
E.g. Balance > 500, if the expression TRUE

withdraw 100, if the expression FALSE


withdraw 50

TYPES OF OPERATORS

Logical Operator
OR (||)
AND (&&)
Not (!)

Use to connect relational expression (decision-making


expression)
To perform operation on logical data

EXAMPLE OF OPERATOR

OPERATOR HIERARCHY (PRECEDENCE)

EXPRESSIONS
It is a sequence of operands and operators that reduce to a
single value.
Operator : It is a symbolic token that represents an action to
be
taken.
Ex: * is an multiplication operator.
Operand: An operand receives operators action.
Ex: A+ B In this A and B are operands and + is an operator.
Note: Expressions always evaluate to a single value.
There is no limit to the number of operator and operand sets in
an expression

Ex: 5.0/9.0*(fah 32.0)

SETTING UP NUMERICAL
EXPRESSION
Normal equation
X(3Y+4) - 4Y
X+6
Appropriate Computer Equation
X* (3*Y+4)-4*Y/(X+6)

EVALUATING MATHEMATICAL EXPRESSION


Consider 5*(X+Y)-4*Y/(Z+6)
Value of X = 2, Y=3, Z=6
Evaliation 5 * (X + Y) 4 * Y / (Z+6)

EVALUATION RELATIONAL EXPRESSION

GETTING STARTED WITH C

PREPROCESSOR DIRECTIVES
An instruction to pre-processor
Standard library header (p154,Deitel)
E.g. #include <stdio.h>

for std input/output

#include <stdlib.h>
Conversion

number-text vise-versa, memory


allocation, random numbers

#include <string.h>

string processing

TYPES OF OPERATORS

Types of operators are:

Arithmetic operators
(+ , - , * , / , %)
Relational operators
(> , < , == , >= , <=, !=)
Logical operators (&& , ||)
Compound assignment operator
(+=, -=, *=, /=, %=)

Binary operators: needs two operands


Unary operators: single operand
Bitwise operators: executes on bit level

ARITHMETIC OPERATORS
Used to execute mathematical equations
The result is usually assigned to a data storage
(instance/variable) using assignment operator
(=)

C OPERATORS

EXERCISE ON ARITHMETIC
OPERATORS

Given x = 20, y = 3

z=x%y
= 20 % 3
= 2 (remainder)

RELATIONAL AND LOGICAL


OPERATORS
Previously, relational operator:
>, < >=, <=, == , !=
Previously, logical operator:
&&, ||
Used to control the flow of a program
Usually used as conditions in loops and branches

MORE ON RELATIONAL OPERATORS


Relational operators use mathematical
comparison (operation) on two data,
but gives logical output

e.g1 let say b = 8, if (b > 10)


e.g2 while (b != 10)
e.g3 if(kod == 1) print(Pegawai);

Reminder:

Dont confuse == (relational op.)


with = (assignment op.)

MORE ON LOGICAL OPERATORS

Logical operators are manipulation of logic

e.g1 let say b=8, c=10,


if ((b > 10) && (c<10))
e.g2 while ((b==8) ||(c > 10))
e.g3 if ((kod == 1) && (salary > 2213))

TRUTH TABLE FOR &&


(LOGICAL AND) OPERATOR

TRUTH TABLE FOR ||


(LOGICAL OR) OPERATOR

COMPOUND ASSIGNMENT OPERATOR


To calculate value from expression and store
it in variable, we use assignment operator (=)
Compound assignment operator combine
binary operator with assignment operator
E.g. val +=one; is equivalent to val = val + one;
E.g. count = count -1; is equivalent to

count -=1;
count--;
--count;

UNARY OPERATORS
Obviously operating on ONE operand
Commonly used unary operators

Increment/decrement { ++ , -- }
Arithmetic Negation { - }
Logical Negation { ! }

Usually using prefix notation


Increment/decrement can be both a prefix and
postfix

UNARY OPERATORS (EG.)

OPERATOR PRECEDENCE & SYMBOL IN C


LANGUAGE

FORMATTED OUTPUT WITH PRINTF


To display result of the program can be
done by using keyword printf and
operator as shown below:
printf(formating,variable) ;

//this is variable declaration


int a, b; //Line 1
char grade; //Line 2
//examples of input and output statements a = 25; grade = A; //Line3
printf(a = ,a) ;
//Line 4
printf(Enter two integers: ) ;
//Line 5
scanf (%d%d,&a,&b) ;
printf(The numbers you entered are %d %d,a,b); //Line 6
printf(Your grade is %c ,grade); //Line 10

END OF CHAPTER

You might also like