By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.

Slides:



Advertisements
Similar presentations
If Statements & Relational Operators Programming.
Advertisements

Chapter 6 Horstmann Programs that make decisions: the IF command.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
CS1061 C Programming Lecture 6: Operators and Expressions A. O’Riordan, 2004.
Flow control 1: if-statements (Liang 72-80) if(radius < 0) { System.out.println(“cannot get area: radius below zero”); } else { double area = radius *
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.
1 Lecture 3  Lexical elements  Some operators:  /, %, =, +=, ++, --  precedence and associativity  #define  Readings: Chapter 2 Section 1 to 10.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 4: Basic C Operators
Expressions, Data Conversion, and Input
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
C programming: Variables, Expressions part II. Data Types of Arithmetic Expressions Relational Expressions Logical Expressions Multiple Assignments Compound.
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
ORDER OF OPERATIONS x 2 Evaluate the following arithmetic expression: x 2 Each student interpreted the problem differently, resulting in.
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.
Lesson - 7. Operators There are three types of operators: Arithmetic Operators Relational and Equality Operators Logical Operators.
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
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.
15-Nov-15 All the Operators. operators.ppt 2 Precedence An operator with higher precedence is done earlier (precedes) one with lower precedence A higher.
CHAPTER 1.3 THE OPERATORS Dr. Shady Yehia Elmashad.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 4: Basic C Operators.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Operators.
1 CS161 Introduction to Computer Science Topic #6.
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.
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.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
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
Operators And Expressions
Operators and Expressions
Relational Operations
Arithmetic Operator Operation Example + addition x + y
2008/10/27: Lecture 13 CMSC 104, Section 0101 John Y. Park
Lecture 3 Expressions Richard Gesick.
All the Operators 22-Nov-18.
Numerical Data Types.
Relational Operators Operator Meaning < Less than > Greater than
All the Operators 4-Dec-18.
Chapter-3 Operators.
2008/10/27: Lecture 13 CMSC 104, Section 0101 John Y. Park
Algorithms computer as the tool process – algorithm
Assignment Operators Topics Increment and Decrement Operators
Chapter 2: Java Fundamentals
Data Types and Expressions
All the Operators 6-Apr-19.
All the Operators 13-Apr-19.
OPERATORS in C Programming
Assignment Operators Topics Increment and Decrement Operators
Operator King Saud University
Assignment Operators Topics Increment and Decrement Operators
Data Types and Expressions
Assignment Operators Topics Increment and Decrement Operators
OPERATORS in C Programming
Data Types and Expressions
Chapter 12 Variables and Operators
Presentation transcript:

By: Mr. Baha Hanene Chapter 6

LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs (L02) 2

CONTENTS Basic Arithmetic Operators Operator Precedence Typecasting Increment & Decrement Operators Relational Operators Logical Operators 3

BASIC ARITHEMATIC OPERATORS OperatorsDescription +Used for Addition Operation -Used for Subtraction Operation *Used for Multiplication Operation /Used for Division Operation %Used for Remainder Operations 4

OPERATOR PRESEDENCE OperatorsPrecedence ( )Top priority in any expression is given to these small parentheses. * / %Second priority in any expression is given to * / % operators whatever the first should be solved first. + -Last priority in any expression is given to + - operators whatever the first should be solved first. 5 Note: All mathematical expressions are solved from left to right. So the operators having the same priority should be treated from left to right in order. Whatever the first should be solved first.

OPERATOR PRECEDENCE PRACTICE 6

7 x = (2.1 + num) / 2 * 5Ans. : 0.51 x = (2.1 + num) / (2 * 5)Ans. : x = num / 2Ans. : 1 Let us consider some examples of real expressions & mixed expressions with floating point types. (Assume num=3) Consider x is integer x = num / 2 Ans. : 1 x = num / 2.0 Ans. : 1

COMPARE THE FOLLOWIG Exp. 8 Suppose x=2 & y=4 y = x * x + 2 * x - 5 * x / y ; Their answers will be same or different ? (When writing complex arithmetic expressions, the spaces have no meaning, so the above two expr. are equivalent)

TYPECASTING 9 If both values are integer, the integer arithmetic is applied and the answer will be integer only, e.g. a = 10 / 3 Ans.: 3 (Whereas ‘a’ is integer) If both values are float, the floating point arithmetic is applied and the answer will be floating point only, e.g. a = / 3.00 Ans.: (Whereas ‘a’ is float) If one value is integer & other is floating point, then the integer is converted to a floating point representation & floating point arithmetic is used, e.g. a = 10 / 3.0 ( ) OR a = 10.0 / 3 ( ) a = 10 / 3 ( ) (Whereas ‘a’ is float)

TYPECASTING However, it is possible to force or cast a variable to a different type. We do typecasting in C language by placing the type name in the parenthesis and putting it in front of the value we want to change, e.g. 10 If b is a float variable b = (float) 10 / 3 Ans b = 10 / 3 Ans We will get , because 10 is converted to floating point value before division. NOTE: The type of left hand side variable has no effect on the calculation of the right hand side expression. This is because expression is always calculated before the assignment operation can take place.

INCREMENT & DECREMENT OPERATORS OperatorsDescription ++aadd one to (a) before using it a++add one to (a) after using it --asubtract one from (a) before using it a--subtract one from (a) after using it 11 ++a “pre-increment” a++ “post-increment” --a “pre-decrement” a-- “post-decrement”

12

RELATIONAL OPERATORS Relational operators allow comparison between two values giving a result whose value depends whether the comparison is true or false. The result is 1 if the comparison is true and 0 if it is false. The relational operators in C are: ==equal !=not equal >greater than >=greater than or equal <less than <=less than or equal You must not confuse the == operator which compares for equality with the = which is the assignment operator. 13

RELATIONAL OPERATORS The following table explains how the relational operators are evaluated: (3 == 3)trueResult will be 1 (3 != 3)falseResult will be 0 (3 > 3)falseResult will be 0 (3 >= 3)trueResult will be 1 (3 < 3)falseResult will be 0 (3 <=3)trueResult will be 1 Therefore the following program segment: int num1 = 3, num2 = 5, result; result = (num1 == num2); printf(“%d”, result); gives a value of zero. 14

LOGICAL OPERATROS Logical operators connect two or more logical expressions to achieve more powerful expressions. There are three logical operators: !not &&and ||or Each logical operator has a truth table The truth table for not is: !truefalse !falsetrue 15

LOGICAL OPERATROS The truth table for and is: true &&truetrue true && falsefalse false && truefalse false && falsefalse The truth table for or is: true || truetrue true || falsetrue false || truetrue false || falsefalse 16

LOGICAL OPERATROS Check the following statements assuming that int x=5, y=8, z=3; result = (x + z == y) && (x > z);/* result = true */ result = (x != y) && (y != z);/* result = true */ result = (x + z == y) && (x >= y);/* result = false*/ result = (x + z == y) || (x >= y);/* result = true */ result = !(x + z == y);/* result = false*/ 17 Note: All logical operations will either result in 1 or 0 i.e. True or False