0% found this document useful (0 votes)
2 views5 pages

Understanding Do-While Loops in C

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)
2 views5 pages

Understanding Do-While Loops in C

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

Structured Programming

Structured Programming
CC112
Lecture 8
Do-While Loop

Eng. Iba Almajzoub


‫ﻭﺏ‬‫ﺬ‬‫ﻤﺠ‬‫ﻟ‬
‫ﺍ‬ ‫ء‬
‫ﺎ‬‫ﺑ‬
‫ﺇ‬‫ﺪﺱ‬ ‫ﻨ‬
‫ﻬ‬‫ﻤ‬‫ﻟ‬
‫ﺍ‬
ibaa99@[Link]
[Link]
c SP Lecture 8 1

A Sentinel-controlled Loop

 Requires a“ priming read”


“priming read”means you read one set
of data before the while

c SP Lecture 8 2

Iba Almajzoub 1
Structured Programming

/* Example of Sentinel controlled loop */


total = 0;
printf(“Patient Blood pressure ( -1 to stop): “);
scanf(“%d “, &thisBP ) ;
while (thisBP != -1) /* while not sentinel */
{
total = total + thisBP;
printf(“Patient Blood pressure ( -1 to stop): “);
scanf(“%d “, &thisBP ) ;
}
printf(“The total is %d \n”,total );
c SP Lecture 8 3

Flag-controlled Loops
 You initialize a flag (to true or false)
 Use meaningful name for the flag
 A condition in the loop body changes the
value of the flag
 Test for the flag in the loop test expression

c SP Lecture 8 4

Iba Almajzoub 2
Structured Programming

count = 0;
total = 0;
isSafe = 1; /* initialize flag */
while ( isSafe == 1 )
{ printf(“Enter Blood pressure “ );
scanf(“%d “, &thisBP );
if ( thisBP >= 200 )
isSafe = 0 ; /* change flag value */
else
{count++;
total = total + thisBP ;
}
c SP Lecture 8 5
}

Do-While Statement
 Isa 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.
c SP Lecture 8 6

Iba Almajzoub 3
Structured Programming

Blood Pressure Example


char more ;
int thisBP , total;
total = 0 ;
do
{
printf(“Patient Blood pressure : “) ;
scanf(“%d “, &thisBP ) ;
total = total + thisBP ;
printf(“Any more patients ? (Y/N) “) ;
scanf(“%c “, &more ) ;
} while ( more == ‘ y’|| more == ‘ Y’) ;
printf ( “Total = %d “, total );
c SP Lecture 8 7

Do-While Loop vs. While Loop

 POST-TEST loop (exit-  PRE-TEST loop (entry-


condition) condition)
 The looping condition is  The looping condition
tested after executing is tested before
the loop body. executing the loop
 Loop body is always body.
executed at least once.  Loop body may not be
executed at all.

c SP Lecture 8 8

Iba Almajzoub 4
Structured Programming

While Do-While Loop


DO

True Statement
Condition
WHILE
Statement 1 False
Expression
True
Statement 2
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 statement.
c SP Lecture 8 9

Iba Almajzoub 5

You might also like