Vous êtes sur la page 1sur 24

CHAPTER 7:

Repetitive Statements

BEE1222: COMPUTER PROGRAMMING

1 YAW- credit to HAA,RAK & AIH


Syllabus

• While( ) and Do…


7.1 While( ) Statement

7.2 • For( ) Statement

• Break and Continue


7.3 Statement
2 YAW- credit to HAA,RAK & AIH
Lesson Outcomes
1. Understand the concept of looping
2. Determine types of repetitive loop and use it in
programming

3 YAW- credit to HAA,RAK & AIH


7.1 While() and Do…While( ) statement
 Repetitive Statements
 Looping and counting
 Control the repetitive re-execution or iteration of
sequences of actions
 Two commonly used looping algorithm
 While()
 Do...While()

4 YAW- credit to HAA,RAK & AIH


The while( ) statement
• Syntax :

while ( expression)
{
statement block;
}

5 YAW- credit to HAA,RAK & AIH


The while( ) statement
The while( ) statement
 Expression is first evaluated
 If it is TRUE, the statement block is executed
 If it is FALSE, the statement block is skipped
Example
while Statement
#include <stdio.h>
#include <conio.h>

main()
{
  int x=1;
     while (x<=10)
{
        printf (“ Number %d\n”,x);
 x++;
     }

 getch ();
 return  0;
}
Common Mistakes in while -- extra semi-colon;

while (num < minimum);


{
scanf(“%d”, &num);
printf(“Number must be greater than %d.\n”, minimum);
printf(“Please try again.\n”);
}

Marks the end of


the while-block --
usual cause of
infinite loops
The Do…While( ) statement
• Syntax :

do
statement block;
while ( expression);

10 YAW- credit to HAA,RAK & AIH


The Do…while() Statement
The Do…while() Statement
• Expression is evaluated after
the statement block is executed
• Loop execution will stop if the
expression is FALSE
Example
do...while Statement
#include <stdio.h>
#include <conio.h>

main ()
{
   int count =1;
   do
    {
       printf ("%d\n", count);
       }
   while (count++ <=10) ;
   printf ("BYE");

   getch ();
   return 0;
}
7.2 For( ) Statement
 Form of loop which allows for
initialization and iteration control
 Syntax:

for ( initialization; condition;


update)
{
statement block;
}

14 YAW- credit to HAA,RAK & AIH


The for Statement
int i;

for (i=0;i<=3;i++)
{
statement block
}
The for Statement
 The for loop executes as follows:
 The initial statement executes
 The loop condition is evaluated
 if the loop condition evaluates to true
 execute the for loop statement
 execute the update statement (the third expression in the
parentheses)
 Repeat the previous step until the loop condition evaluates to false
 The initial statement initializes a variable
 The initial statement in the for loop is the first to
be executed and is executed only once
#include <stdio.h>
Example: addfor.c #include <conio.h>
/**********************************\
Read in numbers and add them up
Read in numbers, add Print out the sum and the average
them, and \**********************************/
print the sum and the main()
{
average
float nextNum, sum = 0.0;
int count, totalNumbers;
set sum to 0
set count to 0 printf("Insert total numbers\n");
input totalNumbers scanf("%d", &totalNumbers);
for ( count=0;
count < totalNumbers;
for (count < totalNumbers) count++ )
{ {
input nextNum printf("Insert next numbers\n");
add nextNum to sum scanf("%f", &nextNum);
sum += nextNum;
add 1 to count
printf("Sum was %f\n",sum);
} }
printf("Mean was %f\n",sum/count);
output "Sum was" sum getch ();
output "Mean was" return 0;
sum/count }
printf("Insert total numbers\n");
scanf("%d", &totalNumbers);
for ( count=0;
count < totalNumbers;
count++ )
{
printf("Insert next numbers\n");
scanf("%f", &nextNum);
sum += nextNum;
printf("Sum was %f\n",sum);
}

totalNumber count nextNum sum


s
3 0 1 0+1=1
1 2 1 + 2 =3
2 3 3+3=6
while VS for
#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
float nextNum, sum = 0.0; float nextNum, sum = 0.0;
int count, totalNumbers; int count, totalNumbers;

scanf("%d", &totalNumbers); scanf("%d", &totalNumbers);

count = 0; for ( count=0;


while (count < totalNumbers) count < totalNumbers;
count++ )
{ {
scanf("%f", &nextNum); scanf("%f", &nextNum);
sum += nextNum; sum += nextNum;
count++; }
}
printf("Sum was %f\n",sum); printf("Sum was %f\n",sum);
printf("Mean was %f\n", printf("Mean was %f\n",
sum/count); sum/count);

return 0; return 0;
} }
while and for (cont)
#include <stdio.h> #include <stdio.h>
int main()
Check
Initialize
condition
int main()
{ {
float nextNum, sum = 0.0;
int count, totalNumbers;
Check condition
float nextNum, sum = 0.0;
int count, totalNumbers;
scanf("%d", &totalNumbers);
scanf("%d", &totalNumbers);
count = 0;
while (count < totalNumbers) for ( count=0;
count < totalNumbers;
{ count++ )
scanf("%f", &nextNum); {
sum += nextNum; scanf("%f", &nextNum);
count++; sum += nextNum;
}
}
printf("Sum was %f\n",sum);
printf("Mean was %f\n", printf("Sum was %f\n",sum);
sum/count); printf("Mean was %f\n",
sum/count);

}
return 0; Update
return 0;
}
7.3 Break and Continue Statement
Break Statement Continue Statement
 Use to exit from a  Use only in repetitive
statement block statement
 Transfer instruction
 Use with:
execution sequence to
• switch..case test
• For condition for repetitive
• while () statement
• do..while()

21 YAW- credit to HAA,RAK & AIH


Example (Break)

22 YAW- credit to HAA,RAK & AIH


Example (Continue)

23 YAW- credit to HAA,RAK & AIH


END OF CHAPTER 7
1. Q& A
2. Outcomes
 Understand the concept of looping
 Determine types of repetitive loop and use it in
programming
3. Reflection

24 YAW- credit to HAA,RAK & AIH

Vous aimerez peut-être aussi