Pendalaman Materi Conditional dan Repetition (Pengulangan)

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming
Advertisements

Repetition Control Structures
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
CS0004: Introduction to Programming Repetition – Do Loops.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Objectives In this chapter, you will learn about:
Repeating Actions While and For Loops
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Do Loop The syntax of DO loop: DO variable = initial_value, final_value[, increment] [statements] END DO Example: PROGRAM LINES ! Illustration of DO-loops.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Chapter 5: Loops and Files.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
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.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
Jaeki Song ISQS6337 JAVA Lecture 04 Control Structure - Selection, and Repetition -
Programming Logic and Design Fifth Edition, Comprehensive
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.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Programming Logic and Design Sixth Edition Chapter 5 Looping.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
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.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Repetition Control Structures Simple Program Design Third Edition A Step-by-Step Approach 5.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Loops and Files. 5.1 The Increment and Decrement Operators.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Chapter 7 Problem Solving with Loops
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
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.
REPETITION CONTROL STRUCTURE
PROGRAM CONTROL STRUCTURE
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
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.
JavaScript: Control Statements I
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Outline Altering flow of control Boolean expressions
Chapter 6: Repetition Statements
CHAPTER 4 Iterative Structure.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Presentation transcript:

Pendalaman Materi Conditional dan Repetition (Pengulangan)

IF THEN Bersarang Input (nilai) IF nilai > 85 AND nilai <= 100 THEN Output (‘Nilai mutu = A’) ELSE IF nilai > 60 AND nilai <= 85 THEN Output (‘Nilai mutu = B’) ELSE IF nilai > 45 AND nilai <= 60 THEN Output (‘Nilai mutu = C’) ELSE IF nilai > 30 AND nilai <= 45 THEN Output (‘Nilai mutu = D’) ELSE Output (‘Nilai mutu = E’) 5.The value variable of nilai = 60, the result is ‘Nilai mutu = B’. 6.The value variable of nilai = 101, the result is ‘Nilai mutu = E’. 7.The value variable of nilai = -5, the result is ‘Error’.

Write the algorithm to calculate cost of electricity with following constraints : (KWHs)Rate * per KWH under 500 over * per KWH under 1000 Enter the values of previous month meters, current month meter, and then displays the total cost which would exist.

4 Loops notation 1.While-Do 2.Repeat-Until,and 3.For-to-Do

5 WHILE-DO-ENDWHILE it allows a {group of} program statement(s) to be executed a number of times. The program statement(s) are executed when the condition evaluates as true. Somewhere inside the loop the value of the variable which is controlling the loop (ie, being tested in the condition) must change so that the loop can finally exit.

6 The structure of the WHILE... DO... statement... {initiation} WHILE (condition) DO program statement … counter statement... Counted loops are special types of pre-test loops. They are used when a known number of repetitions will occur. condition is the Boolean Expression, whether it be AND, OR, or NOT, is first carried out. If the result is TRUE, the DO statement will then be performed. However, if the result is FALSE, the DO statement will not be executed Iteration will be controlled by Counter statement, wether it be increase or decrease The value of counter will be changed at the end of loop.

7 Flowchart WHILE... DO... START END condition = true? action {initiationi} {changing counter} yes No

8 Example 1 Pseudocode WHILE there is pressure on the mat Sound the bell Flowchart Pressure on mat? Sound bell T F

9 Example 2 Pseudocode OUTPUT('before loop starts') i  0 { Initialize counter variable } WHILE (i <= 5) DO { Loop with predicate in ()} i  i + 1 {Increment counter variable} OUTPUT('iteration number ',i) OUTPUT('after loop ends')

10 Execution generates the output Iteration no.ii <= 5 ?i  i+ 1 Output before loop starts 1 0True 1 iteration number True2 iteration number 2 32True3 iteration number 3 43True4 iteration number 4 54True5 iteration number 5 65True6 iteration number 6 76Falseafter loop ends Here, the loop predicate is the relational test "i <= 5", which causes the loop to terminate after i is incremented to six.

11 Example 3 i  0 {initialize} WHILE (i < 5) DO {number of iteration} Output (‘*’){statement} i  i + 1{increment counter} The result of program : *

12 Example 4 Program with enter the value of 3 student’s scores : Pseudocode i  1 WHILE i <= 3 DO Output (‘Score student no-‘,i,’ is:’) Input (score) i  i + 1 Execution : – Score student no-1 is : __ – Score student no-2 is : __ – Score student no-3 is : __

13 Example 5 Average of 3 numbers number  0 {initialize} count  3{counters} WHILE count > 0 DO Input (x){enter value of x} number  number + x {sum of x variables} count  count – 1 {decrement counter variable} average  number/ 3 {calculate the average} Output (average)

14 REPEAT-UNTIL Post-test loops tests the condition at the end of the loop. The process in the loop is executed first whether it is true or false and will keep repeating until the condition is met. Post-test loops end when the condition is true but the loop always do the loop at least once, even if the end condition is originally true. Post-test loops are also called unguarded loops because no check is made before the algorithm begins the loop structure.

15 The structure of the REPEAT-UNTIL statement... {initialize} REPEAT program statement counter variable UNTIL (condition)...

16 Flowchart REPEAT-UNTIL MULAI aksi {inisialisasi} END condition = true? START Program statement {action} {initialize} Counter changes No yes

17 Example 1 Pseudocode REPEAT Take out one item UNTIL bag is empty Is bag empty? Flowchart Take out item T F

18 Example 2 Pseudocode OUTPUT('before loop starts') i  0 { Initialize counter variable } REPEAT i  i + 1 {Increment counter variable} OUTPUT('iteration number ',i) UNTIL (i= 5) { termination} OUTPUT('after loop ends')

19 Execution generates the output Iteration no.ii  i+ 1 Outputi = 5 ? before loop starts iteration number 1 False iteration number 2 False 323 iteration number 3 False 434 iteration number 4 False 545 iteration number 5 True 656 after loop ends

20 Example 3 i  0 {initialize} REPEAT Output (‘*’){action statement} i  i + 1{increment counter} UNTIL i=5{end loop} The result of program : *

21 Example 4 Program with enter the value of 3 student’s scores : Pseudocode i  1 REPEAT Output (‘Score student no-‘,i,’ is:’) Input (score) i  i + 1 UNTIL i = 3 Execution : – Score student no-1 is : __ – Score student no-2 is : __ – Score student no-3 is : __

22 Example 5 Average of 3 numbers number  0 {initialize} count  3{counters} REPEAT Input (x){input value of x} number  number + x {sum of x variables} count  count – 1 {decrement counter variable} UNTIL count=0 average  number/ 3 {calculate the average} Output (average)

23 FOR-TO-DO The statement inside the for block is executed a number of times depending on the control condition. A loop index or loop counter is an integer variable that is used to keep track of how many times a loop has executed. A loop limit is a variable or constant that is integer-valued, which determines the number of times a loop will execute, or a maximum value of the loop index to be reached at loop termination.. A loop increment is the step size for incrementing the loop counter.

24 The structure of the FOR-TO-DO statement FORcounter variable  initial_value TO final_value program statement

25 Flowchart FOR-TO-DO No yes START END Counter >= initial_value AND counter <= final_value ? action {initialize}

26 Example 1 Pseudocode FOR x  1 TO 5 DO output (x) Flowchart x >=1 AND x <= 5 ? START END output (x) No yes

27 Example 2 Pseudocode OUTPUT('before loop starts') FOR i = 1 TO 5 DO OUTPUT('iteration number ',i) OUTPUT('after loop ends')

28 Execution generates the output Iteration no.ii >=1 AND i<= 5 ? Output before loop starts 11 True iteration number 1 22True iteration number 2 33True iteration number 3 44True iteration number 4 55True iteration number False after loop ends

29 Example 3 FOR i  1 TO 5 DO Output (‘*’){action statement} The result of program : *

30 Example 4 Program with enter the value of 3 student’s scores : Pseudocode FOR i  1 TO 3 DO Output (‘Score student no-‘,i,’ is:’) Input (score) Execution : – Score student no-1 is : __ – Score student no-2 is : __ – Score student no-3 is : __

31 Example 5 Average of 3 numbers number  0 {initialize} FOR i  1 TO 3 DO Input (x){input value of x} number  number + x {sum of x variables} average  number/ 3 {calculate the average} Output (average)

32 Contoh Nested Loop (pengulangan bersarang) FOR i  0 TO 4 DO FOR j  0 TO 2 DO Output (‘*’){aksi} Program output : ***

33 Contoh Nested Loop (pengulangan bersarang) Input values of 3 student’s score that have 2 different values FOR i  1 TO 3 DO Output (‘Student score no-‘,i,’ are ’) FOR j  1 TO 2 DO Output (‘Score ’,j,’ :’) Input (score) Running : Student score no- 1 are Score, 1 : _ Score, 2 : _ Student score no- 2 are Score, 1 : _ Score, 2 : _ Student score no- 3 are Score, 1 : _ Score, 2 : _

Soal 1 What are values of the variable x after the execution of the following pseudocode? n  0 x  0 While n < 15 Do If (n mod 3) = 0 Then x  x + n Output(x) n  n +1 A.1, 3, 6, 9, 12 B.0, 3, 6, 9,12 C.3, 18, 24 D.0, 3, 9,18, 30 E.3,6,9,12

Soal 2 What is the output of the following pseudocode? FOR i  1 TO 2 DO IF i mod 2 = 0 THEN FOR j  1 TO 4 DO IF j mod 2 = 1 THEN Output (‘x’) ELSE Output (‘o’) ELSE Output (i) A. i xoxo B. xoxo i C. i oxox D. oxox i E. i i

36 Soal 3 Consider the following statement – INPUT: 7 – PROCESS: – OUTPUT: 28 Create your pseudocode

37 Soal 4 Determine the program of the following output, * ** *** ****

38 Soal 5 Determine the program of the following output, **** *** ** *

39 Soal 6 What is the output of the following code {Declare} X,i : Integer Algorithm For X  1 to 10 IF (X mod 2= 0) THEN Output(X) i  i + 1 Output(i)

40 Soal 7 Describe the output of the following program for i  1 to 3 do for j  5 to 6 do Output(i,j)

41 Soal 8 Write a program that it display the traditional song “Anak Ayam”. Enter the first anak ayam from keyboard.

42 Soal 9 Write a program that display ‘*’ on even line and displays ‘#’ on odd line. Enter number of line from keyboard

43 Soal 10 Write a program which outputs triangle numbers : 1, 3, 6, 10, 15, etc.

44 Soal 11 (nested loop) Write a program which takes two integer values from the user and outputs the multiplication tables between the first and second numbers. (eg 4 and 7 will output the 4 times table, the 5 times table, the 6 times table and the 7 times table

45 Soal 12 Write a program which validate a user password. If program accepts invalid password, the output will display “invalid password, try again!”, and then program request the password again until 3 times, after 3 rd iteration, the output is ”unauthorized user”. The display to user who enter valid password is “wellcome”.

46 Soal 13 Write a program which takes N integer values from the user and outputs the maximum. Illustration – N = 3 Data 1 : 15 Data 2 : 20 Data 3 : 10 – Maximum = 20