Outline

19
Outline Symbolic Constants Character Input and Output if … else switch…case

description

Outline. Symbolic Constants Character Input and Output if … else switch…case. Symbol constants. Symbol name (Meaningful, easy to read). #include #define LOWER 0 #define UPPER 300 #define STEP 20 /* print F-S table */ main() { int fahr; - PowerPoint PPT Presentation

Transcript of Outline

Page 1: Outline

Outline Symbolic Constants Character Input and Output if … else switch…case

Page 2: Outline

Symbol constants

#include <stdio.h>

#define LOWER 0#define UPPER 300#define STEP 20

/* print F-S table */main(){ int fahr;

for(fahr=LOWER; fahr <= UPPER; fahr = fahr+STEP) printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));}

Symbol name(Meaningful, easy to read)

Symbol value

Replace this symbol nameat compile time

Page 3: Outline

Character I/O A text stream is a sequence of characters getchar() getch() getche() getc()

putchar(c) putch(c,stdout) putc(c,stdout)

I/O devices are also takenas files

輸入stdin

輸出stdout

Page 4: Outline
Page 5: Outline

Example: File copying

#include <stdio.h>

/* echo, version 1 */main(){ int c;

c=getchar(); while( c != EOF ){ putchar(c); c = getchar(); }}

not equal to

End Of File

A constant definedin stdio.hNOT the same asany char values

Page 6: Outline

Example: File copying EOF

Print the value of EOF

End of file

OS Keyboardterminationsignal(ctrl-Z)

#include <stdio.h>

main(){ printf("EOF = %d\n", EOF);}

Page 7: Outline

Example: File copyingAssignment

c= getchar(); Assignment is an expression and has a valu

e, which is the value of the left hand side after assignment.

#include <stdio.h>main(){ int c;

while( (c=getchar()) != EOF ){ putchar(c); }}

Precedence of = and !=

Textbook p. 5-25

Page 8: Outline

Example: Character counting

#include <stdio.h>

main(){ long nc; /* number of character */

nc = 0; while(getchar() != EOF) ++nc; printf("%ld\n",nc);}

Good namingConventionFor variables

nc = nc+1;

32-bitinteger

Page 9: Outline

Example : Character counting 2

#include <stdio.h>

main(){ double nc;

for(nc = 0; getchar()!= EOF; ++nc) ; printf("%.0f\n",nc);}

null statement

Increaserange

Page 10: Outline

Outline Symbolic Constants Character Input and Output if … else switch … case

Page 11: Outline

Example: Line counting

#include <stdio.h>

/* count lines */main(){ int c, nl;

nl = 0; while( (c=getchar()) != EOF) if(c == '\n') ++nl; printf("%d\n", nl);}

Test condition

is equal to

條件測試characterconstant

ASCII valueof input char.

“\n” ?

Page 12: Outline

if statement

if( expression ){ statement 1; }else{ statement 2; }

TestYES

Statement 1

NO

if

statement 3;Statement 2

Statement 3

else

Page 13: Outline

if-else: some notes

if (expression)

Nested if Else is associating with the closest

previous else-less if

if (expression != 0)

if (n > 0) if (a > b) z = a; else z = b;

if (n > 0){ if (a > b) z = a; }else z = b;

if (n > 0) if (a > b) z = a;else z = b;

Page 14: Outline

Example 李伯伯 example 1.1-3

Page 15: Outline

Outline Symbolic Constants Character Input and Output if … else switch … case

Page 16: Outline

Switch Multi-way decision

switch(expression){ case const-expr : statements case const-expr : statements … default: statements}

optional

integer-valued constant

* All case expressions must be different

Page 17: Outline

SwitchTest

expression

switch

case

expr1 statements 1

expr2

case

defaultstatements N

statements 2break;

(fall-through)

Page 18: Outline

#include <stdio.h>main(){ int c, i, nwhite, nother, ndigit[10]; nwhite = nother = 0; for(i=0; i<10; i++) ndigit[i] = 0; while( (c=getchar()) != EOF){ switch(c){ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': ndigit[c-'0']++; break; case ' ': case '\n': case '\t': nwhite++; break; default: nother++; break; } } printf("digits="); for(i=0; i<10; i++) printf(" %d", ndigit[i]); printf(", white space = %d, other = %d\n", nwhite, nother); return 0;}

Not necessary, but good

Page 19: Outline

printf("digits="); for(i=0; i<10; i++) printf(" %d", ndigit[i]); printf(", white space = %d, other = %d\n", nwhite, nother); return 0;}

*note: fall-throughs are necessary when there areMultiple labels for a single computation