Assignment Operators = +=-= *= /=%= Statement Equivalent Statement a = a + 2 ;a += 2 ; a = a - 3 ;a -= 3 ; a = a * 2 ;a *= 2 ; a = a / 4 ; a /= 4 ; a =

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
1 10/16/06CS150 Introduction to Computer Science 1 switch Selection Structure.
Control Statements (II) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Control Structures in C++ while, do/while, for switch, break, continue.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Basic Input/Output and Variables Ethan Cerami New York
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 1Winter Quarter Switch Case Structures.
CMSC 104, Version 8/061L15Switch.ppt The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
1 Lecture 4 for loops and switch statements Essentials of Counter-Controlled Repetition Counter-controlled repetition requires  Name of control.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
EC-111 Algorithms & Computing Lecture #4 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Adv. UNIX:io/91 Advanced UNIX v Objectives of these slides: –look in some detail at standard input and output in C Special Topics in Comp.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
CS140: Intro to CS An Overview of Programming in C (part 3) by Erin Chambers.
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
Spring 2005, Gülcihan Özdemir Dağ Lecture 5, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 5 Outline 5.0 Revisiting.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Iterations Very Useful: Ability to repeat a block of code Example:
Program to calculate product of odd numbers b/w 1 and 15 #include main() { int prod = 1, x; for(x = 1; x
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
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,
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
CMSC 1041 More Loops ‘for’ loops and ‘do-while’ loops.
Switch Selection Structure (L14) * General Form of the switch Statement * Details of switch Statement * Flowchart of switch Statement * cin.get * Character.
Chapter 1 Basic C Programming
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Part 1 Outline Counter-Controlled Repetition: Example Sentinel-Controlled Repetition:
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CHARACTER INPUT / OUTPUT AND INPUT VALIDATION. Introduction Input and output devices: keyboards, disk drives, mouse, monitors, printers. I/O functions.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
Dale Roberts Program Control Department of Computer and Information Science, School of Science, IUPUI Fall 2003 CSCI 230 Dale Roberts, Lecturer
 2003 Prentice Hall, Inc. All rights reserved. ECE 2551 Dr. S. Kozaitis Fall Chapter 5 - Control Statements: Part 2 Outline 5.3 for Repetition.
Chapter 2 Overview of C.
Chapter 18 I/O in C.
Chapter 4 - Program Control
Chapter 2.1 Control Structures (Selection)
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Control Structures Lecture 7.
The C “switch” Statement
مبانی برنامه‌سازی با C++ جلسه دوم
The C “switch” Statement
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
- Additional C Statements
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
The switch Statement Topics Multiple Selection switch Statement
The switch Statement Topics Multiple Selection switch Statement
Program Control Topics While loop For loop Switch statement
switch Selection Structure
Computer Programming Techniques Semester 1, 1998
Arrays I Handling lists of data.
2.6 The if/else Selection Structure
CprE 185: Intro to Problem Solving (using C)
Dale Roberts, Lecturer IUPUI
Chapter 4 - Program Control
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
The switch Statement Topics Multiple Selection switch Statement
UMBC CMSC 104 – Section 01, Fall 2016
CprE 185: Intro to Problem Solving (using C)
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Switch Case Structures
Presentation transcript:

Assignment Operators = +=-= *= /=%= Statement Equivalent Statement a = a + 2 ;a += 2 ; a = a - 3 ;a -= 3 ; a = a * 2 ;a *= 2 ; a = a / 4 ; a /= 4 ; a = a % 2 ;a %= 2 ; b = b + ( c + 2 ) ;b += c + 2 ; d = d * ( e - 5 ) ;d *= e - 5 ;

int number = 10, sum = 0; int i; printf("enter int: "); scanf("%d", &i); switch (i) { case 1: printf("Exam2 =>Nov18\n"); printf("1:sum =%d number=%d\n", sum, number); break; case 2: number +=3; printf("2:sum =%d number=%d\n", sum, number); case 3: sum = number++; printf("3:sum =%d number=%d\n", sum, number); case 4: sum = --number; printf("4:sum =%d number=%d\n", sum, number); case 5: printf("fuzzball\n"); break; default: sum = number; printf("D:sum =%d number=%d\n", sum, number); } printf("Final: sum =%d number=%d\n", sum, number); What is the output?

Problems with Reading Characters  When getting characters, whether using scanf( ) or getchar( ), realize that you are reading only one character.  What will the user actually type? The character he/she wants to enter, followed by pressing ENTER.  So, the user is actually entering two characters, his/her response and the newline character.  Unless you handle this, the newline character will remain in the stdin stream causing problems the next time you want to read a character. Another call to scanf() or getchar( ) will remove it.

Reading Chars #include int main ( ) { char myChar, newline ; printf (“Enter a character: “) ; myChar = getchar( ) ; newline = getchar( ) ; /* could also usescanf(“%c”, &newline) ; */ printf (“The value of |%c| is |%c|.\n”, myChar, newline ) ; return 0 ; } User’s input is a Lets use this as an Enter symbol What happens when user hits Enter key on the keyboard?  \n

Reading Chars #include int main ( ) { char myChar='o', newline ; printf(“Enter e to exit\n") ; while(myChar!=‘e') { printf("Enter a character:") ; myChar = getchar( ); newline = getchar( ); printf ("The value of |%c| is |%c|.\n", myChar, newline ) ; } printf(“See You Later!") ; return 0; }

Additional Concerns with Garbage in stdin  When we were reading integers using scanf( ), we didn’t seem to have problems with the newline character, even though the user was typing ENTER after the integer.  That is because scanf( ) was looking for the next integer and ignored the newline (whitespace).  If we use scanf (“%d”, &num); to get an integer, the newline is still stuck in the input stream.  If the next item we want to get is a character, whether we use scanf( ) or getchar( ), we will get the newline.  We have to take this into account and remove it.

switch in a event controlled loop  Create a menu that continually prompts the user to enter 'H' to print a greeting in English, ‘A’ to print a greeting in Hawaiin, 'N' to print a greeting in Hindi, and S to print a greeting in Hebrew. The menu should also include an option 'E' which exits the program. Menu should look something like this: Please enter: H -> To print greeting in English A -> To print greeting in Hawaiian N -> To print greeting in Hindi S -> To print greeting in Hebrew E -> To exit 'H', it prints "Hello"; 'A' it prints "Aloha"; 'N' it prints "Namasthe"; ‘S’ it prints "Shalom". 'E', the program should print "Adios" before it exits the program.

getchar( ) Example Using EOF #include int main () { int grade, aCount, bCount, cCount, dCount, fCount ; aCount = bCount = cCount = dCount = fCount = 0 ; while ( (grade = getchar( ) ) != EOF ) { switch ( grade ) { case ‘A’: aCount++; break ; case ‘B’: bCount++; break ; case ‘C’ : cCount++; break ; case ‘D’: dCount++; break ; case ‘F’: fCount++; break ; default : break ; } return 0 ; }