ECE Application Programming

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

ECE Application Programming
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
1 CSC103: Introduction to Computer and Programming Lecture No 11.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
Flow of Control Part 1: Selection
Dale Roberts Program Control using Java - Selection Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Iterations Very Useful: Ability to repeat a block of code Example:
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
5 While-Statements © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can.
COMP Loop Statements Yi Hong May 21, 2015.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
ECE Application Programming
ECE Application Programming
Chapter 4 – C Program Control
ECE Application Programming
ECE Application Programming
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
ECE Application Programming
ECE Application Programming
ECE Application Programming
CS1371 Introduction to Computing for Engineers
Chapter 4 - Program Control
Week 4 – Repetition Structures / Loops
Chapter 5: Loops and Files.
Chapter 2.2 Control Structures (Iteration)
Topics Introduction to Repetition Structures
Formatted and Unformatted Input/Output Functions
- Additional C Statements
Introduction to Programming
Chapter 4 - Program Control
Chapter 2.2 Control Structures (Iteration)
Control Structures Part 3
Looping III (do … while statement)
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Dale Roberts, Lecturer IUPUI
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Chapter 4 - Program Control
Based on slides created by Bjarne Stroustrup & Tony Gaddis
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
REPETITION Why Repetition?
Switch Case Structures
Presentation transcript:

16.216 ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 20: PE3 (continued)

ECE Application Programming: Lecture 20 Lecture outline Announcements/reminders Program 5 posted; due 3/26 Flowcharts due 3/21 Program 4 grading to be done today Today Review: break/continue statements Finish PE3 5/15/2018 ECE Application Programming: Lecture 20

Review: break/continue Exiting loop early: break Useful if exit condition can occur in middle of loop, e.g. do { scanf(“%lf”, &grd); if ((grd < 0.0) || (grd > 100.0)) break; // Implied “else” grdTotal = grdTotal + grd; grdCount = grdCount + 1; } while (1); Exiting current iteration early: continue Similar to break, but only ends current iteration When used with for loop, does execute variable change (e.g., i++ in for (i = 0; i < 10; i++)) 5/15/2018 ECE Application Programming: Lecture 20

ECE Application Programming: Lecture 20 Last time: PE3 program Prompt the user to enter a single input character followed by an integer, n. If not correctly formatted, print error, clear rest of line, and repeat prompt Depending on the character entered, do the following: ‘F’ or ‘f’: Compute and print the factorial of n, n! For example, if the user enters F 5, print 5! = 120 ‘P’ or ‘p’: Compute 2n, but only if n >= 0. For example, if the user enters p 2, print 2^2 = 4 Print an error if n < 0. ‘X’ or ‘x’: Exit the program In all other cases, print an error: For example: Invalid command Z entered If the user enters any command other than ‘X’ or ‘x’, return to the initial prompt and repeat the program. 5/15/2018 ECE Application Programming: Lecture 20

Flow charts: overall flow 5/15/2018 ECE Application Programming: Lecture 20

Discussion: Overall flow Whole program contains loop Repeats process until user enters ‘X’ or ‘x’ Use while or do-while—unknown # of iterations Since exit condition ends program, infinite loop Testing cmd: switch statement Checking equality of cmd to constant values Exiting program: return statement Use return at any point to end current function (including main) 5/15/2018 ECE Application Programming: Lecture 20

Code: overall flow (skeleton code) while (1) { // Loop repeats until user enters 'X' or 'x' /* Code to read cmd, n */ /* Evaluate cmd and perform appropriate operation */ switch (cmd) { case 'F': case 'f': /* Calculate n! */ break; case 'P': case 'p': /* Calculate 2^n, if n >= 0; print error otherwise */ case 'X': case 'x': return;// Exit program default: printf("Invalid command %c entered\n", cmd); } 5/15/2018 ECE Application Programming: Lecture 20

Flow charts: reading input 5/15/2018 ECE Application Programming: Lecture 20

Discussion: Reading input Loop that repeats as long as input incorrect Once input formatted correctly, exit  break Loop inside that one to handle reading of remainder of line Read character until you reach end of line Both while/do-while loops 5/15/2018 ECE Application Programming: Lecture 20

ECE Application Programming: Lecture 20 Code: Reading input do { printf("Enter command and integer: "); nVals = scanf("%c %d", &cmd, &n); // Correctly formatted input if (nVals == 3) break; // Otherwise, clear line scanf("%c", &junk); } while (junk != '\n'); } while (1); 5/15/2018 ECE Application Programming: Lecture 20

ECE Application Programming: Lecture 20 Today Write flowcharts for Computing n! Computing 2n if n >= 0 Complete code 5/15/2018 ECE Application Programming: Lecture 20

ECE Application Programming: Lecture 20 Similar to this exercise, Program 4 Errors do not cause program to exit Read expression If incorrect, print error and repeat prompt Operations An  A ^ n Note that n can be negative, 0, or positive Nth root of A  A R n or A r n Uses iterative methods Cannot use <math.h> 5/15/2018 ECE Application Programming: Lecture 20

ECE Application Programming: Lecture 20 Iterative methods Repeat calculation until correct value reached “Correctness” defined as: Difference between old, new value <= max error Max error == .000001 in Prog. 5 General process: newVal = <initial value>; do { oldVal = newVal; newVal = <equation based on oldVal>; } while (fabs(newVal – oldVal) > max_err); Remember, you can’t use <math.h>, so you’ll need your own way of computing absolute value (fabs()) 5/15/2018 ECE Application Programming: Lecture 20

ECE Application Programming: Lecture 20 Next time Functions 5/15/2018 ECE Application Programming: Lecture 20