Relational Operations

Slides:



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

Program Control Dilshad M. Shahid New York
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
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.
Chapter 2 part #4 Operator
LESSON 6 – Arithmetic Operators
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
CPS120: Introduction to Computer Science Operations Lecture 9.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
Recap……Last Time [Variables, Data Types and Constants]
Operators & Expressions
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 4: Basic C Operators.
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.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
1 Chapter 3 – Operators and Expressions Outline 3.1Introduction 3.2Arithmetic operators 3.3Relational operators 3.4Logical operators 3.5Assignment operators.
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.
Chapter 3 Math Operations. Objectives Use the assignment and arithmetic operators. Use operators in output statements. Explain the problem with division.
CompSci 230 S Programming Techniques
Arithmetic Expressions
CSE 220 – C Programming Expressions.
Expressions.
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
Expressions.
Operators and Expressions
INSPIRING CREATIVE AND INNOVATIVE MINDS
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
BIL 104E Introduction to Scientific and Engineering Computing
TMF1414 Introduction to Programming
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.
INC 161 , CPE 100 Computer Programming
Assignment and Arithmetic expressions
Variable Declaration, Data types, Expressions
Arithmetic & other operations
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.
Chapter 4 - Program Control
Arithmetic Expressions & Data Conversions
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
Introduction to Programming – 4 Operators
Doing Arithmetic Today’s lecture is in chapter 2 Assignment statement:
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.
Expressions.
Chapter 3 Operators and Expressions
Data Types and Expressions
Herbert G. Mayer, PSU CS Status 7/19/2015
Chapter 4: Expression and Operator
OPERATORS AND EXPRESSIONS IN C++
Chap 7. Advanced Control Statements in Java
OPERATORS in C Programming
Operator King Saud University
Lecture 9: Implementing Complex Logic
Data Types and Expressions
OPERATORS in C Programming
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

Relational Operations Chapter 8 Arithmetic and Relational Operations Chapter 8: Arithmetic and Relational Operations

Assignment Statement Format: variable = expression; variable is an identifier representing variable name expression can be: a variable [eg: min = num;] a constant [eg: age = 20;] a combination of the above [eg: ave = (a1+a2+a3/count);] Chapter 8 Assignment Statement

Assignment Statement Rules: lvalues appear on left of ‘=’ b = 50; b = a + b; lvalue rvalue Rules: lvalues appear on left of ‘=’ rvalues appear on right of ‘=’ lvalues may be used as rvalues, but not vice versa variable names are lvalues constants and expressions are rvalues Chapter 8 Assignment Statement

Assignment Statement Order of evaluation: right to left. Example: a = z = 123; 123 is assigned to z The assignment (z = 123) returns the value 123, which is assigned to a. An assignment statement has a ‘value’. For example, ‘count = 12’; returns the value 12, besides assigning 12 to count. Chapter 8 Assignment Statement

Assignment Statement Assigning a value to a variable of different type? int a, b; float x; char c; a = 23.5; x = 12; c = 109; b = 'n'; Chapter 8 Assignment Statement

Arithmetic Operators Sample program fah2cel.c. Spot the error. Chapter 8 Arithmetic Operators

Unary Operators Unary plus +, unary minus - Examples: +12, -45.80, -7.2e-3 Chapter 8 Unary Operators

Binary Operators Additon: 5 + 2 is 7; 5.4 + 2.7 is 8.1 Subtraction: 5 - 2 is 3; 5.0 - 2.3 is 2.7 Multiplication: 5 * 2 is 10; 3.2 * 1.5 is 4.8 Division: What is 5 / 2? 5.0 / 2.0? 0 / 25? 19 / 0? Modulus (only for integers): What is 5 % 2? 8 % 5? 15 % 5? 17 % -7? 15 % 0? Chapter 8 Binary Operators

Data Types of Expressions Same-type operands: result inherits the type. 7 + 3 is integer; 2.3 + 4.56 is float Mixed-type operands: result is of type that is more general. 7 + 3.0 is float; 5.0 / 2 is float What is 5 / 2? 20 / 3? Chapter 8 Data Types of Expressions

Data Types of Expressions What values are stored in these variables? float x, y; int n; x = 13 / 5; y = 9 * 0.5; n = 9 * 0.5; Chapter 8 Data Types of Expressions

Promotion In mixed-type expressions, the value of a more restricted type is automatically promoted to a more general type. float x; x = 13 / 5.0; 13 is promoted to float before division is carried out. Chapter 8 Promotion

Cast The cast operator (type) is used to explicitly change the type of the operand for the operation. float x; x = (float) 13 / 5; What happens without the (float) cast? Chapter 8 Cast

Precedence Rule Precedence rule for arithmetic operators, from highest to lowest: parentheses Unary operators ++, --, +, -, (type) Binary operators *, /, % Binary operators +, - Example: 3 * (12 - 7) Chapter 8 Precedence Rule

Associativity Rule For operators at the same precedence level, the associativity rule dictates the order of evaluation: Unary operators: right to left Binary operators: left to right Example: 3 * (12 - 7) % 4 - (16 / (2 + 2 * 3 - 1)) Chapter 8 Associativity Rule

Compound Assignment Operators For statement in this form: variable = variable op expression; we may write: variable op= expression; Examples: c += 7; equivalent to c = c + 7; d -= 4; d = d - 4; e *= 5; e = e * 5; f /= 3; f = f / 3; g %= 9; g = g % 9; Chapter 8 Compound Assignment Operators

Compound Assignment Operators Is ‘j *= k + 1’ equivalent to ‘j = j * k + 1’ or ‘j = j * (k + 1)’? What is the result of this? (m + n) *= 2 Chapter 8 Compound Assignment Operators

Increment/Decrement Operators ++a or a++ equivalent to ‘a = a + 1’ or ‘a += 1’ --a or a-- equivalent to ‘a = a -1’ or ‘a -= 1’ Pre-increment (pre-decrement): Increment (decrement) variable, then use its value. Post-increment (post-decrement): Use the variable’s value, then increment (decrement) it. Chapter 8 Increment/Decrement Operators

Increment/Decrement Operators Chapter 8 Increment/Decrement Operators

Increment/Decrement Operators Avoid using the ++ and -- operators in complex expressions in which the variables they are applied appear more than once: x = 5; i = 2; y = i * x + ++i; is y assigned the value 13 or 18? Chapter 8 Increment/Decrement Operators

Mathematical Functions Mathematical functions are available in the math library. Some examples are: pow(): to compute powers fabs(): to return absolute values sqrt(): to compute square roots Need to include math.h file in your program, and compile with -lm option: cc -lm prog.c Study the function prototypes in math.h. Chapter 8 Mathematical Functions

Equality and Relational Operators Selection and repetition constructs require the use of conditions. if (condition) { statements } Conditions are formed by equality operator and relational operators. Chapter 8 Equality and Relational Operators

Equality and Relational Operators equal == x == y not equal != x != y greater than > x > y less than < x < y greater than or equal >= x >= y less than or equal <= x <= y if (x < y) printf("%f is smaller than %f\n", x, y); Chapter 8 Equality and Relational Operators

Equality and Relational Operators Do not mix up == and =. Zero -- false; non-zero values -- true. if (123) printf("Hello!\n"); if (7+3) Chapter 8 Equality and Relational Operators

Equality and Relational Operators False expression returns 0; true expression returns 1. if (3 < 7) printf("Hello!\n"); a = (123 > 321); printf(”%d\n", a); Chapter 8 Equality and Relational Operators

Equality and Relational Operators Do not use == to compare equality of real numbers. Real values may not be stored accurately. if ((a/3)*3 == a) printf("Hello!\n"); To test equality of 2 real numbers, test their difference instead (use fabs()), and take them as equal if the difference is small enough. Chapter 8 Equality and Relational Operators

Logical Operators To combine conditions into more complex ones. Logical AND: && Logical OR: || Logical NOT (negation): ! Evaluation from left to right. Chapter 8 Logical Operators

Logical Operators Functions of logical operators. What is the value of a? int a; a = (3 > 5) || (5 > 1); Chapter 8 Logical Operators

Logical Operators Examples: if (grader == 1 && age >= 65) ++snrFemales; if (semesterAvg >= 90 || finalExam >= 90) grade = 'A'; if !(grade == 'F') /* or (grade != 'F') */ printf("Student passed.\n"); Chapter 8 Logical Operators

Logical Operators Lazy (or short-circuit) evaluation of logical expressions: as soon as truth value can be determined, later expressions are skipped. For logical AND, if front expression is false, the rest are skipped. if (grader == 1 && age >= 65) ++snrFemales; If (grader == 1) is false, there is no need to evaluate (age >= 65) Chapter 8 Logical Operators

Logical Operators For logical OR, if front expression is true, the rest are skipped. if (semesterAvg >= 90 || finalExam >= 90) grade = 'A'; if (semesterAvg >= 90) is true, there is no need to evaluate (finalExam >= 90). Chapter 8 Logical Operators

Homework Try exercises behind chapter 8. Chapter 8 Homework