際際滷

際際滷Share a Scribd company logo
Chapter 4: Control Structure 
Repetitions/Loops
Objectives
State the types of repetitions
Differentiate among three types of
repetitions
Build a simple program to solve a problem
using repetitions
pretest Repetitions structure
 The expression (syarat) is evaluated, before the
loop body is executed.
 If the result of evaluation is true. the loop body is
executed. Following this, control goes back to
the expression and is evaluated again. This
continues until the expression computes to a
value of false.
 If the expression produce of false, the loop body
is bypassed and the program control drops
down to the statement.
Looping
Looping
Posttest repetition statement
First executes the loop body and then
computes the expression (syarat).
If the expression is true, the loop body is
executed again; otherwise the repetition
terminates.
Looping
Looping
Repetitions with counter value (nilai
pembilang)
Nilai Pembilang Hasil dari pelaksanaan arahan nilai pembilang
i++ Nilai pembilang iaitu i ditambah dengan satu
i-- Nilai pembilang i dikurangkan dengan satu
i+=2 Nilai pembilang i ditambah dengan 2
i*=2 Nilai pembilang i didarab dengan 2
While loop
 #include<iostream.h>
 main()
 {
 int i=0;
 while ( i <= 5 )
 {
 cout<<"n Welcome to while statement";
 i++;
 }
 }
do  while loop
 #include<iostream.h>
 main()
 {
 int i=0;
 do {
 cout<<"n Welcome to while statement";
 i++;
 } while ( i <=5);
 }
For loop
#include<iostream.h>
main()
{
 int i;
 for (i=0; i<=5; i++)
 cout<<"n Welcome to while statement";
}
For loops with counter
 Sintaksis:
 for (pembolehubah = nilai_awalan; syarat;
pembilang)
 Blok arahan
 { senarai penyataan;
 :
 :

 }
Loops with sentinel value
we dont know the number of loops will
execute.
For example, we dont know how many
items bought by customer.
Refer to Rajah 4.8 page# 62
Nested Loop
In a nested loop, one loop referred to as
the inner loop and one is called the outer
loop.

More Related Content

Looping

  • 1. Chapter 4: Control Structure Repetitions/Loops
  • 2. Objectives State the types of repetitions Differentiate among three types of repetitions Build a simple program to solve a problem using repetitions
  • 3. pretest Repetitions structure The expression (syarat) is evaluated, before the loop body is executed. If the result of evaluation is true. the loop body is executed. Following this, control goes back to the expression and is evaluated again. This continues until the expression computes to a value of false. If the expression produce of false, the loop body is bypassed and the program control drops down to the statement.
  • 6. Posttest repetition statement First executes the loop body and then computes the expression (syarat). If the expression is true, the loop body is executed again; otherwise the repetition terminates.
  • 9. Repetitions with counter value (nilai pembilang) Nilai Pembilang Hasil dari pelaksanaan arahan nilai pembilang i++ Nilai pembilang iaitu i ditambah dengan satu i-- Nilai pembilang i dikurangkan dengan satu i+=2 Nilai pembilang i ditambah dengan 2 i*=2 Nilai pembilang i didarab dengan 2
  • 10. While loop #include<iostream.h> main() { int i=0; while ( i <= 5 ) { cout<<"n Welcome to while statement"; i++; } }
  • 11. do while loop #include<iostream.h> main() { int i=0; do { cout<<"n Welcome to while statement"; i++; } while ( i <=5); }
  • 12. For loop #include<iostream.h> main() { int i; for (i=0; i<=5; i++) cout<<"n Welcome to while statement"; }
  • 13. For loops with counter Sintaksis: for (pembolehubah = nilai_awalan; syarat; pembilang) Blok arahan { senarai penyataan; : : }
  • 14. Loops with sentinel value we dont know the number of loops will execute. For example, we dont know how many items bought by customer. Refer to Rajah 4.8 page# 62
  • 15. Nested Loop In a nested loop, one loop referred to as the inner loop and one is called the outer loop.