Control Structure: Loop (Part 1) Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop.

Slides:



Advertisements
Similar presentations
Topic Reviews For Unit ET156 – Introduction to C Programming Topic Reviews For Unit
Advertisements

TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Copyright © Cengage Learning. All rights reserved.
4 Control Statements: Part 1.
Bellwork If you roll a die, what is the probability that you roll a 2 or an odd number? P(2 or odd) 2. Is this an example of mutually exclusive, overlapping,
Chapter 5: Control Structures II (Repetition)
Chapter 15 Functions and graphing Bjarne Stroustrup
Chapter 1 The Study of Body Function Image PowerPoint
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
By D. Fisher Geometric Transformations. Reflection, Rotation, or Translation 1.
What two numbers will give you a product of 64 and a quotient of 4?
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
Coordinate Plane Practice The following presentation provides practice in two skillsThe following presentation provides practice in two skills –Graphing.
What two numbers will give you a product of 64 and a quotient of 4?
0 - 0.
ALGEBRAIC EXPRESSIONS
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
MULTIPLYING MONOMIALS TIMES POLYNOMIALS (DISTRIBUTIVE PROPERTY)
ADDING INTEGERS 1. POS. + POS. = POS. 2. NEG. + NEG. = NEG. 3. POS. + NEG. OR NEG. + POS. SUBTRACT TAKE SIGN OF BIGGER ABSOLUTE VALUE.
MULTIPLICATION EQUATIONS 1. SOLVE FOR X 3. WHAT EVER YOU DO TO ONE SIDE YOU HAVE TO DO TO THE OTHER 2. DIVIDE BY THE NUMBER IN FRONT OF THE VARIABLE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
Around the World AdditionSubtraction MultiplicationDivision AdditionSubtraction MultiplicationDivision.
Computer Science Department Introduction To Computers and Programming Knowledge: Understand the concepts of computer, hardware, software and programming.
Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.
O X Click on Number next to person for a question.
© S Haughton more than 3?
5.9 + = 10 a)3.6 b)4.1 c)5.3 Question 1: Good Answer!! Well Done!! = 10 Question 1:
1 Chapter Eleven Arrays. 2 A Motivating Example main( ) { int n0, n1, n2, n3, n4; scanf(“%d”, &n0); scanf(“%d”, &n1); scanf(“%d”, &n2); scanf(“%d”, &n3);
Twenty Questions Subject: Twenty Questions
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
© 2012 National Heart Foundation of Australia. Slide 2.
Adding Up In Chunks.
Past Tense Probe. Past Tense Probe Past Tense Probe – Practice 1.
1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.
Control Structures Selections Repetitions/iterations
 .
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Addition 1’s to 20.
CS 240 Computer Programming 1
25 seconds left…...
Test B, 100 Subtraction Facts
Week 1.
Complexity Analysis (Part II)
Types of selection structures
We will resume in: 25 Minutes.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Chapter 15 Functions and graphing John Keyser’s Modification of Slides by Bjarne Stroustrup
Bottoms Up Factoring. Start with the X-box 3-9 Product Sum
1 Unit 1 Kinematics Chapter 1 Day
O X Click on Number next to person for a question.
PSSA Preparation.
Chapter 30 Induction and Inductance In this chapter we will study the following topics: -Faraday’s law of induction -Lenz’s rule -Electric field induced.
Computer Science Department
ALGORITHMS AND FLOWCHARTS
9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
Representing an algorithm using Flowcharts
L6:CSC © Dr. Basheer M. Nasef Lecture #6 By Dr. Basheer M. Nasef.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Loops.
LOOP (Part 2) for while do-while 1. TK1913-C Programming2 TK1913-C Programming 2 Loop : for Loop : for Condition is tested first Loop is controlled by.
Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop.
Looping While-continue.
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.
Presentation transcript:

Control Structure: Loop (Part 1) Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop control structure 1

TK1913-C Programming2 TK1913-C Programming 2 Loop Structure Condition is tested firstCondition is tested later

TK1913-C Programming3 TK1913-C Programming 3 false true Testing Condition First condition Step x Step y Step a Step n

TK1913-C Programming4 TK1913-C Programming 4 Testing Condition First condition Step x Step y Step a Step n

TK1913-C Programming5 TK1913-C Programming 5 Step a condition Step n Step x false true Step y Testing condition later

TK1913-C Programming6 TK1913-C Programming 6 Step a condition Step n Step x false true Step y Testing condition later

TK1913-C Programming7 TK1913-C Programming 7 How Loops are Controlled Sentinel Controlled Counter Controlled 1, 2, 3, 4, … …, 4, 3, 2, 1 Condition Controlled

TK1913-C Programming8 TK1913-C Programming 8 Counter Controlled Loop counter ← initialValue test counter value Step n Step x false true Update counter

TK1913-C Programming9 TK1913-C Programming 9 Counter Controlled Loop counter = initialValue test counter value Step n Step x false true Update counter counter ← initialValue

TK1913-C Programming10 TK1913-C Programming 10 Example: Can you identify the input and output??? Draw a flowchart for the following problem: Read 5 integer and display the value of their summation. Input : 5 integer n1, n2, n3, n4, n5 Output: The summation of n1, n2,.., n5 Input example: Output example: 20

TK1913-C Programming11 TK1913-C Programming 11 Input n1 Input n2 Input n3 input n4 input n5 output sum sum ← n1+n2+n3+n4+n5 start 2 n1 Assume input example: n2 4 n3 5 n4 6 n5 20 sum end This flowchart does not use loop, hence we need to use 6 different variables

TK1913-C Programming12 TK1913-C Programming 12 Counter- Controlled Loop counter ← 1, sum ← 0 counter < 6 sum ← sum + n false true counter++ output sum input n 1 counter sum 0 1 < 6true 2 n < 6true < 6true < 6true < 6true < 6false Assume input example: This loop is counter-controlled The counter Increases by 1 Uses only 3 variables

TK1913-C Programming13 TK1913-C Programming 13 Decreasing Counter-Controlled Loop counter ← 5, sum ← 0 counter > 0 sum←sum+ x false true counter-- output sum input x

TK1913-C Programming14 TK1913-C Programming 14 Example: Draw a flowchart for this problem; Given an exam marks as input, display the appropriate message based on the rules below:  If marks is greater than 49, display “PASS”, otherwise display “FAIL”  However, for input outside the range, display “WRONG INPUT” and prompt the user to input again until a valid input is entered

TK1913-C Programming15 TK1913-C Programming 15 Condition-Controlled Loop false true input m m 100 m>49 “PASS” “FAIL” true false “WRONG INPUT” Assume m=110 m WRONG INPUT Assume m= > 49 FAIL Assume m= > 49 PASS Condition-controlled loop with its condition being tested at the end

TK1913-C Programming16 TK1913-C Programming 16 false true input m m 100 m>49 “PASS” “FAIL” true false “WRONG INPUT” input m Condition-controlled loop with its condition being tested first

TK1913-C Programming17 TK1913-C Programming 17 Sentinel-Controlled Loop Draw a flowchart for a problem which: Receive a number of positive integers and display the summation and average of these integers. A negative or zero input indicate the end of input process Can you identify the input and output??? Input: A set of integers ending with a negative integer or a zero Output: Summation and Average of these integers

TK1913-C Programming18 TK1913-C Programming 18 Input Example: Output Example: Sum = 88 Average = Sentinel Value

TK1913-C Programming19 TK1913-C Programming 19 Now… What have you understand?

TK1913-C Programming20 TK1913-C Programming 20 x>0 sum←sum+x false true input x sum←0 input x display sum Try to understand What will happen if this statement is deleted??? ? What happened if these 2 statements exchange places sum←sum+x input x ?