Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.

Slides:



Advertisements
Similar presentations
Programming Logic and Design Eighth Edition
Advertisements

Repetition control structures
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Chapter 04 (Part III) Control Statements: Part I.
Understanding the Three Basic Structures
Subject: Information Technology Grade: 10
CS0004: Introduction to Programming Repetition – Do Loops.
Repeating Actions While and For Loops
An Introduction to Programming with C++ Fifth Edition
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Adapted from slides by Marie desJardins
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this.
Algorithmic Problem Solving CMSC 201 Adapted from slides by Marie desJardins (Spring 2015 Prof Chang version)
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
Programming Logic and Design Fifth Edition, Comprehensive
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 2: Flowcharts.
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
Programming Logic and Design Sixth Edition Chapter 5 Looping.
ANALYSIS AND ALGORITHM DESIGN - IV. Repeat statements/looping/counting/iterations It is often necessary to repeat certain parts of a program a number.
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
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.
ITEC113 Algorithms and Programming Techniques
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Chapter 1 Pseudocode & Flowcharts
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
(C)opyright 2000 Scott/Jones Publishers Introduction to Flowcharting.
Counting Loops.
Programming Logic and Design, Introductory, Fourth Edition1 Understanding the Three Basic Structures Structure: a basic unit of programming logic Any program.
Chapter 7 Problem Solving with Loops
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Trace Tables In today’s lesson we will look at:
Program design Program Design Process has 2 phases:
REPETITION CONTROL STRUCTURE
while Repetition Structure
Lecture 7: Repeating a Known Number of Times
CHAPTER 5A Loop Structure
Think What will be the output?
Chapter 5: Control Structure
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
( Iteration / Repetition / Looping )
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Chapter 8 The Loops By: Mr. Baha Hanene.
Understanding the Three Basic Structures
Chapter (3) - Looping Questions.
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
3 Control Statements:.
Chapter 8: More on the Repetition Structure
3.1 Iteration Loops For … To … Next 18/01/2019.
Computer Science Core Concepts
Let’s all Repeat Together
CHAPTER 4 Iterative Structure.
EPSII 59:006 Spring 2004.
REPETITION Why Repetition?
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Topic: Control Statements

Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and calculated the gross salary by adding the basic salary and the allowance amount. The program should output the gross salary for the employee. Requirements: List the operations List variable Write Pseudocode Draw flowchart

Subtopic: Loop Control Statements

Loops (or Iteration or Repetition) The solution to many problems requires performing some of the instructions more than once. Example: Calculate the gross pay for a number of employees. In this example, each employee’s gross pay is calculated using the same method. Hence the same instructions that are used for the first employee can be repeated for all the employee. A loop is used to repeatedly perform an operation or a block of code through the use of a conditional expression.

Types of Loops For Loop While Loop Repeat Until Loop

Cases in which types of Loops are used: A loop that involves repeating the instructions a predetermined (known) number of times. The loop constructs that can be use in this case are the for and the while construct. In this case a counter must be used to control the number of times the loop is repeated. A loop that involves repeating the instructions for an unknown number of times. The loop constructs that can be use in this case are the while and the repeat construct.

For Loop Construct Number of times loop is to be repeated is predetermined. Pseudocode For variable = beginning to ending do Instructions to be repeated Endfor

For variable = beginning to ending do “beginning to ending” - represents the range Example: For number = 1 to 10 do 1 to 10 is the range This one line is used to: Initialize the variable to the first value in the range Test the variable with the values in the range Increment the variable until the test is false

Flowchart Initialize variable Test variable Increment variable Instructions Yes No

While Loop Construct Number of times that the loop is to be repeated is predetermined. Pseudocode Initialize variable to be used in condition While condition do Instructions to be repeated Increment the variable used in the condition Endwhile Note: The condition is where the variable is tested

Flowchart Initialize variable Test variable Increment variable Instructions Yes Note: When the number of times that the loop will be repeated is predetermined, the flowcharts are the same with for loop and while loop. No

While Loop Construct Number of times that the loop is to be repeated is unknown. Pseudocode Get value for the variable to be used in condition While condition do Instructions to be repeated Get value for the variable to be used in condition Endwhile

Flowchart Get value for variable Test variable Get value for variable Instructions Yes No

Problem 1 Write a program that accepts the basic salary and allowance amount for ten (10) employees and calculate the gross salary by adding the basic salary and the allowance amount. The program should output the gross salary for the employees. Requirements: a) State the control structure to be used. b) Write the pseudocode c) Draw the flowchart

a) State the control structure to be used Answer Loop control structure - for loop or while loop Reason: The number of employees is known, as a result the number of times the loop is to be repeated is predetermined.

b) Write the Pseudocode Answer if the for loop is used Start Declare basic, allowance, gross as Real count as Integer Print “A program that calculates the gross salary amount for ten employees” for count = 1 to 10 do Print “Enter basic salary” Read basic Print “Enter allowance” Read allowance gross = basic + allowance Print “Gross salary is ”, gross endfor Stop Answer if the while loop is used Start Declare basic, allowance, gross as Real count as Integer Print “A program that calculates the gross salary amount for ten employees” count = 1 while count < = 10 do Print “Enter basic salary” Read basic Print “Enter allowance” Read allowance gross = basic + allowance Print “Gross salary is ”, gross count = count + 1 endwhile Stop

c) Draw the flowchart count = 1 START STOP gross = basic + allowance count = count + 1 count <= 10 Print “Enter basic salary and allowance” Read basic, allowance Print gross No Yes

Problem 2 Write a program that accepts the basic salary and allowance amount for employees and calculate the gross salary by adding the basic salary and the allowance amount. Output the gross salary for the employees. The program should be terminated by zero (0). Requirements: State the control structure to be used. Write the pseudocode Draw the flowchart

a) State the control structure to be used Answer Loop control structure - while loop or repeat until loop Reason: The number of employees is unknown, as a result the number of times the loop is to be repeated is not known.

b) Write the Pseudocode Start Declare basic, allowance, gross as Real Print “A program that calculates the gross salary amount for employees” Print “Enter basic salary” Read basic while basic 0 do Print “Enter allowance” Read allowance gross = basic + allowance Print “Gross salary is ”, gross Print “Enter basic salary” Read basic endwhile Stop

c) Draw the flowchart START STOP gross = basic + allowance basic 0 Print “Enter allowance” Read allowance Print gross No Yes Read basic Print “Enter basic salary” Read basic Print “Enter basic salary”

Homework