LECTURE 3: BASIC C OPERATORS. Objectives  In this chapter, you will learn about:  Arithmetic operators Unary operators Binary operators  Assignment.

Slides:



Advertisements
Similar presentations
Operators and Arithmetic Operations. Operators An operator is a symbol that instructs the code to perform some operations or actions on one or more operands.
Advertisements

Arithmetic Calculations
1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
1  Ex: Declare a variable to store user’s age: int age; Prompt the user to enter his/her age: printf (“ How old are you? “); Read / scan the entered value.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Operators and Expressions
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 4: Basic C Operators
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
2440: 211 Interactive Web Programming Expressions & Operators.
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
Chapter 3: Data Types and Operators JavaScript - Introductory.
CSE1301 Computer Programming Lecture 5: C Primitives 2 Corrections.
LESSON 6 – Arithmetic Operators
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
Lesson - 7. Operators There are three types of operators: Arithmetic Operators Relational and Equality Operators Logical Operators.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
1 Expressions. 2 Variables and constants linked with operators  Arithmetic expressions Uses arithmetic operators Can evaluate to any value  Logical.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
4. EXPRESSIONS. Display the value of pi, to 5 decimal places, right justified, in 9 columns Read in someone’s height in feet and inches using.
Expressions and Operators in C. Expressions and Operators Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); Two types: – Function calls – The expressions.
Operators & Expressions
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 4: Basic C Operators.
Operators.
Logical Operator It is useful to test multiple conditions OperatorANDORNOT Symbol&&||! ConditionBOTHEITHERREVERSE.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.
Expression and Operator. Expressions and Operators u Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); u Two types: –Function calls –The expressions formed.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
LECTURE # 6 : STRUCTURED PROGRAMMING C++ OPERATORS By Mr. Ali Edan.
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
ISBN Chapter 7 Expressions and Assignments Statements.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Operators & Expressions
CSE 220 – C Programming Expressions.
Chapter 7: Expressions and Assignment Statements
Operators And Expressions
Operators and Expressions
University of Central Florida COP 3330 Object Oriented Programming
CS2403 Programming Languages Expressions and Assignment Statements
University of Central Florida COP 3330 Object Oriented Programming
Relational Operations
Chapter 7: Expressions and Assignment Statements
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
OPERATORS (2) CSC 111.
Lecture 3 Expressions Richard Gesick.
Introduction to C Programming
Introduction to Programming – 4 Operators
Chapter 4: Expression and Operator
OBJECT ORIENTED PROGRAMMING I LECTURE 4 PART 2 GEORGE KOUTSOGIANNAKIS
Chap 7. Advanced Control Statements in Java
Operator and Expression
Presentation transcript:

LECTURE 3: BASIC C OPERATORS

Objectives  In this chapter, you will learn about:  Arithmetic operators Unary operators Binary operators  Assignment operators  Equalities and relational operators  Logical operators  Conditional operator

Arithmetic Operators I  In C, we have the following operators (note that all these example are using 9 as the value of its first operand) OperationOperatorOperandValue After Addition+211 Subtraction-27 Multiplication*218 Division/33 Increment+++10 Decrement---8 Modulus%21

Arithmetic Operators II  There are 2 types of arithmetic operators in C:  unary operators operators that require only one operand.  binary operators. operators that require two operands.

Unary Operator  The first assigns positive 3 to a  The second assigns the negative value of a to b.  i++ is equivalent to i = i + 1  i-- is equivalent to i = i-1 C OperationOperatorExample Positive+a=+3 Negative-b=-a Increment++i++ Decrement--i--

PRE- / POST-Increment  It is also possible to use ++i and --i instead of i++ and i--  However, the two forms have a slightly yet important difference.  Consider this example: int a = 9; printf(“%d\n”, a++); printf(“%d”, a);  The output would be: 9 10

PRE- / POST-Increment cont…  But if we have: int a = 9; printf(“%d\n”, ++a); printf(“%d”, a);  The output would be: 10  a++ would return the current value of a and then increment the value of a  ++a on the other hand increment the value of a before returning the value

The following table illustrates the difference between the prefix and postfix modes of the increment and decrement operator. int R = 10, count=10; ++ Or -- Statement Equivalent Statements R value Count value R = count++;R = count; count = count R = ++count;count = count + 1; R = count; 11 R = count --;R = count; count = count – 1; 109 R = --count;Count = count – 1; R = count; 99

Binary Operators  The division of variables of type int will always produce a variable of type int as the result.  You could only use modulus (%) operation on int variables. C OperationOperatorExample Addition+a+3 Subtraction-a-6 Multiplication*a*b Division/a/c Modulus%a%x

Assignment Operators  Assignment operators are used to combine the '=' operator with one of the binary arithmetic operators  In the following slide, All operations starting from c = 9 OperatorExample Equivalent Statement Results +=c += 7c = c + 7c = 16 -= c - = 8 c = c – 8c = 1 *=c *= 10c = c * 10c = 90 /=c /= 5c = c / 5c = 1 %=c %= 5c = c % 5c = 4

Precedence Rules  Precedence rules come into play when there is a mixed of arithmetic operators in one statement. For example: x = 3 * a - ++b%3;  The rules specify which of the operators will be evaluated first. PrecedenceOperatorAssociativity Level 1 (highest)()Left to right 2UnaryRight to left 3* / %Left to right 4+ -Left to right 6 (lowest)= += -= *= /= %=Right to left

Precedence Rules cont…  For example: x = 3 * a - ++b % 3; how would this statement be evaluated?  If we intend to have the statement evaluated differently from the way specified by the precedence rules, we need to specify it using parentheses ( )  Using parenthesis, we will have x = 3 * ((a - ++b)%3);  The expression inside a parentheses will be evaluated first.  The inner parentheses will be evaluated earlier compared to the outer parentheses.

Equality and Relational Operators  Equality Operators:  Relational Operators: OperatorExampleMeaning ==x==yx is equal to y !=x!=yx is not equal to y OperatorExampleMeaning >x>yx is greater than y <x<yx is less than y >=x>=yx is greater than or equal to y <=x<=yx is less than or equal to y

Logical Operators  Logical operators are useful when we want to test multiple conditions.  There are 3 types of logical operators and they work the same way as the boolean AND, OR and NOT operators.  && - Logical AND  All the conditions must be true for the whole expression to be true.  Example: if (a == 10 && b == 9 && d == 1) means the if statement is only true when a == 10 and b == 9 and d == 1.

Logical Operators cont…  || - Logical OR  The truth of one condition is enough to make the whole expression true.  Example: if (a == 10 || b == 9 || d == 1) means the if statement is true when either one of a, b or d has the right value.  ! - Logical NOT (also called logical negation)  Reverse the meaning of a condition  Example: if (!(points > 90)) means if points not bigger than 90.

Conditional Operator  The conditional operator (?:) is used to simplify an if/else statement.  Syntax: Condition ? Expression1 : Expression2  The statement above is equivalent to: if (Condition) Expression1 else Expression2

Conditional Operator cont…  Example 1: if/else statement: if (total > 60) grade = ‘P’ else grade = ‘F’; conditional statement: total > 60 ? grade = ‘P’: grade = ‘F’; OR grade = total > 60 ? ‘P’: ‘F’;

Conditional Operator cont…  Example 2: if/else statement: if (total > 60) printf(“Passed!!\n”); else printf(“Failed!!\n”); Conditional Statement: printf(“%s!!\n”, total > 60? “Passed”: “Failed”);

SUMMARY  This chapter exposed you the operators used in C  Arithmetic operators  Assignment operators  Equalities and relational operators  Logical operators  Conditional operator  Precedence levels come into play when there is a mixed of arithmetic operators in one statement.  Pre/post fix - effects the result of statement