Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.

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

LECTURE 3: BASIC C OPERATORS. Objectives  In this chapter, you will learn about:  Arithmetic operators Unary operators Binary operators  Assignment.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Flow of Control (1) : Logic Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 4: Basic C Operators
Operaciones y Variables
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Decision Structures and Boolean Logic
2440: 211 Interactive Web Programming Expressions & Operators.
LESSON 6 – Arithmetic Operators
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.
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 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
1 Expressions. 2 Variables and constants linked with operators  Arithmetic expressions Uses arithmetic operators Can evaluate to any value  Logical.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 4: Basic C Operators.
OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples.
Operators.
Logical Operator It is useful to test multiple conditions OperatorANDORNOT Symbol&&||! ConditionBOTHEITHERREVERSE.
1 CS161 Introduction to Computer Science Topic #6.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.
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.
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.
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
Expressions and Assignment Statements
Chapter 7: Expressions and Assignment Statements
Operators And Expressions
Operators and Expressions
University of Central Florida COP 3330 Object Oriented Programming
Computing Fundamentals
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.
Variable Declaration, Data types, Expressions
Intro to C Tutorial 4: Arithmetic and Logical expressions
Topics The if Statement The if-else Statement Comparing Strings
JavaScript: Control Statements I
Introduction to C Programming
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
OPERATORS (2) CSC 111.
Lecture 3 Expressions Richard Gesick.
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Introduction to C Programming
Chapter-3 Operators.
Expressions and Assignment Statements
Introduction to Programming – 4 Operators
Expressions.
Chapter 2: Java Fundamentals
Chapter 4: Expression and Operator
OPERATORS AND EXPRESSIONS IN C++
Chap 7. Advanced Control Statements in Java
Using C++ Arithmetic Operators and Control Structures
Operator King Saud University
HNDIT11034 More Operators.
CSS161: Fundamentals of Computing
ICS 101 Lab 3 Hossain Arif ICS Dept
Presentation transcript:

Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators Unary operators Binary operators Equalities and relational operators Logical operators Conditional operator

Principles of Programming - NI July 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 OperatorExampleEquivalent 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

Principles of Programming - NI July 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)

Principles of Programming - NI July 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.

Principles of Programming - NI July Unary Operator C Operation Operator Example Positive + a = +3 Negative - b = -a Increment ++ i++ Decrement -- i-- 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

Principles of Programming - NI July Binary Operators C Operation Operator Example: Addition+ a + 3 Subtraction- a - 6 Multiplication* a * b Division/ a / c Modulus % a % x 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.

Increment and Decrement Operators Both increment and decrement operators can be used as a prefix (pre) or as a suffix (post). The operator can be written before the identifier as a prefix (++a) or after the identifier as a suffix (a++). In simple operations such as a++ or ++a both have exactly the same meaning. However in some cases there is a difference. Consider the following set of statements: int a, x; //line 1 a = 3; //line 2 x = ++a; //line 3 (prefix) After executing above segment of code, “a” will be 4 and “x” will be 4. In line 3, first the variable “a” is incremented before assigning it to variable “x”. Principles of Programming - NI July2005 7

Increment and Decrement Operators (con’t) Consider the following segment of code. int a, x; //line 1 a = 3;//line 2 x = a++;//line 3 (suffix) After executing above code segment “a” will be 4 and “x” will be 3. In the second approach in line 3 first “a” is assigned to “x” and then “a” is incremented. Principles of Programming - NI July2005 8

9 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

Exercise Predict the values of variables “a”, “b”, “sum1” and “sum2” if the following code segment is executed. int a, b; a = b = 2; sum1 = a + (++b); sum2 = a + (b++); Principles of Programming - NI July

Precedence and Associatively of Arithmetic Operators Precedence defines the priority of an operator while associativity indicates which variable(s) an operator is associated with or applied to. In the same expression, if two operators of the same precedence are found, they are evaluated from left to right, except for increment and decrement operators which are evaluated from right to left. Principles of Programming - NI July

Principles of Programming - NI July Precedence Rules 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.

Equal and Relational Operators The relational operators are used to compare values forming relational expressions. The logical operators are used to connect relational expressions together using the rules of formal logic. Both types of expressions produce TRUE or FALSE results. Principles of Programming - NI July

Principles of Programming - NI July 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.

Principles of Programming - NI July 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.

Precedence of Equal, Relational and Logical Operators Below are summarises the relative recedence of the relational and logical operators. These operators are lower in precedence than arithmetic operators. Exercise: What is a value return after you evaluate this following expression? a==b && x==y || m==n Principles of Programming - NI July

Principles of Programming - NI July 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

Principles of Programming - NI July 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’;

Principles of Programming - NI July 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”);

Principles of Programming - NI July 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