© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this.

Slides:



Advertisements
Similar presentations
Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Published by Addison-Wesley.
Advertisements

Chapter 4 Loops Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X.
© 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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
11.3 Function Prototypes A Function Prototype contains the function’s return type, name and parameter list Writing the function prototype is “declaring”
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 6: Loop Control Structures.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
Chapter 4: Control Structures II
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Wage Calculator Application: Introducing.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Chapter 4: Loops and Files
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Loops 1. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved while Loop Flow.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Chapter 4: Control Structures II
Control Structures Repetition or Iteration or Looping Part II.
Chapter 5: Control Structures II
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing.
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 4 Slide #1.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
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.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
General Condition Loop A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Introduction to Computer Programming
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Repetition-Counter control Loop
Chapter 5: Control Structures II
Java Programming Loops
Presentation transcript:

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this time you have learned four looping constructs (also called repetition statements). Each of these four loops are ideal to use in different situations. The while Loop The while loop is a pre-test loop. It is ideal in situations where you do not want the loop to iterate if the condition is false from the beginning. It is also ideal if you want to use a sentinel. The while-break Loop The while-break loop is a mid-test loop. It is ideal in situations where you want to test an exit condition within the loop. The do-while Loop The do-while loop is a post test loop. It is ideal in situations where you always want the loop to iterate at least once. The for Loop The for loop is a pre-test loop that first executes an initialization expression. In addition, it automatically executes an update expression at the end of each iteration. It is ideal for situations where a counter variable is needed. The for loop is primarily used when the exact number of required iterations is known. Source: Section 5.8 “Focus on Software Engineering” Starting out with C++ Alternate 2 nd Edition, by Tony Gaddis

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8.2Essentials of Counter-Controlled Repetition For each question write down these 3 elements of counter- controlled repetition –Data type, name, and initial value of the control variable int counter = 2; –The condition that tests for the final value of the control variable counter <= 5; –The increment (or decrement) by which the control variable is modified during each iteration of the loop. counter++ ;

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6.2 while Repetition Statement In Section 6.2 we found the first power of 3 greater than 50 Identify the key elements of repetition for this example. Rewrite the example using a do…while repetition statement. Rewrite the example using a for repetition statement.

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6.5 do…while Repetition Statement (Cont.) In Section 6.5 we taught C++ how to count to 3, using a do…while repetition statement. Identify the key elements of repetition for this example. Rewrite the example using a while repetition statement. Rewrite the example using a for repetition statement.

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8.4Examples Using the for Statement In Section 8.4 we stepped the control variable i from 7 to 77 in increments of 7. Identify the key elements of repetition for this example. Rewrite the example using a while repetition statement. Rewrite the example using a do…while repetition statement.

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Factorial In mathematics, the factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example, 0! is a special case that is explicitly defined to be 1. Here is the pseudo-code for one possible solution. Define variable f of type integer and initialize to 1 Prompt user for a number n Using a repetition statement calculate the factorial Display the message “factorial = ” followed by the factorial. Identify the key elements of repetition for this example. Write your program using a while repetition statement. Rewrite your program using a do…while repetition statement. How did you handle the 0! case? Rewrite your program using a for repetition statement.

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Sum of Numbers Write a program that asks the user for a counting number. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 9, the loop will find the sum of , Input Validation: Do not accept a zero or negative starting number. Design Challenge: Can you discover a way to rewrite the program to run faster? Hint: 0+9=9, 1+8=9, …4+5=9 Source: Chapter 5 “Looping” Programming Challenges from Starting out with C++ Alternate 2 nd Edition, by Tony Gaddis

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Distance Traveled The distance a vehicle travels can be calculated as follows: distance = speed * time For example, if a train travels 40 mph for 3 hours, the distance traveled is 120 miles. Write a program that asks the user for the speed of a vehicle (in miles-per-hour) and how many hours it has traveled. It should then use a loop to display the distance the vehicle has traveled for each hour of that time period. Here is an example of the output: What is the speed of the vehicle in mph? 40 How many hours has it traveled? 3 Hour Distance Traveled Input Validation: Do not accept a negative number for speed and do not accept any value less than one for time traveled. Source: Chapter 5 “Looping” Programming Challenges from Starting out with C++ Alternate 2 nd Edition, by Tony Gaddis

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Pennies For Pay Write a program that calculates how much a person would earn over a period of time if his salary is one penny the first day, two pennies the second day, and continued to double each day. The program should ask the user for the number of days. Display a table showing how much the salary was for each day, and then show the total pay at the end of the period. The output should be displayed in a dollar amount, not the number of pennies. Input Validation: Do not accept a number less than one for the number of days worked Source: Chapter 5 “Looping” Programming Challenges from Starting out with C++ Alternate 2 nd Edition, by Tony Gaddis

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Fibonacci Number Consider the following sequence of numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, …. Given the first two numbers of the sequence (say a 1 and a 2 ), the nth number a n, n >= 3, of this sequence is given by a n = a n-1 + a n-2 Thus a 3 = a 2 + a 1 = = 2, a 4 = a 3 + a 2 = = 3,and so on… Such a sequence is called a Fibonacchi sequence. Write a program that given any first two numbers, using this algorithm, determine the nth number an, n <= 3, of the sequence. Sample Run Enter the first two Fibonacci numbers -> The first two Fibonacci numbers are 12 and 16 Enter the desired Fibonacci number to be determined -> 10 The 10 th Fibonacci number is 796. Source: Chapter 5 “Control Stucture II” C++ Programming, by D.S. Malik

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. More Programming Exercises 1.An integer is divisible by 9 if the sum of its digits is divisible by 9. Write a program that prompts the user to input an integer. The program should then output the number and a message string stating whether the number is divisible by 9. It does so by first adding the digits and then checking whether the sum of the digits is divisible by 9. 2.Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example, it should output the individual digits of 3456 as ; output the individual digits of 8030 as ; output the individual digits of as ; output the individual digits of 4000 as ; and output the individual digits of-2345 as Write a program that prompts the user to input an integer and then outputs the number with the digits reversed. For example, if the input is 12345, the output should be Your program must also output 5000 as 0005 and 980 as Write a program that reads a set of integers, and then finds and prints the sum of the even and odd integers. 5.Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is divisible by 2. An odd integer is prime if it is not divisible by any odd integer less than or equal to the square root of the number.) Source: Chapter 5 “Control Stucture II” C++ Programming, by D.S. Malik