LEC20080919

# LEC20080919 - Introduction to Computer Programming CSC180...

This preview shows pages 1–9. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Computer Programming September 19, 2008 CSC180 Fall 2008, University of Toronto Global variables Required readings: chapter 5 and 6. • Also called external variables • Declared outside of any function • Accessible by all following functions • Remain in the memory until the program finishes • Useful when many functions need to share and communicate data CSC180 Fall 2008, University of Toronto 1 • Example: #include <stdio.h> int n; /* global variable, accessible by functions below */ void print() { printf(‘‘%d\n’’, n); } void reset() { n = 0; } void add(int a) { n += a; } void sub(int a) { n -= a; } CSC180 Fall 2008, University of Toronto 2 int main() { print(); add(3); print(); sub(2); print(); reset(); print(); return 0; } • The above example prints: 3 1 CSC180 Fall 2008, University of Toronto 3 if, else if, else • Make multi-way decision; frequently used. • Conditions are tested in order • If any condition is true, the statement associated with it is executed, and the whole chain is terminated. • Several statements are grouped using curly braces. • Example (modify a part of read.c ): if (scanf(‘‘%d’’, &n) < 1) { CSC180 Fall 2008, University of Toronto 4 printf(‘‘Illegal input, use numbers\n’’); return 1; } else if (n < 0) { printf(‘‘Input must be a non-negative integer\n’’); return 1; } else if (n == 0) /* no lines? */ read_ch2_twice(); else if (n > 0 && n <= 1000) /* some lines? */ read_ch2_once(); else /* enough lines? */ ignore_ch2(); • Note: else if part is optional; else part is also optional. CSC180 Fall 2008, University of Toronto 5 The dangling else problem • else pairs with the closest previous unpaired if . • Example: if (n > 0) /* version 1 */ if (a > b) z = a; else z = b; if (n > 0) { /* version 2 */ if (a > b) z = a; } else CSC180 Fall 2008, University of Toronto 6 z = b; • Version 1 and version 2 are different. In version 1, else associates with if (a > b) , but in version 2, else associates with if (n > 0) . • Example: if (n >= 0) for (i = 0; i < n; i++) if (s[i] > 0) { printf(‘‘...’’); return i; } else /* WRONG */ printf(‘‘error -- n is negative\n’’); • In the above example, else associates with if (s[i] > 0) instead of if (n >= 0) ....
View Full Document

## This note was uploaded on 01/10/2011 for the course CSC 180 taught by Professor Na during the Fall '01 term at University of Toronto.

### Page1 / 25

LEC20080919 - Introduction to Computer Programming CSC180...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online