Operators And Expressions

Similar documents
Operators and Expressions:

Operators & Expressions

Operators. Java operators are classified into three categories:

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

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

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

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

Department of Computer Science

Operators in C. Staff Incharge: S.Sasirekha

Lecture 3. More About C

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

Data Types and Variables in C language

Unit 3. Operators. School of Science and Technology INTRODUCTION

JAVA OPERATORS GENERAL

Expressions and Precedence. Last updated 12/10/18

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

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

Basics of Programming

Fundamental of Programming (C)

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. Lecture 3 COP 3014 Spring January 16, 2018

Operators in java Operator operands.

LECTURE 3 C++ Basics Part 2

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

Flow Control. CSC215 Lecture

Chapter 4: Basic C Operators

UNIT 3 OPERATORS. [Marks- 12]

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

The Arithmetic Operators

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

Unit-2 (Operators) ANAND KR.SRIVASTAVA

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

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

Expression and Operator

Prof. Navrati Saxena TA: Rochak Sachan

Fundamentals of Programming CS-110. Lecture 3

Programming for Engineers Iteration

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

QUIZ: What value is stored in a after this

Chapter 3: Operators, Expressions and Type Conversion

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

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

Variables and Operators 2/20/01 Lecture #

More Programming Constructs -- Introduction

COP 3275: Chapter 04. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

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

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

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

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

Infix to Postfix Conversion

Fundamentals of Programming

CHAPTER 3 BASIC INSTRUCTION OF C++

DEPARTMENT OF MATHS, MJ COLLEGE

Chapter 2. Lexical Elements & Operators

Programming in C++ 5. Integral data types

Arithmetic Operators. Portability: Printing Numbers

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

Prefix/Infix/Postfix Notation

Computers Programming Course 6. Iulian Năstac

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

Chapter 3 Structure of a C Program

Chapter 2: Using Data

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

YOLOP Language Reference Manual

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

3. Java - Language Constructs I

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

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

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

UNIT- 3 Introduction to C++

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

SECTION II: LANGUAGE BASICS

Information Science 1

Visual C# Instructor s Manual Table of Contents

PART II. Computation

Conditional Statement

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Data types, variables, constants

Full file at

Prepared by: Shraddha Modi

Structures, Operators

Basics of Java Programming

C - Basic Introduction

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

VARIABLES & ASSIGNMENTS

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

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen. Introduction

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Programming with Java

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

CHAD Language Reference Manual

C Programming

Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops.

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

C: How to Program. Week /Mar/05

Transcription:

Operators And Expressions Operators Arithmetic Operators Relational and Logical Operators Special Operators Arithmetic Operators Operator Action Subtraction, also unary minus + Addition * Multiplication / Division % Modulus -- Decrement ++ Increment 3 Precedence and Associativity Arithmetic Operators: High ++ -- - (unary minus) * / % Low + - - a * b c ((- a) * b) c 4 Expressions inside parentheses are evaluated first. Increment & Decrement Operators Provide a concise notation for incrementing or decrementing a variable by. * ( - 3) Are unary operators. Operators on the same level of precedence are evaluated from left to right. (Associativity). + + 3 + 4 5 ((( + ) + 3) + 4) 5 5 ++x or x++ --x or x-- Can be applied to variables but not to constants or ordinary expressions. ++i; legal cnt--; legal 777++; illegal ++(a * b -); illegal 6

May either prefix or postfix the operand. Increment Postfix: i++; Expression value is the current value (Before you increment) then it increments. use - then increment Prefix ++x; or Postfix x++; x = x + ; Increment Prefix: ++i; Expression value is the value After you increment. increment - then use ++ & -- both cause a value of a variable to change in memory. ( Have a side effect). 7 Decrement Postfix: i--; use - then decrement Decrement Prefix: --i; decrement - then use 8 Examples x =0; y = ++x; y x =0; y = x++; y 0 l = 4; n = 3; m = ; int i = 3, j =, k; x = l * n + m++; 4 x i++; i 4 j = ++i; j 5 i 5 k = i++; k 5 i 6 k = (--j+3) k 7 j 4 After the assignment to x. 3 m 9 0 int a, b, c=0; a = ++c; b = c++; a =? b =? c=? int b =, d = 4; 7--b*++d 7-((-b)*(++d))? int j =, k = 3, m = 4; int a,b; a = ; b = ; printf ( a+++b = %d/n, a+++b); a = ; b = ; printf ( a++ +b = %d/n, a++ +b); a = ; b = ; printf ( a+ ++b =% d/n, a+ ++b); j*=k=m+5 j=(j*(k=(m+5)))?

Relational and Logical Operators Relational Operators: Relational refer to the relationship that value can have with one another. Logical refers to the ways these relationships can be connected. > Greater than >= Greater than or equal < Less than <= Less than or equal = = Equal!= Not equal True is any value other than zero. False is zero. 3 Logical Operators: && AND OR! NOT 4 The truth table for the logical operators. True(), False(0). p q p&&q p q!p 0 0 0 0 0 0 0 0 0 0 Precedence and Associativity High! > >= < <= = =!= && Low!0&&0 0 ((!0)&&0) 0 FALSE 5 6 int x; x = 00; printf(''%d", x>0);? Examples!A is false (0) if A s value is:. is true () if A s value is:. Both are lower in precedence than the arithmetic operators.!!5! (!5)! (0) 0 > + 0 > ( + ) FALSE 0 Associativity: left to right. 5 && 3? 7 8 3

int i, j = ; j i j = j && (i = = 3); ( ) not needed j = j && (i = ); ( ) needed ) (i=) i ) (i = = 3) false 0 ) && j && true && true ) && j && 0 0 3) = j 0 3) = j 9 0 j i j = j (i/); 0 ) (i/) (/) ( ) not needed j i j =!j && (i = i + ); ) i + 3 ) = i 3 ) j true 3)!!j! 0 3) = j 4) && 0 && 3 5) = j 0 The Comma Operator Lowest precedence of all the operators. Causes a sequence of operations, do this and this and this. Is a binary operator. expression_, expression_ Associates left to right. expression_ is evaluated first expression_ is evaluated second The Comma Expression as a whole has the value and type of expression_. int i = ; j = 4; j = i++, i - j; * i 3 * j (3-4) higher precedence than, operator x = (y=3, y+); x 4 ( ) needed 3 4 4

It allows multiple initializations and multiple processing of indices. for (sum=0, i=; i<=n; ++i) sum += i; e.g. int i; i = 0; for ( ; i < 0; putchar ( a + i), i++); will output? 3rd expression in the for statement is a comma expression. Comma Operators can be useful in control statements though many C advocates discourage their use. 5 putchar is called,executed.then i is increased. Most commas in a program DO NOT represent comma operators. see text - pg. 99 6 The ( ) and [ ] Operators Parentheses are operators that increase the precedence of the operations inside them. Square brackets perform array indexing (See Chapter 9 for a discussion of array.) int main(void) { char s[80]; s[3] = 'X'; printf(''%c", s[3]); return 0; } 7 The Conditional Operator? Ternary operator. A powerful and convenient operator that replaces certain statements of the if-thenelse form. Exp? Exp: Exp3 Stands for: if Exp then Exp else Exp3 8 Examples x = 0; y = x>9? 00 : 00; x = 0; if(x>9) y = 00; else y = 00; int i, h, j = ; i = (j==)? :3; k = (i>j)? i:j; ( ) not needed i get k get max of I or j 9 30 5

This statement does what? c = (c > = a && c < = z )? c - ( z - Z ):c; IF True - have a Lower case letter in the variable C. Exp : c - ( z - Z ) will give Capital Letter of whatever is in C. e.g. a - ( z - Z ) 97 - ( 90) = 65 which is A. Expressions vs. Statements An expression in C is any valid combination of operators, constants, functions and variables. A statement is a valid expression followed by a semicolon. Func(); A function call as a statement. Y = X + Y; An assignment statement. B + Func(); Valid? IF False Capital Letter and leaves it alone. 3 3 Null Statement: ; [A semi-colon alone by itself]. Can be useful in loops & conditional statements. The body of a loop could be empty. Scanf( %d, &x); While(printf( %d,x), scanf( %d,&x)==) Spacing and Parentheses x=0/y-(7/x); x = 0 / y - (7/x); Redundant or additional parentheses do not cause errors or slow down the execution of an expression. x = y/3-34*temp+7; x = (y/3) - (34*temp) + 7; ; 33 34 6