Expressions So far, we have seen fairly simple expressions. For example, x = 4; z = z - y; if(x%4 == 0) However, expressions can be much more complex.

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

Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
CS201 - Repetition loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
CS150 Introduction to Computer Science 1
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
C. About the Crash Course Cover sufficient C for simple programs: variables and statements control functions arrays and strings pointers Slides and captured.
CIS 234: Order of Operations, Shortcut & Other Operators Dr. Ralph D. Westfall February, 2004.
Program Control Dilshad M. Shahid New York
1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators.
1 Introduction to Computers and Programming Class 3 Introduction to C Professor Avi Rosenfeld.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions.
Chapter 4: Basic C Operators
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
1 Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
Chapter 2 part #4 Operator
CSC 204 Programming I Loop I The while statement.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
LESSON 6 – Arithmetic Operators
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Spring 2005, Gülcihan Özdemir Dağ Lecture 5, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 5 Outline 5.0 Revisiting.
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
1 Expressions. 2 Variables and constants linked with operators  Arithmetic expressions Uses arithmetic operators Can evaluate to any value  Logical.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Chapter 4: Expressions Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 4 Expressions.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
4. EXPRESSIONS. Display the value of pi, to 5 decimal places, right justified, in 9 columns Read in someone’s height in feet and inches using.
Variables Symbol representing a place to store information
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
Modulus Operator #include main() { int a, b, q, r; printf(" a b a/b a%b\n"); while (scanf("%d%d", &a, &b) != EOF) { q = a / b; /* quotient */ r = a % b;
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Dr. Sajib Datta Sep 8,  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters.
UMBC CMSC 104 – Section 01, Fall 2016
ESC101: Introduction to Computing
Operators And Expressions
Chapter 5: Loops and Files.
Arithmetic Operator Operation Example + addition x + y
Increment and Decrement
Lecture 3 Expressions Richard Gesick.
Alternate Version of STARTING OUT WITH C++ 4th Edition
A First Book of ANSI C Fourth Edition
Assignment Operators Topics Increment and Decrement Operators
Repetition and Loop Statements
Assignment Operators Topics Increment and Decrement Operators
Associativity and Prescedence
UMBC CMSC 104 – Section 01, Fall 2016
Your questions from last session
Chapter 4: Expression and Operator
Review of Previous Lesson
HNDIT11034 More Operators.
Assignment Operators Topics Increment and Decrement Operators
Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a language-specific syntactical token.
Programming Techniques :: Arithmetic & Boolean Operators
Presentation transcript:

Expressions So far, we have seen fairly simple expressions. For example, x = 4; z = z - y; if(x%4 == 0) However, expressions can be much more complex.

Expressions – Complex Here is an example of a complex expression being used in a for loop. From page 41 of The C Programming Language, 2nd. Ed. by Kernighan and Ritchie, for(i=0; i<lim-1 && (c=getchar()) != ’\n’ && c != EOF; ++i) s[i] = c; We can see that the expressions for initializing and incrementing the counter are simple. The complexity occurs in the middle expression for testing whether to continue the loop.

Expressions Sometimes a complex expression is less cumbersome than the equivalent using only simple expressions. if (a && b && c && d && e) x = 5; does the same thing as if (a) if (b) if (c) if (d) if (e) x = 5;

Expressions – Examples To use more complex expressions, we often must either have knowledge of the order of precedence of the operators control the order of precedence using parentheses Recall the example from Kernighan and Ritchie for determining if a year is a leap year: if( year%4 == 0 && year%100 != 0 || year%400 == 0 )

Increment/Decrement, Part 2 We saw how to use the increment (++) and decrement (- -) operators to increase or decrease a variable’s value by 1, e.g., sum++. We can place the double plus or minus signs before the variable (prefix) or after the variable (postfix). Depending on where a variable is being used when incremented/decremented, this can make a difference.

Increment/Decrement, Part 2 The increment and decrement operators are unary operators, so they require one argument. We can use them to modify a variable’s value when that variable is part of a larger expression. Examples: x++; /* makes no difference */ ++x; /* makes no difference */ sum = sum + y++; /* y is added to sum, then y is incremented */ sum = sum + ++y; /* y is incremented, then the new value of y is added to sum */

Increment/Decrement, Part 2 Here we see equivalent methods for using a variable before incrementing it: x = 3 + y++;  x = 3 + y; y++; Here we see equivalent methods for incrementing a variable before using it: x = y;  y++; x = 3 + y;

Increment/Decrement, Part 2 This code int x = 3; printf("line 1: x is %d\n", x); printf("line 2: x is %d\n", x++); /* increment x after using */ printf("line 3: x is %d\n", x); printf("line 4: x is %d\n", ++x); /* increment x before using */ printf("line 5: x is %d\n", x); produces line 1: x is 3 line 2: x is 3 line 3: x is 4 line 4: x is 5 line 5: x is 5

Incrementing in for Loops int i; for(i = 0; i <= 5; i++) /* increment as postfix */ printf("%d ", i); for(i = 0; i <= 5; ++i) /* increment as postfix */ printf("%d ", i); for(i = 5; i >= 0; i--) /* increment as postfix */ printf("%d ", i); for(i = 5; i >= 0; --i) /* increment as postfix */ printf("%d ", i); produces

Incrementing in for Loops Why doesn’t it matter in a for loop if we use the postfix or prefix versions? Remember that for a for loop we can create an equivalent while loop. int i, x = 0; for(i = 0; i < 5; i++) x = x + i; is equivalent to int i, x = 0; i = 0; while(i < 5) { x = x + i; i++; }

Functions in Expressions When we call a function that returns a value, that value is used in the expression. y = 4; x = x + doubleNum(y); /* assume doubleNum returns 8 */ This is equivalent to y = 4; x = x + 8; This means we can use functions in expressions just as we would use other values.

Functions in Expressions Here we use a function in the typical way. #include int squareNum(int num) { return num*num; } int main(void) { int x = 1; int sq = squareNum(x); while( sq <=100 ) { printf("The square of %2d is %3d\n", x, sq); x++; sq = squareNum(x); }

Functions in Expressions This program has the same functionality but includes the function in the test of the while loop. #include int squareNum(int num) { return num*num; } int main(void) { int x = 1, sq; while( (sq = squareNum(x)) <= 100 ) { printf("The square of %2d is %3d\n", x, sq); x++; }

Functions in Expressions #include int doubleNum(int num) { return 2*num; } int main(void) { int x = 8, output; output = doubleNum( doubleNum(x) ); printf("output is %d\n", output); /* output is 32 */ } To evaluate, we must work our way inside much the way we do when looking at mathematical expressions with many parentheses, e.g., (((x − 3) − 2x 2 ) + 8)