Professional Documents
Culture Documents
Computing Fundamentals: Dr. Muhammad Yousaf Hamza
Computing Fundamentals: Dr. Muhammad Yousaf Hamza
int main()
{
int x = 7;
while (x < 9)
{
printf(" %d is less than 9\n", x);
x++;
}
getchar();
return 0;
}
int main()
{
int x = 0; /* Don't forget to declare variables */
while ( x < 10 )
{ /* While x is less than 10 */
printf( "%d\n", x );
x++; /* Update x so the condition can be met
eventually */
}
getchar();
return 0;
}
Dr. Yousaf, PIEAS
do while Loop
#include<stdio.h>
int main()
{
int x = 11;
do
{
printf(" %d is less than 9\n", x);
x++;
} while (x < 9);
getchar();
return 0;
}
action(s)
true
condition
false
true
product <= 1000 product = 2 * product
false
• Repetition structure
– Programmer specifies an action to be repeated
while some condition remains true
– Psuedocode:
While there are more items on my shopping list
Purchase next item and cross it off my list
– while loop repeated until condition becomes
false
}
printf(“\nEnd of While loop”);
getchar(); return 0; }
Dr. Yousaf, PIEAS
while Loop -- Example
(Try it Yourself)