0% found this document useful (0 votes)
7 views41 pages

Lecture 13 Loops1

Uploaded by

ashraf_elbehari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views41 pages

Lecture 13 Loops1

Uploaded by

ashraf_elbehari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Repetition (1)

C Programming
AAST
CCIT
 What is a Loop?
 Count-Controlled Loops
 Event-Controlled Loops
 While Statement Syntax
 Using a While Statement for Summing
and Counting
 Nested While Loops
 Loop Testing and Debugging
 A loop is a repetition control structure.

 That is, you can execute particular


statements more than once in a
controlled fashion

 Statements are executed as long as


some condition remains true
count controlled loops
repeat a specified number of times

event-controlled loops
some condition within the loop body
changes and this causes the repeating to
stop
SYNTAX

while ( Expression )
{ .
. /*loop body */
.
}

NOTE: Loop body can be a single


statement, a null statement, or a block.
When the expression is tested and found to be
false, the loop is exited and control passes to
the statement which follows the loop body.

WHILE LOOP

FALSE
Expression

TRUE
body
statement
Parts of a While Loop

 Every while loop will always contain three main


elements:

 Priming: initialize your variables.

 Testing: test against some known condition.

 Updating: update the variable that is tested.


int count ;
1. Priming

count = 4; /* initialize loop variable */


2. Test Condition

while (count > 0) /* test expression */


{
printf(“ %d \n ”,count ) ; /* repeated action */

count -- ; /*update loop variable */


} 3. Update

printf( “Done” ) ;
count
int count ;

count = 4;

while (count > 0)


{ OUTPUT
printf( " %d \n " , count ) ;

count -- ;
}
printf( " Done " ) ;
count
int count ;

count = 4; 4

while (count > 0)


{ OUTPUT
printf( " %d \n " , count ) ;

count -- ;
}
printf( " Done " ) ;
count
int count ;

count = 4; 4

while (count > 0) TRUE


{ OUTPUT
printf( " %d \n " , count ) ;

count -- ;
}
printf( " Done " ) ;
count
int count ;

count = 4; 4

while (count > 0)


{ OUTPUT
printf( " %d \n " , count ) ; 4

count -- ;
}
printf( " Done " ) ;
count
int count ;

count = 4; 3

while (count > 0)


{ OUTPUT
printf( " %d \n " , count ) ; 4

count -- ;
}
printf( " Done " ) ;
count
int count ;

count = 4; 3

while (count > 0) TRUE


{ OUTPUT
printf( " %d \n " , count ) ; 4

count -- ;
}
printf( " Done " ) ;
count
int count ;

count = 4; 3

while (count > 0)


{ OUTPUT
printf( " %d \n " , count ) ; 4
3
count -- ;
}
printf( " Done " ) ;
count
int count ;

count = 4; 2

while (count > 0)


{ OUTPUT
printf( " %d \n " , count ) ; 4
3
count -- ;
}
printf( " Done " ) ;
count
int count ;

count = 4; 2

while (count > 0) TRUE


{ OUTPUT
printf( " %d \n " , count ) ; 4
3
count -- ;
}
printf( " Done " ) ;
count
int count ;

count = 4; 2

while (count > 0)


{ OUTPUT
printf( " %d \n " , count ) ; 4
3
count -- ; 2
}
printf( " Done " ) ;
count
int count ;

count = 4; 1

while (count > 0)


{ OUTPUT
printf( " %d \n " , count ) ; 4
3
count -- ; 2
}
printf( " Done " ) ;
count
int count ;

count = 4; 1

while (count > 0) TRUE


{ OUTPUT
printf( " %d \n " , count ) ; 4
3
count -- ; 2
}
printf( " Done " ) ;
count
int count ;

count = 4; 1

while (count > 0)


{ OUTPUT
printf( " %d \n " , count ) ; 4
3
count -- ; 2
1
}
printf( " Done " ) ;
count
int count ;

count = 4; 0

while (count > 0)


{ OUTPUT
printf( " %d \n " , count ) ; 4
3
count -- ; 2
1
}
printf( " Done " ) ;
count
int count ;

count = 4; 0

while (count > 0) FALSE


{ OUTPUT
printf( " %d \n " , count ) ; 4
3
count -- ; 2
1
}
printf( " Done " ) ;
count
int count ;

count = 4; 0

while (count > 0)


{ OUTPUT
printf( " %d \n " , count ) ; 4
3
count -- ; 2
1
}
Done
printf( " Done " ) ;
Simple While Loop
OUTPUT:
#include <stdio.h> Index: 1
#define MAX 10 Index: 2
main () 1. Priming
Index: 3
Index: 4
Index: 5
{ Index: 6
int index =1; 2. Test Condition Index: 7
Index: 8
Index: 9
while (index <= MAX)
Index: 10
{

printf ("Index: %d\n", index);


index = index + 1;
3. Update
}
}
Infinite loop

•Infinite Loop: A loop that never ends.


–Generally, you want to avoid these!
–There are special cases, however, when
you do want to create infinite loops on
purpose.
•Common Exam Questions:
–Given a piece of code, identify the bug in
the code.
–You may need to identify infinite loops.
Infinite While Loop
#include <stdio.h>
#define MAX 10
main () 1. Priming Index: 1
Index: 1
{ Index: 1

int index =1; Index: 1


2. Test Condition Index: 1

while (index <= MAX) … [forever]


{

printf ("Index: %d\n", index);


3. Where is the
} update part
}
Infinite While Loop
#include <stdio.h>
/* no MAX here */ Index: 1

main () 1. Priming
Index: 2
Index: 3
Index: 4
{ Index: 5
int index =1; 2. Test Condition
… [forever]

while (index > 0)


{

printf ("Index: %d\n", index);


index = index + 1;
3. Update
}
}
Count-Controlled Loop Example

Use a while loop to read 100


grades in an exam and find their
total.
int thisGrade ;
int total=0 ;
int count ;

count = 1 ; /* initialize */

while ( count < 101 ) /* test expression */


{
printf(“ Student %d Grade : “, count );
scanf(“ %d “ , &thisGrade ) ;
total = total + thisGrade ;
count++ ; /* update */

printf(“The total = %d \n“ , total ) ;


 Signals the end of data entry

 Also known as signal value, dummy value,


or flag value

 Also known as indefinite repetition


because the number of repetitions the
code will perform is unknown before the
loop
 How are they used?
 Programmer picks a value that would never be
encountered for normal data

 User enters normal data and then when done,


enters the unusual value

 The loop would stop when seeing the unusual


value
total = 0;

printf(“ Grade ( -1 to stop): “ );


scanf(“ %d “ , &thisGrade ) ;

while (thisGrade != -1)


{
total = total + thisGrade;
printf(“ Grade ( -1 to stop): “ );
scanf(“ %d “ , &thisGrade ) ;
}
printf(“ The total is %d \n” ,total );
count = 0;
total = 0;
GoOn = 1; /* initialize flag */

while ( GoOn == 1 )
{
printf(“ Enter Grade “);
scanf(“ %d “ , &thisGrade );
if ( thisGrade == -1 )
GoOn = 0 ; /* change flag value */
else {
count++;
total = total + thisGrade ;
}
}
printf( “ Total is %d \n “, total ) ;
 write a program that reads in the
grades of 50 students in a course (out
of 100 points each ) and then count the
number of A students ( grade > 85 ) and
the number of B students (grade > 75 ).
 Write a program that does a survey on a
certain question. The question has 3
possible answers. Run the survey on 20
people and then display the number of
people who chose each answer.
 Example:
What is your favorite subject?
A. Mathematics
B. Economics
C. Programming
Is a looping control structure in which the loop
condition is tested after each iteration of the
loop.

SYNTAX
do
{
Statements

} while ( Expression ) ;

Loop body statement can be a single statement or a


block.
Grades Example
char more ;
int thisGrade , total;

total = 0 ;
do
{
printf(“ Grade : “ ) ;
scanf(“ %d “ , &thisGrade ) ;

total = total + thisGrade ;

printf(“ Any more students ? (Y/N) “ ) ;


scanf(“ %c “ , &more ) ;

} while ( more == ‘y’ || more == ‘Y’ ) ;

printf ( “ Total = %d “ , total );


 POST-TEST loop  PRE-TEST loop
(exit-condition) (entry-condition)
 The looping condition  The looping condition
is tested after is tested before
executing the loop executing the loop
body. body.
 Loop body is always  Loop body may not
executed at least be executed at all.
once.
DO

Statement

WHILE

Expression
TRUE
FALSE

When the expression is tested and found to be


false, the loop is exited and control passes to
the statement that follows the do-while
 Assume you put 1000 pounds in a projects that
returns a profit of about 5% per year. How long
will it take for your money to double ?
 Assume you put 5000 pounds in a projects that
returns a profit of about 10% per year. How
much money will you have in 5 years ?

You might also like