CHAPTER #7 Problem Solving with Loop. Overview Loop logical structure Incrementing Accumulating WHILE/WHILE-END FOR Nested loop Pointer Algorithmic instruction.

Slides:



Advertisements
Similar presentations
Programming with Microsoft Visual Basic 2008 Fourth Edition
Advertisements

Programming with Microsoft Visual Basic th Edition
Repetition Control Structures
Repetition control structures
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture two Dr. Hamdy M. Mousa.
Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.
UNIT 3 PROBLEM SOLVING WITH LOOP AND CASE LOGIC STRUCTURE
CS0004: Introduction to Programming Repetition – Do Loops.
Repeating Actions While and For Loops
Flowcharts Remember that a solution to a problem is called an algorithm. Algorithms are often a series of steps required to solve the problem. A flowchart.
An Introduction to Programming with C++ Fifth Edition
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand basic loop concepts: ■ pretest loops and post-test loops ■ loop.
Chapter 7 Problem Solving with Loops
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 3P. 1Winter Quarter Structured Engineering.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this.
CIS Computer Programming Logic
Chapter 5: Control Structures II (Repetition)
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Chapter 12: How Long Can This Go On?
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Describe the Program Development Cycle. Program Development Cycle The program development cycle is a series of steps programmers use to build computer.
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Six The Do Loop and List Boxes.
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.
Visual Basic Programming
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
1 Objectives ❏ To understand basic loop concepts: ■ pretest loops and post-test loops ■ loop initialization and updating ■ event and counter controlled.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 3P. 1Winter Quarter Structured Engineering Problem Solving and Logic.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 7 Problem Solving with Loops
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
Chapter 4 – C Program Control
REPETITION CONTROL STRUCTURE
Chapter 2- Visual Basic Schneider
Programming Logic and Design Fourth Edition, Comprehensive
CHAPTER 5A Loop Structure
Chapter 5: Control Structure
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Chapter 5: Repetition Structures
( Iteration / Repetition / Looping )
Topics Introduction to Repetition Structures
Repeating Program Instructions
Lecture 4B More Repetition Richard Gesick
Control Structure Senior Lecturer
Chapter 6 Repetition Objectives ❏ To understand basic loop concepts:
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Topics discussed in this section:
Chapter 2- Visual Basic Schneider
CISC101 Reminders All assignments are now posted.
Topics Introduction to Repetition Structures
The structure of programming
Thinking procedurally
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

CHAPTER #7 Problem Solving with Loop

Overview Loop logical structure Incrementing Accumulating WHILE/WHILE-END FOR Nested loop Pointer Algorithmic instruction and symbol Recursion

Objectives Develop problems using the loop logic structure Use the problem-solving tools when developing a solution using the loop logic structure Use counters and accumulators in a problem solution Use nested loop instructions Distinguish the different uses of three types of loop logic structures. Use recursion in a simple problem.

Background The loop logic structure is the repeating structure Some ‘process’ is repeated several times (known) or until a condition is reached (unknown) for different sets of data So this structure is extremely important

Example(s) Emptying a pool with small pail Cut a cucumber into small piece Sharpening a pencil Push up 20 times Etc...

Increment The task of incrementing, or counting, as we said, is done by adding a constant, such as 1 or 2, to the value of a variable To write the instruction to increment a variable, you use an assignment statement For example, Counter = Counter + 1 when counting by ones

What’s happen? No of iteration Expression/Statemen t New Value 0 (init)Counter = 1 (example, its called initialization) 1Counter = Counter + 1Counter = = 2 2Counter = Counter + 1Counter = = 3 3Counter = Counter + 1Counter = = 4 etc Red written indicates old value

Accumulating The process of accumulating is similar to incrementing, except a variable instead of a constant is added to another variable, which holds the value of the sum or total The instruction for accumulating is the following: Sum = Sum + Variable

What’s happen? No of iteratio n Old value of sum Example of ‘Value’ Sum = Sum + Value 1Sum = 0Value = 1Sum = = 1 2 Sum = 1 Value = 2Sum = = 3 3 Sum = 3 Value = 3Sum = = 6 4 Sum = 6 Value = 4Sum = = 10 etc Red written indicates old value Initialization : Sum = 0

Product Calculating the product of a series of numbers is similar to finding the sum of a series of numbers with two exceptions First, the plus sign is replaced by the multiplication sign (*) Second, the product variable must be initialized to 1 instead of 0. For example Product = Product * Number

What’s happen? No of iteratio n Old value of Product Example of ‘Number’ Product = Product * Number 1Product = 1Number = 1Product = 1 * 1 = 1 2 Product = 1 Number = 2Product = 1 * 2 = 2 3 Product = 2 Number = 3Product = 2 * 3 = 6 4 Product = 6 Number = 4Product = 6 * 4 = 24 etc Red written indicates old value Initialization : Product = 1

Loop Logic Structure

Types of Loop Structures While/While End For/End For Repeat/Until

Case study Develop a problem-solving solution for counting an average for 10 numbers.

While/WhileEnd Pretest Looping (While Loop) 0-15

Figure 7.1 Flowchart Diagram of While/WhileEnd 0-16

Algorithm #1  using WHILE Set Sum = 0 Set count = 0 While (count < 10) –Enter Number –Sum = Sum + Number –Count = Count + 1 End While //finish when count = 10 or more (>= 10) Average = Sum / Count //count = 10 Display Average

Automatic Counter Loop Format Pretest Looping (For / Counter Loop)

Flowchart of Automatic- Counter Loop

Algorithm #2  using FOR Set Sum = 0 For count = 1 to 10 –Enter Number –Sum = Sum + Number End For //finish for (10 – 1 + 1) repetitions Average = Sum / Count //count = 10 Display Average

Repeat/Until Algorithm Format Posttest Looping (Do-While Loop)

Flowchart Diagram of Repeat/Until

Algorithm #3  using REPEAT/UNTIL Set Sum = 0 Set count = 0 Repeat –Enter Number –Sum = Sum + Number –Count = Count + 1 Until (count >= 10) //finish when count = 10 or more Average = Sum / Count //count = 10 Display Average

Exercise Develop a problem-solving solution for counting an average for determined numbers (entered by user).

Nested Loops

Exercise – Nested Loop Write a problem-solving solution for a problem that ask user for an input (called n), then display a ‘triangle’ with this format * * * * * * *... (depends on n)

Review Mention about problem solving that involves three logic structures

Indicators Logical variables that a programmer sets within a program to change the processing path or to control when the processing of a loop should end  Therefore you will get unstopped loop The user has no knowledge of these indicators during processing

Recursion Recursion occurs when a module or a function calls itself The condition that ends the loop must be within the module Recursive procedures can be replaced by conventional loop structures; however, recursive procedures are usually faster

When we using recursion? Recursion is difficult to use in a solution However, it is needed more and more in today’s programming A recursive technique is used in finding the power of a number, sorting large amounts of data, and selecting the correct form from many report forms, and find factorial for a number

Assignment Write a problem-solving solution to solve a problem that takes an integer value and returns the number with its reversed digits. For example, given the number 7631, the solution should return 1367 Hint : Use the loop structure and modulo operation %