SlideShare a Scribd company logo
1
OUTCOMES: 
Explain constants and variables 
Describe data types 
Identify operators in programming 
Explain different between formula and 
expression 
2
VARIABLES 
a meaningful name of data storage location in 
computer 
memory 
Variable’s characteristics: 
• has a name 
• has a type 
• holds a value that you assign to the variable 
3
CONSTANTS 
are values that do not change during program 
execution 
Types of constants: 
• Integer constant 
• Floating point constant 
• String constant 
• Character constant 
4
RULES OF DECLARING VARIABLE & CONSTANT 
• Variable names can be short as a single letter or 
as long as 31 character 
• Names must begin with a letter of alphabet 
• Variables only contain letters, numbers and 
underscore (_) character 
• Variables cannot have same name as C 
command or function 
• Variables can be formed by freely combining 
the letters, digits and underscore 
5
EXAMPLE: 
Valid Invalid Comment 
c “c” Illegal character 
Totalx2 2totalx Illegal first 
character 
Monthly_rate Monthly-rate Illegal character (-) 
AGE AGE@ Illegal character @ 
SALARYPAY SALARY 
PAY 
Illegal blank 
6
DECLARE CONSTANT & VARIABLES 
Variables hold different types of data: 
• Numeric (integer, floating point, double floating point) 
• Character 
• String 
Variables can be defined in 2 places: 
a) After the opening brace of a block of code 
b) Before a function name 
7
DECLARATION FORM: 
Type 
variable_list 
Data types Variable; 
Example: 
int no1,no2; int mark; 
char name[20]; float amount, rate; 
8
#include <stdio.h> 
int main() 
{ 
int a, b; 
int result; 
a=5; 
b=2; 
a=a+1; 
result=a-b; 
printf("nntt%d",result); 
return 0; 
} 
4 
Output: 
9
Variables may be initialized by assigning 
constants to 
them either at the time of their declaration or at a 
later 
tiSmyentax to declare a 
constant: 
data type variable value 
int days_of_week = 
7; 
10
Example: 
int m, n =10; 
char response = ‘n’; 
char color [6] = ‘green’; 
float rate, total = 0.0; 
11
#include <stdio.h> 
main() 
{ 
char name[50], matrix[10]; 
int date, month, year, age; 
int yearnow=2013; 
printf("What is your name?n"); 
scanf( "%s", &name ); 
printf("What is your matrix number?n"); 
scanf( "%s", &matrix ); 
printf("What is your date of birth?n"); 
scanf( "%d", &date); 
printf("What is your month of birth?n"); 
scanf( "%d", &month); 
printf("What is your year of birth?n"); 
scanf( "%d",&year); 
age = yearnow-year; 
printf("nn Your name is %s",name); 
printf (" with matrix number %s ", matrix); 
printf (“n Your age is %d”, age); 
return 0; 
} 
Output: 
What is your name? 
Ali 
What is your matrix number? 
1035 
What is your date of birth? 
10 
What is your month of birth? 
3 
What is your year of birth? 
1990 
Your name is Ali with matrix number 1035 
Your age is 23 
12
IDENTIFY THE SCOPE OF CONSTANT & VARIABLES 
Integer constant 
are the whole numbers that do not contain decimal 
point 
Example; 
int number = 10; 
0, -708 
13
Floating point constant 
a decimal number that contains the decimal point (.) 
Example; 
float number = 
0.7564; 
-12.0, 65.4 
14
String constant 
• always enclosed in double quotation marks (“ and “) 
• a single space, a word, or a group of words between 
double quotation marks is a C string constant 
Example; 
char name[6]= 
“AHMAD”; 
“2.0”, “X” 
15
Character constant 
• should be enclosed within single quotation marks (‘ 
and ‘) 
• all the alphabetic, numeric and special character can 
be character constant 
Example; 
char numeric= ‘C’; 
‘X’, ‘0’ 
16
KEYWORDS IN C PROGRAMME 
• every word in C language is a keyword or an 
identifier 
• keyword cannot be used as a variable name 
Definition Name Type 
char Character 
unsigned char Unsigned character 
signed char Signed character (same as char) 
int Integer 
unsigned int Unsigned integer 
signed int Signed integer 
short int Short integer 
unsigned short int Unsigned short integer 
signed short int Signed short integer (same as short 
int) 
long Long integer 
long int Long integer (same as long) 
signed long int Signed long integer (same as long 
int) 
unsigned long int Unsigned long integer 
float Floating-point 
double Double floating-point 
17
DATA TYPES 
Basic data types in C: 
• Integer 
• Character 
• Floating point 
• Double floating point 
18
INTEGER 
hold whole numbers 
Keyword : int 
Syntax: int variable_name; 
Example: 
int number; 
int a, b, c; 
19
CHARACTER 
hold only a single character 
Keyword : char 
Syntax: char variable_name; 
Example: 
char name[30]; 
char huruf; 
20
FLOATING POINT 
contain decimal points 
Keyword : float 
Syntax: float variable_name; 
Example: 
float value; 
Float gross_pay; 
21
DOUBLE FLOATING POINT 
will ensure a maximum accuracy in decimal point 
Keyword : double 
Syntax: double variable_name; 
Example: 
double tax; 
double value; 
22
UNDERSTAND OPERATORS & EXPRESSION 
• C math operators are symbols for multiplying, 
dividing, 
• adding and subtracting and as well as for other 
• operations 
• are not always mathematical, but most of it is 
• mathematical 
• expression – combining operators, variables 
and 
constants 
23
Arithmetic Operator 
Most C programs perform arithmetic 
calculations. 
OPERATO 
R 
ACTION ALGEBRAIC 
EXPRESSION 
C 
EXPRESSI 
ON 
+ Addition f + 7 f + 7 
- Subtraction p – c p – c 
* Multiplication bm b * m 
/ Division x/y or or x÷y x / y 
% Remainder or 
Modulus 
r mod s r % s 
24
Typical operator results. With a=7 and b=2, 
the expression on the values on the right 
EXPRESSION VALUE 
a – b 5 
a + b 9 
a * b 14 
a / b 3 
a % b 1 
25
Parentheses are used to group terms in C 
expressions in much the same manner as 
in algebraic expressions. 
For example, to multiply a times the 
quantity B+C, we write: 
A*(B+C) 
26
Example: 
ALGEBRA C 
m= (a+b+c+d+e)/5; 
y=(m*x) + b; 
a b c d e 
5 
m 
    
 
y  mx  b 
27
Relational Operators 
Operators for data comparisons are 
available 
It’s called relational operators, and the task 
is to compare data. 
28
Standard 
algebraic equality 
operator or 
relational operator 
C equality 
or relational 
operator 
Example of 
C condition Meaning of C 
condition 
Equality operators 
= == X == Y X is equal to Y 
≠ != X != Y X is not equal to Y 
Relational operators 
> > X > Y X is greater than Y 
< < X < Y X is less than Y 
≥ >= X >= Y X is greater than or 
equal to Y 
≤ <= X <= Y X is less than or 
equal to Y 
29
Example: 
if a = 7 and b = 5, 
then a < b yields 0 and a != b yields 1. 
30
Logical operators 
There may be times when you need to 
test more than one set of variables. 
You can combine more than one 
relational test in a compound relational 
test by using C’s logical operators 
31
OPERATO 
R 
MEANING 
&& AND 
|| OR 
! NOT 
32
The first two logical operators, && and ||, 
never appear by themselves. 
They typically go between two or more 
relational tests. 
33
a b 
a | | b 
either a or b 
must be true 
a&&b 
Both a and b 
Must be true 
! a 
Produces the 
opposite 
relation 
0 0 0 0 1 
0 1 1 0 1 
1 0 1 0 0 
1 1 1 1 0 
34
Example: 
The true and false on each side of the 
operators 
represent a relational if test. 
The following statements, for example, are 
valid if 
test that use logical operators (sometimes 
called 
compound relational operators). 
35
if ((a<b) && (c>d)) 
{ 
printf(“Results are invalid”); 
} 
If a is less than b, and c is greater than d, 
print results are invalid to the screen. 
36
if ((sales > 5000) || (hrsWorked > 81)) 
{ 
bonus = 500; 
} 
The sales must be more than 5000, or the 
hrsWorked must 
be more than 81, before the assignment executes. 
37

More Related Content

What's hot

What's hot (18)

Variables, Data Types, Operator & Expression in c in detail
Variables, Data Types, Operator & Expression in c in detailVariables, Data Types, Operator & Expression in c in detail
Variables, Data Types, Operator & Expression in c in detail
 
Constant, variables, data types
Constant, variables, data typesConstant, variables, data types
Constant, variables, data types
 
Unit ii ppt
Unit ii pptUnit ii ppt
Unit ii ppt
 
C language (more)
C language (more)C language (more)
C language (more)
 
2. operator
2. operator2. operator
2. operator
 
Token and operators
Token and operatorsToken and operators
Token and operators
 
Basic Input and Output
Basic Input and OutputBasic Input and Output
Basic Input and Output
 
Module 4- Arrays and Strings
Module 4- Arrays and StringsModule 4- Arrays and Strings
Module 4- Arrays and Strings
 
Constants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya JyothiConstants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya Jyothi
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c language
 
C language basics
C language basicsC language basics
C language basics
 
Module 5-Structure and Union
Module 5-Structure and UnionModule 5-Structure and Union
Module 5-Structure and Union
 
Data types
Data typesData types
Data types
 
Project in TLE
Project in TLEProject in TLE
Project in TLE
 
Basics of c++
Basics of c++Basics of c++
Basics of c++
 
C –imp points
C –imp pointsC –imp points
C –imp points
 
Report Group 4 Constants and Variables
Report Group 4 Constants and VariablesReport Group 4 Constants and Variables
Report Group 4 Constants and Variables
 
C – operators and expressions
C – operators and expressionsC – operators and expressions
C – operators and expressions
 

Viewers also liked (10)

CHAPTER 1
CHAPTER 1CHAPTER 1
CHAPTER 1
 
CHAPTER 6
CHAPTER 6CHAPTER 6
CHAPTER 6
 
CHAPTER 5
CHAPTER 5CHAPTER 5
CHAPTER 5
 
Cohesion and coupling
Cohesion and couplingCohesion and coupling
Cohesion and coupling
 
Software Design 1: Coupling & cohesion
Software Design 1: Coupling & cohesionSoftware Design 1: Coupling & cohesion
Software Design 1: Coupling & cohesion
 
Chapter 5 software design
Chapter 5 software designChapter 5 software design
Chapter 5 software design
 
Image enhancement techniques
Image enhancement techniquesImage enhancement techniques
Image enhancement techniques
 
Introduction to Computer graphics
Introduction to Computer graphics Introduction to Computer graphics
Introduction to Computer graphics
 
Design concepts and principles
Design concepts and principlesDesign concepts and principles
Design concepts and principles
 
Slideshare ppt
Slideshare pptSlideshare ppt
Slideshare ppt
 

Similar to CHAPTER 2

presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptx
KrishanPalSingh39
 
presentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptxpresentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptx
KrishanPalSingh39
 
introductory concepts
introductory conceptsintroductory concepts
introductory concepts
Walepak Ubi
 

Similar to CHAPTER 2 (20)

CSharp Language Overview Part 1
CSharp Language Overview Part 1CSharp Language Overview Part 1
CSharp Language Overview Part 1
 
C Building Blocks
C Building Blocks C Building Blocks
C Building Blocks
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptx
 
Unit i intro-operators
Unit   i intro-operatorsUnit   i intro-operators
Unit i intro-operators
 
M.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C LanguageM.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C Language
 
C introduction by thooyavan
C introduction by  thooyavanC introduction by  thooyavan
C introduction by thooyavan
 
INTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptxINTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptx
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)
 
C intro
C introC intro
C intro
 
presentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptxpresentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptx
 
introductory concepts
introductory conceptsintroductory concepts
introductory concepts
 
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit DubeyMCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
 
Unit 4 Foc
Unit 4 FocUnit 4 Foc
Unit 4 Foc
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
C programming
C programming C programming
C programming
 
lecture2.ppt
lecture2.pptlecture2.ppt
lecture2.ppt
 
Chapter-2 is for tokens in C programming
Chapter-2 is for tokens in C programmingChapter-2 is for tokens in C programming
Chapter-2 is for tokens in C programming
 
C introduction
C introductionC introduction
C introduction
 
Constants and variables in c programming
Constants and variables in c programmingConstants and variables in c programming
Constants and variables in c programming
 

Recently uploaded

Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 

Recently uploaded (20)

Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
Advances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfAdvances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdf
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx
 

CHAPTER 2

  • 1. 1
  • 2. OUTCOMES: Explain constants and variables Describe data types Identify operators in programming Explain different between formula and expression 2
  • 3. VARIABLES a meaningful name of data storage location in computer memory Variable’s characteristics: • has a name • has a type • holds a value that you assign to the variable 3
  • 4. CONSTANTS are values that do not change during program execution Types of constants: • Integer constant • Floating point constant • String constant • Character constant 4
  • 5. RULES OF DECLARING VARIABLE & CONSTANT • Variable names can be short as a single letter or as long as 31 character • Names must begin with a letter of alphabet • Variables only contain letters, numbers and underscore (_) character • Variables cannot have same name as C command or function • Variables can be formed by freely combining the letters, digits and underscore 5
  • 6. EXAMPLE: Valid Invalid Comment c “c” Illegal character Totalx2 2totalx Illegal first character Monthly_rate Monthly-rate Illegal character (-) AGE AGE@ Illegal character @ SALARYPAY SALARY PAY Illegal blank 6
  • 7. DECLARE CONSTANT & VARIABLES Variables hold different types of data: • Numeric (integer, floating point, double floating point) • Character • String Variables can be defined in 2 places: a) After the opening brace of a block of code b) Before a function name 7
  • 8. DECLARATION FORM: Type variable_list Data types Variable; Example: int no1,no2; int mark; char name[20]; float amount, rate; 8
  • 9. #include <stdio.h> int main() { int a, b; int result; a=5; b=2; a=a+1; result=a-b; printf("nntt%d",result); return 0; } 4 Output: 9
  • 10. Variables may be initialized by assigning constants to them either at the time of their declaration or at a later tiSmyentax to declare a constant: data type variable value int days_of_week = 7; 10
  • 11. Example: int m, n =10; char response = ‘n’; char color [6] = ‘green’; float rate, total = 0.0; 11
  • 12. #include <stdio.h> main() { char name[50], matrix[10]; int date, month, year, age; int yearnow=2013; printf("What is your name?n"); scanf( "%s", &name ); printf("What is your matrix number?n"); scanf( "%s", &matrix ); printf("What is your date of birth?n"); scanf( "%d", &date); printf("What is your month of birth?n"); scanf( "%d", &month); printf("What is your year of birth?n"); scanf( "%d",&year); age = yearnow-year; printf("nn Your name is %s",name); printf (" with matrix number %s ", matrix); printf (“n Your age is %d”, age); return 0; } Output: What is your name? Ali What is your matrix number? 1035 What is your date of birth? 10 What is your month of birth? 3 What is your year of birth? 1990 Your name is Ali with matrix number 1035 Your age is 23 12
  • 13. IDENTIFY THE SCOPE OF CONSTANT & VARIABLES Integer constant are the whole numbers that do not contain decimal point Example; int number = 10; 0, -708 13
  • 14. Floating point constant a decimal number that contains the decimal point (.) Example; float number = 0.7564; -12.0, 65.4 14
  • 15. String constant • always enclosed in double quotation marks (“ and “) • a single space, a word, or a group of words between double quotation marks is a C string constant Example; char name[6]= “AHMAD”; “2.0”, “X” 15
  • 16. Character constant • should be enclosed within single quotation marks (‘ and ‘) • all the alphabetic, numeric and special character can be character constant Example; char numeric= ‘C’; ‘X’, ‘0’ 16
  • 17. KEYWORDS IN C PROGRAMME • every word in C language is a keyword or an identifier • keyword cannot be used as a variable name Definition Name Type char Character unsigned char Unsigned character signed char Signed character (same as char) int Integer unsigned int Unsigned integer signed int Signed integer short int Short integer unsigned short int Unsigned short integer signed short int Signed short integer (same as short int) long Long integer long int Long integer (same as long) signed long int Signed long integer (same as long int) unsigned long int Unsigned long integer float Floating-point double Double floating-point 17
  • 18. DATA TYPES Basic data types in C: • Integer • Character • Floating point • Double floating point 18
  • 19. INTEGER hold whole numbers Keyword : int Syntax: int variable_name; Example: int number; int a, b, c; 19
  • 20. CHARACTER hold only a single character Keyword : char Syntax: char variable_name; Example: char name[30]; char huruf; 20
  • 21. FLOATING POINT contain decimal points Keyword : float Syntax: float variable_name; Example: float value; Float gross_pay; 21
  • 22. DOUBLE FLOATING POINT will ensure a maximum accuracy in decimal point Keyword : double Syntax: double variable_name; Example: double tax; double value; 22
  • 23. UNDERSTAND OPERATORS & EXPRESSION • C math operators are symbols for multiplying, dividing, • adding and subtracting and as well as for other • operations • are not always mathematical, but most of it is • mathematical • expression – combining operators, variables and constants 23
  • 24. Arithmetic Operator Most C programs perform arithmetic calculations. OPERATO R ACTION ALGEBRAIC EXPRESSION C EXPRESSI ON + Addition f + 7 f + 7 - Subtraction p – c p – c * Multiplication bm b * m / Division x/y or or x÷y x / y % Remainder or Modulus r mod s r % s 24
  • 25. Typical operator results. With a=7 and b=2, the expression on the values on the right EXPRESSION VALUE a – b 5 a + b 9 a * b 14 a / b 3 a % b 1 25
  • 26. Parentheses are used to group terms in C expressions in much the same manner as in algebraic expressions. For example, to multiply a times the quantity B+C, we write: A*(B+C) 26
  • 27. Example: ALGEBRA C m= (a+b+c+d+e)/5; y=(m*x) + b; a b c d e 5 m      y  mx  b 27
  • 28. Relational Operators Operators for data comparisons are available It’s called relational operators, and the task is to compare data. 28
  • 29. Standard algebraic equality operator or relational operator C equality or relational operator Example of C condition Meaning of C condition Equality operators = == X == Y X is equal to Y ≠ != X != Y X is not equal to Y Relational operators > > X > Y X is greater than Y < < X < Y X is less than Y ≥ >= X >= Y X is greater than or equal to Y ≤ <= X <= Y X is less than or equal to Y 29
  • 30. Example: if a = 7 and b = 5, then a < b yields 0 and a != b yields 1. 30
  • 31. Logical operators There may be times when you need to test more than one set of variables. You can combine more than one relational test in a compound relational test by using C’s logical operators 31
  • 32. OPERATO R MEANING && AND || OR ! NOT 32
  • 33. The first two logical operators, && and ||, never appear by themselves. They typically go between two or more relational tests. 33
  • 34. a b a | | b either a or b must be true a&&b Both a and b Must be true ! a Produces the opposite relation 0 0 0 0 1 0 1 1 0 1 1 0 1 0 0 1 1 1 1 0 34
  • 35. Example: The true and false on each side of the operators represent a relational if test. The following statements, for example, are valid if test that use logical operators (sometimes called compound relational operators). 35
  • 36. if ((a<b) && (c>d)) { printf(“Results are invalid”); } If a is less than b, and c is greater than d, print results are invalid to the screen. 36
  • 37. if ((sales > 5000) || (hrsWorked > 81)) { bonus = 500; } The sales must be more than 5000, or the hrsWorked must be more than 81, before the assignment executes. 37