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

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
For loops For loops are controlled by a counter variable. for( c =init_value;c
Engineering Computing I Chapter 1 – Part B A Tutorial Introduction continued.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
1 Homework Assignments Turn in HW1 (If not done yet, catch up!) Questions about HW1? Anyone still stuck on apply / UNIX account? Everyone have the books?
The C programming language: Introduction Fall 2003, Jen-Chang Liu.
Character Input and Output C and Data Structures Baojian Hua
流程控制: while loop 迴圈 Test condition Enter loop Yes (non-0) Execute Loop body no exit F=0 F=F+20 … F=F
Imperative Programming Prof. Béat Hirsbrunner Amine Tafat, PhD Student Matthias Buchs and Raphaël Lesceux, Graduate Students Department of Informatics.
0 Arrays (1/2) #include /* count digits, white space, others */ main() { int c, i, nwhite, nother; int ndigit[10]; nwhite = nother = 0; for (i = 0; i
Prof. Béat Hirsbrunner Fulvio Frapolli, PhD Student (exercises) Bachelor students : - Major in computer science (first year, 2nd term) - Major in information.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Character Input and Output
Review: midterm #9 #include void main(void) { int c; c = getchar(); if(c>=48){ if(c
CMSC 104, Version 8/061L15Switch.ppt The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading.
Introduction to C Language
Spring 2005, Gülcihan Özdemir Dağ Lecture 4, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 4 Outline 4.0 Reading.
C Programming A Modern Approach
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
Conditional Statement
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Computer programming Lecture 4. Lecture 4: Outline Making Decisions [chap 6 – Kochan] –The if Statement –The if-else Construct –Logical Operators –Boolean.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
Programming Language  C Tutorial Introduction 主講人:虞台文.
Introduction to Computer Algorithmics and Programming Ceng 113 Program Control Statements.
1/16 Programski jezik C Vladimir Filipović
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
Chapter 05 (Part III) Control Statements: Part II.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
CS115 FALL Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
CMSC 104, Version 9/011 The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading Section 4.7,
Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd.
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
Lecture 15: Course Review BJ Furman ME 30 16MAY2011.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
The Cast Operator The cast operator converts explicitly from one data type of an expression to another. For example, if x is of type int, the value of.
CPT: Chars/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to look at how C processes characters 4. Character.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Chapter 4 – C Program Control
ECE Application Programming
Chapter 4 - Program Control
C programming---basic
The C “switch” Statement
INC 161 , CPE 100 Computer Programming
The C “switch” Statement
Structured Programming (Top Down Step Refinement)
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
- Additional C Statements
Chapter 5 Decision Making and Branching
Chapter 4 - Program Control
The switch Statement Topics Multiple Selection switch Statement
The switch Statement Topics Multiple Selection switch Statement
B. Ramamurthy University at Buffalo
Your questions from last session
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Dale Roberts, Lecturer IUPUI
Chapter 4 - Program Control
Lec 6 Loop Statements Introduction to Computer Programming
The switch Statement Topics Multiple Selection switch Statement
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Switch Case Structures
Presentation transcript:

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

Symbol constants #include #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 name at compile time

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 taken as files 輸入 stdin 輸出 stdout

Example: File copying #include /* echo, version 1 */ main() { int c; c=getchar(); while( c != EOF ){ putchar(c); c = getchar(); } not equal to End Of File A constant defined in stdio.h NOT the same as any char values

Example: File copying EOF Print the value of EOF End of file OSKeyboard termination signal(ctrl-Z) #include main() { printf("EOF = %d\n", EOF); }

Example: File copying Assignment c= getchar(); Assignment is an expression and has a value, which is the value of the left hand side after assignment. #include main() { int c; while( (c=getchar()) != EOF ){ putchar(c); } Precedence of = and != Textbook p. 5-25

Example: Character counting #include main() { long nc; /* number of character */ nc = 0; while(getchar() != EOF) ++nc; printf("%ld\n",nc); } Good naming Convention For variables nc = nc+1; 32-bit integer

Example : Character counting 2 #include main() { double nc; for(nc = 0; getchar()!= EOF; ++nc) ; printf("%.0f\n",nc); } null statement Increase range

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

Example: Line counting #include /* 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 條件測試 character constant ASCII value of input char. “ \n ” ?

if statement if( expression ){ statement 1; } else{ statement 2; } Test YES Statement 1 NO if statement 3; Statement 2 Statement 3 else

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;

Example 李伯伯 example 1.1-3

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

Switch Multi-way decision switch(expression){ case const-expr : statements … default: statements } optional integer-valued constant * All case expressions must be different

Switch Test expression switch case expr1 statements 1 expr2 case … default statements N statements 2 break; (fall-through)

#include 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

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 are Multiple labels for a single computation