Preview

Counter Controlled Loops and While Loops

Satisfactory Essays
Open Document
Open Document
391 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Counter Controlled Loops and While Loops
08/12/2014
Unit 8 Research Assignment 1: Exploring the Difference between Using Count-Controlled Loops and While Loops Count-controlled: control variable (or loop counter) initial value of the control variable increment (or decrement) by which the control variable is modified each iteration through the loop condition that tests for the final value of the control variable
A count-controlled repetition will exit after running a certain number of times. The count is kept in a variable called an index or counter. When the index reaches a certain value (the loop bound) the loop will end.
Count-controlled repetition is often called definite repetition because the number of repetitions is known before the loop begins executing. When we do not know in advance the number of times we want to execute a statement, we cannot use count-controlled repetition. In such an instance, we would use sentinel-controlled repetition.
(http://www.cs.iit.edu/~cs561/cs115/looping/count.html)
While Loop:
The while loop is used to repeat a section of code an unknown number of times until a specific condition is met. For example, say we want to know how many times a given number can be divided by 2 before it is less than or equal to 1. If we know a specific number, such as 32, we can say 5 times, but for a given symbolic variable "NUMBER" which represents any number in the world, how many times is not known a priori (before hand). In this case, we could use a while loop to determine that answer:
The "pseudocode" for such an algorithm is: while the number is bigger than one keep dividing it by two. Additionally keep a count of how many times we do the division.
Why While Loops: Like all loops, "while loops" execute blocks of code over and over again.
The advantage to a while loop is that it will go (repeat) as often as necessary to accomplish its goal. while ( condition is true ) do something % Note: the "something" should eventually result

You May Also Find These Documents Helpful

  • Satisfactory Essays

    cout << "You are in factorial program of C++. Please enter a +ve integer:- ";…

    • 426 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Nt1330 Unit 2 Assignment 1

    • 3740 Words
    • 15 Pages

    1. A while loop will not execute the body of the code if you have your test condition incorrect.…

    • 3740 Words
    • 15 Pages
    Satisfactory Essays
  • Satisfactory Essays

    3. When a module is executing, what happens when the end of the module is reached?…

    • 765 Words
    • 5 Pages
    Satisfactory Essays
  • Satisfactory Essays

    PT1420 Unit 8 Lab 8

    • 365 Words
    • 2 Pages

    Calculating a Running Total, page 201): Asks for input of a number 5 times then adds all numbers into one total sum, example 2,4,6,8,10 = 30…

    • 365 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Homework Unit 3

    • 354 Words
    • 2 Pages

    1. Statements are execute in order according to the program code. Complex programs can need the help of a decision structure. Statements have to be true to be executed.…

    • 354 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    unit 7 assignment 1

    • 431 Words
    • 2 Pages

    5. What three actions do count-controlled loops typically perform using the counter variable? Initialization: Before the loop begins, the counter variable is initialized to a starting value. Test : The loop tests the counter variable by comparing it to a maximum value. If the counter variable is less than or equal to the maximum, the loop iterates. If the counter is greater than the maximum value, the program exits the loop. Increment : To increment a variable means to increase its value. During each iteration, the loop increments the counter variable by adding a predetermined amount to it…

    • 431 Words
    • 2 Pages
    Satisfactory Essays
  • Powerful Essays

    Unit 3 network discovery

    • 1031 Words
    • 5 Pages

    The shell tries to find an executable program with the same name as the command. When it does, the shell executes the program. When finishing the command put an & ampersand symbol at the end of the command and the command will run in the background. Then a cursor will be displayed immediately to allow the next command to be entered.…

    • 1031 Words
    • 5 Pages
    Powerful Essays
  • Satisfactory Essays

    Unit 7 Assignment 1

    • 261 Words
    • 2 Pages

    2. A pretest loop is a loop tests the conditions before performing the iteration. A posttest loop performs the iteration then test the condition.…

    • 261 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Unit 7 Programming

    • 334 Words
    • 3 Pages

    2. A pretest loop tests its condition before performing an iteration; while a post-test loop tests its condition after an iteration.…

    • 334 Words
    • 3 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Control Flow Diagram—Main Control [pic] Control Flow Diagram—Display Menu | | |[pic] | Control Flow Diagram—Get_Int_Value | | |[pic] | Control Flow Diagram—Convert Currency | | |[pic] | Control Flow Diagram—Display_Results |[pic] | Pseudocode Example Main Module Declare currencyType as Integer Declare internationalValue as real Declare USValue as real currencyType = 1 \ Comment: set currencyType to some number less than 6 \ to drop into the Do While loop below Do While currencyType < 6 Call Display Menu \Comment: if the user selects 6 in the Display Menu \ module then the user wants to Quit. If currencyType >= 1 and currencyType 6 \ to drop into the Do While loop) Do While (currencyType < 1 OR currencyType > 6) Display "Currency conversion program" Display "Please make a selection" Display "1: Canadian Dollars" Display "2: Mexican Pesos" Display "3: English Pounds" Display "4: Japanese Yen"…

    • 389 Words
    • 2 Pages
    Satisfactory Essays
  • Good Essays

    Imp 2 Pow: Kick It

    • 591 Words
    • 3 Pages

    This pattern is a little bit more tricky, but i noticed that its just like the threes it goes up three every number but instead of zero being the starting number it is five.…

    • 591 Words
    • 3 Pages
    Good Essays
  • Better Essays

    Mat 126 Week 1 Assignment

    • 1084 Words
    • 5 Pages

    Here you can see that the 2 is multiplied by 5, which is 10. Then the 10 is also multiplied by 5, which is 50 and so on.…

    • 1084 Words
    • 5 Pages
    Better Essays
  • Good Essays

    Paperose Lab Activity

    • 863 Words
    • 4 Pages

    b. The timer will tell the enzyme when to begin and will tell the announcer when they have reached each time interval (10 seconds, 30 seconds, 60 seconds, 120 seconds, 180 seconds, and 360 seconds). The time intervals are cumulative. The enzyme should keep ripping and counting for the entire 360 seconds. If the enzyme runs out of paperose molecules, none should be added to the container.…

    • 863 Words
    • 4 Pages
    Good Essays
  • Satisfactory Essays

    8. How many times can this process be repeated? This process will continue to repeat it’s self until the enzyme becomes inhibited, denatured, or wears out.…

    • 318 Words
    • 2 Pages
    Satisfactory Essays
  • Good Essays

    The Real Number System

    • 1751 Words
    • 8 Pages

    * The use of three dots at the end of the list is a common mathematical notation to indicate that the list keeps going forever.…

    • 1751 Words
    • 8 Pages
    Good Essays

Related Topics