Loose endsCS-2301, B-Term 20091 “Loose Ends” CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms The C Programming Language.
Advertisements

Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
Arrays in CCS-2301, B-Term Arrays in C (including a brief introduction to pointers) CS-2301, System Programming for Non-Majors (Slides include materials.
Numerical Computation Review and Continuation CS-2301, B-Term Numerical Computation in C Review and Continuation CS-2301, System Programming for.
Arrays in C & C++CS-2303, C-Term Arrays in C & C++ (including a brief introduction to pointers) CS-2303 System Programming Concepts (Slides include.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
Discussion of Assignment #2 CS-2301, B-Term Discussion of Assignment #2 CS-2301, System Programming for Non-Majors (Slides include materials from.
Introduction to FunctionsCS-2301 D-term Introduction to Functions CS-2301 System Programming C-term 2009 (Slides include materials from The C Programming.
Review of Exam #2CS-2301, B-Term Review of Exam #2 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language,
More Miscellaneous Topics CS-2301 B-term More Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The.
Arrays in CCS-2301 B-term Arrays in C (with a brief Introduction to Pointers) CS-2301, System Programming for Non-majors (Slides include materials.
"Loose ends"CS-2301 D-term “Loose Ends” CS-2301 System Programming C-term 2009 (Slides include materials from The C Programming Language, 2 nd edition,
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
More on Operator Overloading CS-2303, C-Term More on Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The.
Scope Rules and Storage Types CS-2303, C-Term Scope Rules and Storage Types CS-2303, System Programming Concepts (Slides include materials from The.
Conditionals, Loops, and Other Statements CS-2301 D-term Conditionals, Loops, and Other Kinds of Statements CS-2301 System Programming C-term 2009.
Miscellaneous topicsCS-2301 B-term Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
JavaScript, Third Edition
Chapter 4 Making Decisions
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Introduction to C Programming
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
Conditionals, Loops, and Other Statements CS-2301, B-Term Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors.
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
ISBN Chapter 7 Expressions and Assignment Statements.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
CPS120: Introduction to Computer Science Decision Making in Programs.
Arithmetic Expressions
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Arrays in CCIS 1057 Fall Arrays in C (with a brief Introduction to Pointers) CIS 1057 Computer Programming in C Fall 2013 (Slides include materials.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Introduction to FunctionsCIS 1057 Fall Introduction to Functions CIS 1057 Computer Programming in C Fall 2013 (Acknowledgement: Many slides based.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Free Ebooks Download Mba Ebooks By Edhole Mba ebooks Free ebooks download
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Variable Scope. When you declare a variable, that name and value is only “alive” for some parts of the program  We must declare variables before we use.
CS1010 Discussion Group 11 Week 5 – Functions, Selection, Repetition.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Programming Languages and Paradigms
Programming Paradigms
Conditionals, Loops, and Other Kinds of Statements
11/10/2018.
Flow of Control.
More about Numerical Computation
C Operators, Operands, Expressions & Statements
Flow of Control.
Classes, Constructors, etc., in C++
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Scope Rules and Storage Types
Differences between Java and C
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Function.
Classes, Objects and Methods
A Deeper Look at Classes
Chapter 7: Expressions and Assignment Statements Sangho Ha
Programming Languages and Paradigms
Function.
Scope Rules.
Introduction to Classes and Objects
Presentation transcript:

Loose endsCS-2301, B-Term “Loose Ends” CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel)

Loose endsCS-2301, B-Term Reminder – Relational Operators Relational operators –, >=, ==, != –Return 0 if false, 1 if true Let double pi = ; Then pi < 3 returns 0 pi <= 4 returns 1 pi == 3 returns 0

Loose endsCS-2301, B-Term Logical AND and OR operators A > B && C < D is true (i.e., has value 1 ) if and only if both A > B and C < D are both true A > B || C < D is true (i.e., has value 1 ) if either of A > B and C < D is true

Loose endsCS-2301, B-Term So what is the Difference … … between '|' and '||' ? … between '&' and '&&' ? Answer:– –'|' and '&' are bitwise operators Will evaluate both operands before doing bitwise arithmetic –'||' and '&&' are logical operators Will only evaluate one operand, if answer is known after first operand!

Loose endsCS-2301, B-Term Logical AND and OR operators (continued) expr 1 && expr 2 has value 1 if and only if both expr 1 and expr 2 are non-zero Otherwise, it has the value 0 expr 1 || expr 2 has value 1 if either expr 1 and expr 2 is non-zero

Loose endsCS-2301, B-Term Special Property of Logical Operators Logical operators are evaluated left-to-right Including side effects When the result is known, evaluation stops! E.g.:– in expr 1 && expr 2 –expr 1 is evaluated first –If result is zero, the && expression returns 0 expr 2 is not evaluated! –If result is non-zero, then expr 2 is evaluated If expr 2 is zero, && expression returns 0 Otherwise, && expression returns 1

Loose endsCS-2301, B-Term Special Property (continued) In expr 1 || expr 2 –expr 1 is evaluated first –If result is non zero, || expression returns 1 expr 2 is not evaluated! –If result is zero, then expr 2 is evaluated If expr 2 is non-zero, || expression returns 1 Otherwise, || expression returns 0

Loose endsCS-2301, B-Term Why this Special Property? if (n != 0 && x/n > y) {…} –Don’t want to attempt x/n if n is zero  zero- divide fault! Many similar situations in C, both && and || –E.g., following pointers, indexing arrays, etc. –expr 1 checks a limit situation, and then expr 2 tests what you really want to know.

Loose endsCS-2301, B-Term Questions?

Loose endsCS-2301, B-Term Scope Definition:– Scope of an identifier –The region of a program where an identifier is known –Compiler reports error on use of an identifier outside of its scope –An identifier may be used in different ways in different scopes of the same program –An identifier may be redefined for a different purpose in an inner scope

Loose endsCS-2301, B-Term Possible Scopes of Identifiers The entire program (i.e.,.c file) –Starts at point of declaration and continues to end A function –Starts with function name, continues to end of function (i.e., '}' ), includes parameters A compound statement –Starting at point of declaration and continuing to '}' for loop –Expressions and statement (C99) A function prototype –Limited to the prototype itself

Loose endsCS-2301, B-Term Scope – Examples int n; void func(int m){ int k; { double j; …; } }//func Visible throughout program (from this point on) m visible throughout function k visible throughout function (from this point on) j visible only within '{}' (from this point on)

Loose endsCS-2301, B-Term Scope – Name Spaces Different kinds of identifiers have separate name spaces Do not conflict with identifiers of other spaces E.g., Function and variable can share same name in same scope! Stylistically, this is a really, really bad idea!

Loose endsCS-2301, B-Term Scope – Re-using Identifiers Any identifier can be freely re-used in a separate scope Very common E.g., for (int i; …; …) — separate i variables! An identifier can be re-defined in an inner scope Happens often enough – e.g., int i;... for (int i; …; …) { … i … }; Refers to i of inner scope i of outer scope becomes inaccessible in inner scope

Loose endsCS-2301, B-Term Questions?

Loose endsCS-2301, B-Term Order of Evaluation Operator Precedence rules specify the order of operations … … but –don’t specify which operand is evaluated first z = f(x) + g(y) – side effects make a difference! –don’t specify which argument is evaluated first h(f(x), g(y)) – side effects make a difference! –Note: ',' specifies order in expressions, not argument lists!

Loose endsCS-2301, B-Term Questions?