Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Similar documents
Operators & Expressions

Conditional Statement

Programming and Data Structures

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Chapter 4: Basic C Operators

Unit 3. Operators. School of Science and Technology INTRODUCTION

Operators and Expressions:

Fundamentals of Programming Session 7

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Operators And Expressions

In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. programs into their various phases.

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Arithmetic Operators. Portability: Printing Numbers

Lecture 3. More About C

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Computer System and programming in C

Chapter 3. Section 3.10 Type of Expressions and Automatic Conversion. CS 50 Hathairat Rattanasook

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Expressions and Statementst t. Assignment Operator. C Programming Lecture 6 : Operators. Expression

Chapter 3 Structured Program Development in C Part II

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Chapter 3: Operators, Expressions and Type Conversion

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

A flow chart is a graphical or symbolic representation of a process.

Information Science 1

Computer Programming CS F111

Data Types and Variables in C language

COMP Primitive and Class Types. Yi Hong May 14, 2015

Information Science 1

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

UNIT 3 OPERATORS. [Marks- 12]

Fundamental of Programming (C)

Basics of Programming

Programming in C++ 5. Integral data types

These are reserved words of the C language. For example int, float, if, else, for, while etc.

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Operators and Type Conversion. By Avani M. Sakhapara Assistant Professor, IT Dept, KJSCE

Fundamentals of Programming

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Programming for Engineers Iteration

Operators. Java operators are classified into three categories:

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

LECTURE 3 C++ Basics Part 2

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

1.3b Type Conversion

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Computers Programming Course 6. Iulian Năstac

DEPARTMENT OF MATHS, MJ COLLEGE

Informatica e Sistemi in Tempo Reale

Expression and Operator

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Fundamentals of Programming

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

BRANCHING if-else statements

Declaration and Memory

Prepared by: Shraddha Modi

Chapter 4 Homework Individual/Team (1-2 Persons) Assignment 15 Points

Chapter 2: Using Data

CS313D: ADVANCED PROGRAMMING LANGUAGE

ANSI C Programming Simple Programs

Chapter 3 Structure of a C Program

Reserved Words and Identifiers

JAVA OPERATORS GENERAL

Structures, Operators

Variables and Operators 2/20/01 Lecture #

Basic Assignment and Arithmetic Operators

ISA 563 : Fundamentals of Systems Programming

UNIT- 3 Introduction to C++

Basics of Java Programming

Fundamentals of Programming CS-110. Lecture 3

Week 2 Introduction to Computer and Algorithm (Part 2)

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

First of all, it is a variable, just like other variables you studied

C: How to Program. Week /Mar/05

Java enum, casts, and others (Select portions of Chapters 4 & 5)

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

SECTION II: LANGUAGE BASICS

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

Dept. of CSE, IIT KGP

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

Chapter 4 Expression & Operators

Course Outline Introduction to C-Programming

Lecture 3 Tao Wang 1

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen. Introduction

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

Chapter 2 - Introduction to C Programming

Programming for Engineers Introduction to C

Transcription:

Expressions 1

Expressions n Variables and constants linked with operators Arithmetic expressions n Uses arithmetic operators n Can evaluate to any value Logical expressions n Uses relational and logical operators n Evaluates to 1 or 0 (true or false) only Assignment expression n Uses assignment operators n Evaluates to value depending on assignment 2

Arithmetic Operators n Binary operators Addition: + Subtraction: Division: / Multiplication: * Modulus: % n Unary operators Plus: + Minus: Examples 2*3 + 5 10/3 1 + 3*25/5 7 distance / time 3.14* radius * radius a * x * x + b*x + c dividend / divisor 37 % 10 3

Contd. n Suppose x and y are two integer variables, whose values are 13 and 5 respectively x + y 18 x y 8 x * y 65 x / y 2 x % y 3 4

n All operators except % can be used with operands of all of the data types int, float, double, char (yes! char also! We will see what it means later) n % can be used only with integer operands 5

Type of Value of an Arithmetic Expression n If all operands of an operator are integer (int variables or integer constants), the value is always integer Example: 9/5 will be 1, not 1.8 Example: int a=9, b=5; printf( %d, a/b) will print 1 and not 1.8 6

n If at least one operand is real, the value is real Caution: Since floating-point values are rounded to the number of significant digits permissible, the final value is an approximation of the final result Example: 1/ 3.0 * 3.0 may have the value 0.99999 and not 1.0 7

Assignment Expression n Uses the assignment operator (=) n General syntax: variable_name = expression n Left of = is called l-value, must be a modifiable variable n Right of = is called r-value, can be any expression n Examples: velocity = 20 b = 15; temp = 12.5 A = A + 10 v = u + f * t s = u * t + 0.5 * f * t * t 8

Contd. n An assignment expression evaluates to a value same as any other expression n Value of an assignment expression is the value assigned to the l-value n Example: value of a = 3 is 3 b = 2*4 6 is 2 n = 2*u + 3*v w is whatever the arithmetic expression 2*u + 3*v w evaluates to given the current values stored in variables u, v, w 9

Contd. n Several variables can be assigned the same value using multiple assignment operators a = b = c = 5; flag1 = flag2 = y ; speed = flow = 0.0; n Easy to understand if you remember that the assignment expression has a value Multiple assignment operators are right-to-left associative 10

More Assignment Operators n +=, -=, *=, /=, %= n Operators for special type of assignments n a += b is the same as a = a + b n Same for -=, *=, /=, and %= n Exact same rules apply for multiple assignment operators 11

More Operators: Increment (++) and Decrement (--) n Both of these are unary operators; they operate on a single operand n The increment operator causes its operand to be increased by 1 Example: a++, ++count n The decrement operator causes its operand to be decreased by 1. Example: i--, --distance 12

Pre-increment versus postincrement n Operator written before the operand (++i, --i)) Called pre-increment operator (also sometimes called prefix ++ and prefix --) Operand will be altered in value before it is utilized in the program n Operator written after the operand (i++, i--) Called post-increment operator (also sometimes called postfix ++ and postfix --) Operand will be altered in value after it is utilized in the program 13

Contd. n Suppose x and y are two integer variables, whose values are 5 and 10 respectively. x += y x = y x *= y x /= y Stores 15 in x Evaluates to 15 Stores -5 in x Evaluates to -5 Stores 50 in x Evaluates to 50 Stores 0 in x Evaluates to 0 14

Type Casting n Convert a variable from one data type to another data type n Cast operator: (type_name) expression 15

Cont. #include <stdio.h> What is the output? int main() { int a = 25, b = 10; float result; result = a/b; printf( The result is %f\n, result); return 0; } 16

Cont. #include <stdio.h> What is the output? int main() { The result is 2.000000 int a = 25, b = 10; float result; result = a/b; printf( The result is %f\n, result); return 0; } 17

Cont. (Apply Type Cast) #include <stdio.h> What is the output? int main() { int a = 25, b = 10; float result; result = (float) a/b; printf( The result is %f\n, result); return 0; } 18

Cont. (Apply Type Cast) #include <stdio.h> What is the output? int main() { The result is 2.500000 int a = 25, b = 10; float result; result = (float) a/b; printf( The result is %f\n, result); return 0; } 19

Implicit casting n Compiler can implicitly cast the values in an expression based on the declared variable type n Implicit casting sequence priority: int -> long -> float -> double 20

Implicit Casting int a = 10; float b = 2.0 n The implicit type for (a*b) is float Rule: The final type of an expression is the highest priority type among all the variable types in the expression 21

Cont. #include <stdio.h> What is the output? int main() { int a = 25; float b = 10.0; float result; result = a/b; printf( The result is %f\n, result); return 0; } 22

Cont. #include <stdio.h> What is the output? int main() { The result is 2.500000 int a = 25; float b = 10.0; float result; result = a/b; printf( The result is %f\n, result); return 0; } 23

Logical Expressions n Uses relational and logical operators in addition n Informally, specifies a condition which can be true or false n Evaluates to value 0 or 1 0 implies the condition is false 1 implies the condition is true 24

Relational Operators n Used to compare two quantities. < is less than > is greater than <= is less than or equal to >= is greater than or equal to == is equal to!= is not equal to 25

Logical Expressions (count <= 100) ((math+phys+chem)/3 >= 60) ((sex == M ) && (age >= 21)) ((marks >== 80) && (marks < 90)) ((balance > 5000) (no_of_trans > 25)) (! (grade == A )) 26

Examples 10 > 20 is false, so value is 0 25 < 35.5 is true, so value is 1 12 > (7 + 5) is false, so value is 0 32!= 21 is true, so value is 1 n When arithmetic expressions are used on either side of a relational operator, the arithmetic expressions will be evaluated first and then the results compared a + b > c d is the same as (a+b) > (c+d) 27

Logical Operators Logical AND (&&) n Evalutes to 1 if both the operands are non-zero Logical OR ( ) n Result is true if at least one of the operands is non-zero X Y X && Y X Y 0 0 0 0 0 non-0 0 non-0 non-0 0 0 non-0 non-0 non-0 non-0 non-0 28

Contd n Unary negation operator (!) Single operand Value is 0 if operand is non-zero Value is 1 if operand is 0 29

Example n (4 > 3) && (100!= 200) 4 > 3 is true, so value 1 100!= 200 is true so value 1 Both operands 1 for &&, so final value 1 n (!10) && (10 + 20!= 200) 10 is non-0, so value!10 is 0 10 + 20!= 200 is true so value 1 Both operands NOT 1 for &&, so final value 0 n (!10) (10 + 20!= 200) Same as above, but at least one value non-0, so final value 1 30

A Special Operator: AddressOf (&) n Remember that each variable is stored at a location with an unique address n Putting & before a variable name gives the address of the variable (where it is stored, not the value) n Can be put before any variable (with no blank in between) int a =10; printf( Value of a is %d, and address of a is %d\n, a, &a); 31

Statements in a C program n Parts of C program that tell the computer what to do n Different types Declaration statements n Declares variables etc. Assignment statement n Assignment expression, followed by a ; Control statements n For branching and looping, like if-else, for, while, dowhile (to be seen later) Input/Output n Read/print, like printf/scanf 32

Example Declaration statement int a, b, larger; scanf( %d %d, &a, &b); larger = b; if (a > b) larger = a; Control statement Assignment statement printf( Larger number is %d\n, larger); Input/Output statement 33