PROBLEM SOLVING WITH LOOPS. Chapter 7

Similar documents
UNIT 3 PROBLEM SOLVING WITH LOOP AND CASE LOGIC STRUCTURE

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Repetition Algorithms

Control Structures II. Repetition (Loops)

Programming for Engineers Iteration

Lecture 7 Tao Wang 1

A Look Back at Arithmetic Operators: the Increment and Decrement

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

SNS COLLEGE OF ENGINEERING

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Increment and the While. Class 15


Introduction. C provides two styles of flow control:

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

Loops. Repeat after me

Lecture Transcript While and Do While Statements in C++

Unit 7. Lesson 7.1. Loop. For Next Statements. Introduction. Loop

Pseudocode. ARITHMETIC OPERATORS: In pseudocode arithmetic operators are used to perform arithmetic operations. These operators are listed below:

IT 1033: Fundamentals of Programming Loops

Looping Subtasks. We will examine some basic algorithms that use the while and if constructs. These subtasks include

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

Loops / Repetition Statements

Algorithms. Abdelghani Bellaachia, CSCI 1121 Page: 1

REPETITION CONTROL STRUCTURE LOGO

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Repetition. Chapter 6

Repetition. Chapter 6

LECTURE 5 Control Structures Part 2

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

CHAPTER 2 PROBLEM SOLVING TECHNIQUES. Mr Mohd Hatta Bin Hj Mohamed Ali Computer Programming BFC2042

Flow Charts. Visual Depiction of Logic Flow

Iteration Loops. The Do While Loop (Continued) Looping Subtasks

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures


Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

Chapter 3 Structured Program Development in C Part II

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Adding Integers. Unit 1 Lesson 6

Computer Programming

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

In this chapter you will learn:

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

Condition Controlled Loops. Introduction to Programming - Python

Add Subtract Multiply Divide

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

Computer Programming I - Unit 5 Lecture page 1 of 14

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Chapter 4: Control structures. Repetition

Score score < score < score < 65 Score < 50

Flowchart & Algorithm

Information Science 1

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Lecture 10. Daily Puzzle

University of Massachusetts Lowell

Understanding the problem

3 The L oop Control Structure

Java Coding 3. Over & over again!

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

Chapter 4: Control structures

An Introduction to Programming with C++ Sixth Edition. Chapter 7 The Repetition Structure

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable

Structured Program Development

Theory of control structures

Day06 A. Young W. Lim Mon. Young W. Lim Day06 A Mon 1 / 16

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Class 8 ALGORITHMS AND FLOWCHARTS. The City School

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

Loop Structures. Loop Structures. Algorithm to record 5 TV programmes. Recall Structured Programming..3 basic control structures.

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 4: Repetition Control Structure

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Repetition Structures Chapter 9

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

Control Statements. Musa M. Ameen Computer Engineering Dept.

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

COSC 122 Computer Fluency. Iteration and Arrays. Dr. Ramon Lawrence University of British Columbia Okanagan

Computer Programming: C++

CSCE150A. Introduction. While Loop. Compound Assignment. For Loop. Loop Design. Nested Loops. Do-While Loop. Programming Tips CSCE150A.

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

Relational operators (1)

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 5. Repetition in Programs. Notes. Notes. Notes. Lecture 05 - Loops

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Chapter 3 Structured Program Development

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Sudeshna Sarkar Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

CS112 Lecture: Repetition Statements

Object Oriented Programming with Java

fifth Solutions to Exercises 85

V3 1/3/2015. Programming in C. Example 1. Example Ch 05 A 1. What if we want to process three different pairs of integers?

Flow Control: Branches and loops

Week 6 Lesson 1: Control-Flow Statements (Continued)

Structured Programming. Dr. Mohamed Khedr Lecture 9

Information Science 1

Transcription:

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 many times. Ex. The Process of calculating the Total Payment for more than one employee. Loops can be written using combination of previous logic structures

Example: Write an Algorithm that calculate the average of 7 students grade.

Solution without looping structure Real St1_Grade, St2_Grade, St3_Grade, St4_Grade, St5_Grade, St6_Grade, St7_Grade, Control () 1. Read() 2. Average() 3. End Read() 1. Read St1_Grade 2. Read St2_Grade 3. Read St3_Grade 4. Read St4_Grade 5. Read St5_Grade 6. Read St6_Grade 7. Read St7_Grade 8. Exit Average() 1. Real Sum, Average 2. Sum = St1_Grade+St2_Grade+St3_Grade+ St4_Grade+St5_Grade+St6_Grade+ St7_Grade 3. Average = Sum/ 7 4. Exit

Solution with looping structure Real Student_Grade Control () 1. Read() 2. Average() 3. End Read() 1. Enter Student_Grade 2. Exit Average() 1. Integer Count=1 2. Real Sum=0 3. While ( count <= 7) 1. Read() 2. Sum=Sum +Student_Grade 3. Count=Count+1 4. WhileEnd 5. Real Average = Sum / count 6. Exit

Average Module Algorithm Flowchart What will Happen If we remove this? Count has no data, its value is Unknown, So the value of the condition is unpredictable. Exit F Real Average = Sum / 7 Flowchart Average() Integer Count=1 Real Sum=0 While Count<=7 T What will Happen If we remove this? Read() Sum=Sum+Student_Grade Count=Count+1 Sum has no initial value, its value is Unknown What will Happen If we remove this? This will lead to an infinite loop

The loop logic structure There are three types of loop structures: The While/WhileEnd loop Which repeats instructions while a condition is True and stops repeating when a condition arises that is not True. The Repeat/Until loop Which repeats instructions while a condition is False or until a condition is True. The automatic-counter loop a variable is set equal to a given number and increases in equal given increments until it is greater than an ending number.

The loop logic structure The algorithm and flowchart differ with each type of loop structure. Several standard types of tasks are accomplished through the use of the loop structure. Counting ( incrementing and decrementing) Accumulating ( calculating a sum or a total) In both task a number is added or subtracted from a variable and the result is stored back into the same variable. In each case the resultant variable is assigned the value of zero before starting the loop( initializing the variable ).

Initialization The Initialization set to an initial value usually zero but not all the time Examples: Count = 0 Count = 1 Count = 100

Incrementing (or Decrement) Is done by adding a constant, such as 1 or 2 to the value of a variable Example : Counter = counter + 1 or c = c + 1 Note: Remember Variable Must be Initialized before starting the loop. The increment can be one, two, or any other constant, including negative number if you want to decrement rather than increment. Example: Counter = counter -1 or c = c - 1

The Accumulating Or summing, a group of numbers Similar to incrementing, except a variable instead of a constant is added to another variable. sum = sum + variable or s = s + v Examples: Totalsales = Totalsales + Sales Note: Remember Variable Must be Initialized to zero.

While/ While End Loop. Repeats the instructions between the While & While End, if the condition is true. While <Condition (s)> Instruction Instruction... WhileEnd

while/whileend Use the While/ While End Loop structure when you do not know the number of times an instruction to be repeated. Or if there are cases when the instructions in the loop should not be processed. Primer read The value must be entered before the loop start. It gives the while/whileend loop a valid value for the variable in order for the conditions to be true the first time through the loop. The value of variable that allow to control when to stop the looping process called a trip value.

Example 1: Create the algorithm to find the average age of all the students in class How many Students? UNKOWN How can I solve this problem?

Average Age of a Class While/WhileEnd

Repeat/Until Repeat Instruction Instruction... Until< Condition(S)>

Example 1: Create the algorithm to find the average age of all the students in class How many Students? UNKOWN How can I solve this problem?

Average Age of a Class Repeat/Until

While/EndWhile & Repeat/Until While/WhileEnd You must initialize the data so that the resultant of the condition is true the first time through the loop. Otherwise, the loop instruction will never be processed. Repeat the loop until the resultant of condition is false. The condition is processed at the beginning Repeat/Until You can set the operand of the conditions anywhere within the loop since the condition is processed at the end. Repeat the loop until the resultant of condition is true. The condition is processed at the end Instructions in the loop are processed entirely at least once.

Automatic-Counter Loop Increments or decrements a variable each time the loop is repeated. Use it when you know from the start the number of times the loop will be executed. The beginning value, the ending value, and the increment value may be constant, variable, or expressions.

Automatic-Counter Loop Loop: counter=begin To End Step S Variable name Instruction Instruction Instruction Loop End: Counter..

Automatic-Counter Loop: General Rules When the computer executes the Loop instruction its sets the counter equal to the beginning number. When the computer executes the Loop-End instruction it increments the counter. When the counter is less than or equal to the ending number The processing continues for the instructions that follows the loop. When the counter is greater than the ending number. The processing continues for the instructions that follows the loop-end instruction.

Automatic-Counter Loop When decrementing the counter : The counter is decremented at the end of the loop. When the counter is greater than or equal to the ending value, the loop continues. Step value needs to be a negative number and begin must be greater than end

Example 1: using Automatic-Counter Loop Create the algorithm to find the average age of all the students in class

2 3 4 5 J, ( J -1 ) ( J 1) J,

Example 2: Create the algorithm & flowchart to: Prints the even numbers. The series of numbers will: Start From 0. Ends at 50.

Example 2 Solution PrintEvenNum() 1. Loop: counter=0 To 50 Step 2 1. Print counter 2. Loop-End: counter 3. End Algorithm

Recursion Another type of loop structure. A module or a function calls itself. Example : FactorialFunction (N) 1. If N > 1 then Factorial = N * FactorialFunction (N-1) Else Factorial = 1 2. Exit