Iteration. Side effects

Similar documents
Tail recursion. Decision. Assignment. Iteration

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Flow Control. CSC215 Lecture

The Design of C: A Rational Reconstruction: Part 2

Continued from previous lecture

LESSON 4. The DATA TYPE char

Introduction to C Programming

Introduction. C provides two styles of flow control:

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

The Arithmetic Operators

Review of the C Programming Language for Principles of Operating Systems

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

Chapter 7 Control I Expressions and Statements

C-LANGUAGE CURRICULAM

Review of the C Programming Language

LESSON 6 FLOW OF CONTROL

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

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

High Performance Computing in C and C++

Computer Security. Robust and secure programming in C. Marius Minea. 12 October 2017


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

Left to right design 1

C Course. IIT Kanpur. Lecture 3 Aug 31, Rishi Kumar <rishik>, Final year BT-MT, CSE

G Programming Languages - Fall 2012

Programming for Engineers Iteration

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

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

The PCAT Programming Language Reference Manual

C++ Programming Lecture 1 Software Engineering Group

Variables and literals

Lecture 3. More About C

Module 4: Decision-making and forming loops

Chapter 4 C Program Control

A programming language requires two major definitions A simple one pass compiler

This book is licensed under a Creative Commons Attribution 3.0 License

C Programming Language. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

Princeton University Computer Science 217: Introduction to Programming Systems The Design of C

Arithmetic Operators. Portability: Printing Numbers

o Echo the input directly to the output o Put all lower-case letters in upper case o Put the first letter of each word in upper case

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

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Lecture 6. Statements

The SPL Programming Language Reference Manual

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

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

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

x = 3 * y + 1; // x becomes 3 * y + 1 a = b = 0; // multiple assignment: a and b both get the value 0

Syntax. A. Bellaachia Page: 1

1 Lexical Considerations

Structured Programming. Jon Macey

Operators And Expressions

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

MODULE 2: Branching and Looping

YOLOP Language Reference Manual

CS 314 Principles of Programming Languages. Lecture 9

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

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

LECTURE 18. Control Flow

Chapter 7. Additional Control Structures

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

8. Control statements

Reminder. Sign up for ee209 mailing list. Precept. If you haven t received any from ee209 yet Follow the link from our class homepage

Programming for Electrical and Computer Engineers. Loops

ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C

A Fast Review of C Essentials Part I

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

Principles of Programming Languages COMP251: Syntax and Grammars

Language Extensions for Vector loop level parallelism

Chapter 9: Dealing with Errors

Control Structures. Outline. In Text: Chapter 8. Control structures Selection. Iteration. Gotos Guarded statements. One-way Two-way Multi-way

C Pointers. 7.2 Pointer Variable Definitions and Initialization

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

Programming Languages Third Edition

Introduction to C/C++ Lecture 3 - Program Flow Control

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Input/output functions

Fundamental of Programming (C)

Introduction to C++ with content from

Language Reference Manual

Chapter 13 Control Structures

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

Computer Programming

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

COP4020 Programming Languages. Control Flow Prof. Robert van Engelen

Formal Specification and Verification

TED Language Reference Manual

Input/output functions

Statements. Control Flow Statements. Relational Operators. Logical Expressions. Relational Operators. Relational Operators 1/30/14

Chapter 7. Basic Types

Strings. Daily Puzzle

Fundamentals of Programming

A Quick Introduction to C Programming

CSE au Final Exam Sample Solution

Transcription:

Computer programming Iteration. Side effects Marius Minea marius@cs.upt.ro 17 October 2017

Assignment operators We ve used the simple assignment: lvalue = expression lvalue = what can be on the left of an assignment so far: variable; see later: array element; pointer dereference Compound assignment operators: += -= *= /= %= x += expr is a shorthand for x = x + expr etc. later: also for bitwise assignment operators >> << & ^ use them: shorter and makes intent of transformation clearer Increment/decrement operators prefix/postfix: ++ -- ++i increments i, expression value is value after assignment i++ increments i, expression value is value before assignment both have same side effect (assignment) but different value int x=2, y, z; y = x++; /* y=2,x=3 */; z = ++x; // x=4,z=4 same effect as statements, not same value in expressions

Side effects and sequence points In a complex expression, when do side effects actually take place? Most operators have unspecified evaluation order of operands (e.g., arithmetic) only partial order of computations is imposed. But: All side effects must complete before crossing a sequence point. Examples of sequence points (standard, Annex C) for function calls, between evaluating the function designator (function expression) + arguments, and the actual call for, && between evaluating first and second operand in? : between evaluating the first operand and the second/third If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings. C standard, 6.5 Expressions Thus, i = i++ or a[i] = i++ are undefined!

Caution with multiple side effects! Even when order of side effects is well defined, use with caution! DON T write: return i++; assignment to i is useless, since the function returns obscures intent: should it be return i; or return i+1;? DON T: c = toupper(c); return c; DO: return toupper(c); DON T read multiple characters in an expression: if (getchar() == * && ((c = getchar()) == / ) if first comparison fails, second char is not read (c has previous / uninitialized value) hard to reason about program behavior

The for statement for ( init-clause ; test-expr ; update-expr ) statement is equivalent with: * except: continue statement, see later init-clause; while (test-expr) { statement update-expr ; Any of the 3 parts in (...) may be missing, but semicolons stay If test-expr is absent, it is considered true (infinite loop) Before C99: init part could only be an expression, e.g. i = 0 Since C99: init-clause can also be a declaration, e.g. int i = 0 scope of declared identifiers is loop body only USE loop scope for counters, if they are not needed later (scope of identifiers should only be as much as needed) WARNING! The semicolon ; is the empty statement DO NOT use after closing ) of for unless you want empty body!

Counting with for loops #include <stdio.h> int main(void) { unsigned n = 5; while (n--) // from n-1 to 0: n--!= 0, postdecrement printf("loop 1: n = %d\n", n); n = 5; // reinitialize after countdown to 0 for (int i = 0; i < n; ++i) // from 0 to n-1 printf("loop 2: counter %d\n", i); for (int i = 1; i <= n; ++i) // from 1 to n printf("loop 3: counter %d\n", i); for (int i = n; i > 0; --i) // from n to 1 printf("loop 4: counter %d\n", i); for (int i = n; i--;) // from n-1 to 0, postdecr. printf("loop 5: counter %d\n", i); return 0;

Counting with for loops If direction does not matter, this is shortest: for (int i = n; i--;) also easier to compare to zero Warning: test expression is computed every time avoid needless computation, e.g. for (int i = 0; i < strlen(s); ++i) (compiler may optimize some, but not always) If needed, precompute upper bound: for (int i = 0, len = strlen(s); i < len; ++i)

The break statement Exits the immediately enclosing loop or switch statement Used if we don t want to continue the remaining processing Usually: if ( condition ) break; #include <ctype.h> #include <stdio.h> int main(void) // count words in input { // word: sequence of non-whitespace chars unsigned nrw = 0; for (int c;; ++nrw) { // exit w/ break; count each iter while (isspace(c = getchar())); // consume whitespace if (c == EOF) break; // done while (!isspace(c = getchar()) && c!= EOF); // word // word counted in loop update part printf("%u\n", nrw); return 0;

Example: rewrite, starting every word with uppercase word = sequence of non-whitespace chars (common term usage) \t \n \v \f \r and space, as checked by isspace() #include <ctype.h> #include <stdio.h> int main(void) { for (int c; (c = getchar())!= EOF; ) if (isspace(c)) putchar(c); else { // first non-space putchar(toupper(c)); // print uppercase if letter while ((c = getchar())!= EOF) { // still word? putchar(c); // print even if space if (isspace(c)) break; // exit inner loop return 0;

The continue statement jumps to the end of the loop body in a while, do or for loop i.e., to update expression in for and to test in do or while void printfact(unsigned n) { // print prime factors of n for (unsigned d = 2; d*d <= n; d += 1 + d % 2) { if (n % d!= 0) continue; // not divisible; next d unsigned exp = 1; while ((n /= d) % d == 0) ++exp; printf ("%u", d); // write current factor if (exp > 1) printf("^%u", exp); // write exponent if (n > 1) putchar( * ); else return; printf("%u", n); // 0, 1 or remaining prime Use continue sparingly (much less common than break) can make code clearer, if decision to skip is early, and loop is long otherwise, a simple if may be easier to read and understand.

The goto statement Syntax: goto statementlabel ; Jumps to statement with given label, only inside same function. Any statement can be prefixed with a label followed by : Discouraged (unstructured code); ok to jump out of several loops. #include <ctype.h> #include <stdio.h> int main(void) // count chars, words, lines { unsigned nc = 0, nw = 0, nl = 0; for (int c; (c = getchar())!= EOF; ++nc) { if (!isspace(c)) // word start for (++nc, ++nw;!isspace(c = getchar()); ++nc) if (c == EOF) goto outloop; // exit both loops if (c == \n ) ++nl; // c isspace here; ++nc in for outloop: printf("%u lines, %u words, %u chars\n", nl, nw, nc); return 0;

The switch statement: example Used for multiple branches depending on an integer value can be clearer/more efficient than a multiple if... else #include <stdio.h> int main(void) { int a = 3, b = 4, c, r; switch (c = getchar()) { case + : r = a + b; break; // end switch case - : r = a - b; break; case x : c = * ; // no effect on flow, continue case * : r = a * b; break; case / : r = a / b; break; default: fputs("unknown operator\n", stderr); return 1; // main finished with error printf("result: %d %c %d = %d\n", a, c, b, r); return 0;

The switch statement Syntax: switch ( integer-expression ) statement statement is a block with multiple statements, some labeled: case value: statement The integer expression is evaluated. If the statement has a case label with that value, jump to it Otherwise, if there is a default, label, jump to it Else, do nothing (goes on to next statement after switch) A statement may have several labels (flow jumps to same code) case val1: case val2: statement WARNING! Normal statement sequencing applies: control flow does not stop at the next case label (it s just a label) DON T forget: to exit switch statement, use break;

switch vs. if... else A multiple if... else statement will do multiple tests (until one succeeds) A switch statement may be implemented using a jump table: the expression is evaluated and used as index in a table of addresses can be more efficient if range of possible values is limited (also: compiler may limit range of values to 1023, cf. standard) More importantly: a switch may be easier to read But: be careful not to forget break where needed!

Writing and testing loops Think about: what variables change in each iteration? what is the loop continuation/stopping condition? Don t forget update of variable that controls loop! (otherwise will loop forever) On loop exit, the loop condition is false. use this to reason about what happens next Inspect/check/test the program: mentally, running it pencil and paper on simple cases then with more complex tests, including corner cases

Example: Parsing expressions Expression syntax: rigorously defined by a grammar frequent notation: Backus-Naur form (BNF) Writing code: one function for each defined notion (nonterminal) Prefix expressions (no parantheses/precedence needed) expr ::= number operator expr expr Postfix expressions expr ::= number expr expr operator Left recursive, can t decide branch (start is always number) rewrite grammar: expr ::= number restexpr restexpr ::= ɛ expr operator restexpr ɛ is usual notation for empty string

Parsing usual (infix) expressions Simplest attempt: ambiguous, no associativity or precedence expr ::= number expr operator expr ( expr ) separate additive/multiplicative expressions/operators expr ::= term expr + term expr - term term ::= factor term * factor term / factor factor ::= number ( expr ) expr and term still left-recursive rewrite: expr ::= term restexpr restexpr ::= ɛ + term restexpr - term restexpr term ::= factor restterm restterm ::= ɛ * factor restterm / factor restterm factor ::= number ( expr )

Writing code from recursive definitions One function for each nonterminal Function structure determined by computation (data flow) expr ::= term restexpr restexpr needs previous term gets it as parameter int expr(void) { return restexpr(term()); restexpr ::= ɛ + term restexpr - term restexpr restexpr is right-recursive write as tail-recursive function int restexpr(int t1) { int c = getchar(); if (c == + ) return restexpr(t1 + term()); else... or rewrite as loop within expr(), accumulate expression value int expr(void) { int c, e = term(); for (;;) { // use break; to stop if ((c = getchar()) == + ) e += term; else... // try to write the complete program!