Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':

Slides:



Advertisements
Similar presentations
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
Advertisements

Introduction to C Programming
Introduction to C Programming
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
CS0007: Introduction to Computer Programming
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Computer programming Lecture 3. Lecture 3: Outline Program Looping [Kochan – chap.5] –The for Statement –Relational Operators –Nested for Loops –Increment.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
True BASIC Ch. 6 Practice Questions. What is the output? PRINT X LET X = -1 PRINT X FOR X = 4 TO 5 STEP 2 PRINT X NEXT X PRINT X END.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
CS150 Introduction to Computer Science 1
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
Chapter 5 Repetition and Loop Statements Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Iterations Very Useful: Ability to repeat a block of code Example:
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Overview Go over parts of quiz? Another iteration structure for loop.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Chapter 5: Repetition and Loop Statements By: Suraya Alias.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
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.
CISC105 – General Computer Science Class 4 – 06/14/2006.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter 5 Repetition and Loop Statements. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.5-2 Figure 5.1 Flow Diagram of Loop Choice Process.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
CHAPTER 6: REPETITION AND LOOP STATEMENTS Learning outcomes  Define the concept of repetition structure.  Specify.
Repetition statements
CHAPTER 4 DECISIONS & LOOPS
Chapter 4 – C Program Control
CHAPTER 6: REPETITION AND LOOP STATEMENTS
Chapter 5: Loops and Files.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Beginning C Lecture 4 Lecturer: Dr. Zhao Qinpei
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Introduction to Programming
Unary Operators ++ and --
Chapter 5: Repetition and Loop Statements
Repetition and Loop Statements
Lec 6.
IPC144 Introduction to Programming Using C Week 3 – Lesson 1
CS150 Introduction to Computer Science 1
Computer programming Lecture 3.
Program Flow.
Repetition Statements (Loops) - 2
Lec 6 Loop Statements Introduction to Computer Programming
ICS103: Programming in C 5: Repetition and Loop Statements
Programming Fundamental
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Presentation transcript:

Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k': printf("The prefix is equal to 1E3.\n"); break; case 'm': printf("The prefix is equal to 1E-3.\n"); break; case 'u': printf("The prefix is equal to 1E-6.\n"); break; case 'n': printf("The prefix is equal to 1E-9.\n"); break; default: printf("Error! You must enter a valid amount.\n"); }

UNIT 5 ET156 Introduction to C Programming

Sample Conditions Table 4-2

Figure 5.1 Flow Diagram of Loop Choice Process

Types of Loops Table 5.1

Counter-Controlled Loops Used when you know the number of times the loop must execute General steps: 1. Set loop control variable to 0 2. Loop control variable < final value Execute statements Increase loop control variable by 1 For loop is best choice, but while loop will work!

Compound Assignment Operators Increment: i = i + 1; i += 1; ++I; i++; Decrement: i = i -1; i -= 1; --I; i--;

Figure 5.6 Comparison of Prefix and Postfix Increments

while Loop Style while (condition is true) { Statements to be repeated; Counter updated; } while (count <= 10){ Statements to be repeated; count++; }

Sum & Average with a while Loop count=0, sum=0; while (count < 10){ printf(Enter score: ); scanf(%d, &quiz); sum = sum + quiz; count++; } ave = sum / count;

Perform a known number actions with a while Loop product=0; multiplier=5; multiplicand=3; ctr=0; while (ctr < multiplier){ product += multiplicand; count++; }

for Loop Style for ( counter start status; condition is true; update counter; ) { Statements to be repeated; } for (i=0 ;i <= 10; i++){ Statements to be repeated; }

Sum & Average with a for Loop sum=0; for (i=0; i < 10; i++;){ printf(Enter score: ); scanf(%d, &quiz); sum = sum + quiz; } ave = sum / count;

Perform a known number actions with a for Loop product=0; multiplier=5; multiplicand=3; for (i=0; i < multiplier; i++;){ product += multiplicand; }

Off by One Errors This loop executes n+1 times: for (count = 0; count <= n; ++count) sum += count; Always test at the loop boundaries to verify the loop does the right thing.

Debugger Debugger allows: Single-step execution Setting breakpoints on a statement Variable inspections

Diagnostic Calls Use printf to output intermediate results. Define a constant named DEBUG and use a conditional: #define DEBUG 1 if (DEBUG) printf("*** score is %d, sum is %d\n", score, sum);

Common Errors Forgetting to use curly braces around multiple steps Termination condition never met (infinite loop) Mistyping an equality operator (==) as an assignment operator (=) Confusing do-while and while Mistakes related to operator precedence Avoid increment, decrement, and compound assignment operators in complex expressions. Use parentheses to control evaluation order.