3. Printing the counting from 1 to 10
#include<stdio.h>
int main()
{
int i = 1;
while (i <= 10)
{
printf("i = %dn", i);
i++;
}
getchar();
return 0;
}
Dr. Yousaf, PIEAS
4. while Loop
For loops can usually be rewritten as while
loops:
initialization;
while ( loopContinuationTest )
{
statement;
increment;
}
Dr. Yousaf, PIEAS
5. while Loop
• Generic Form
while (condition)
statement
• Executes as expected:
1. condition is evaluated
2. If condition is false (i.e. 0), loop is exited (go to step 5)
3. If condition is true (i.e. nonzero), statement is executed
4. Go to step 1
5. Next statement
• Note:
for (exp1; exp2; exp3)
stmt;
is equivalent to
exp1;
while(exp2)
{ stmt; exp3; }
Dr. Yousaf, PIEAS
6. // While loop is a top-test loop
#include<stdio.h>
int main()
{
int x = 7;
while (x < 9)
{
printf(" %d is less than 9n", x);
x++;
}
getchar();
return 0;
}
Dr. Yousaf, PIEAS
7. while Loop
#include <stdio.h>
int main()
{
int x = 0; /* Don't forget to declare variables */
while ( x < 10 )
{ /* While x is less than 10 */
printf( "%dn", x );
x++; /* Update x so the condition can be met
eventually */
}
return 0;
}
Dr. Yousaf, PIEAS
9. Counting from 1 to 10
#include<stdio.h>
int main()
{
int i = 1;
do
{
printf("i = %dn", i);
i++;
} while (i <= 10) ;
getchar();
return 0;
}
Dr. Yousaf, PIEAS
10. // Do While loop demonstration
// It is a bottom-test loop instead of a top-test loop
#include<stdio.h>
int main()
{
int x = 11;
do
{
printf(" %d is less than 9n", x);
x++;
} while (x < 9);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
11. • The do/while repetition structure
– Similar to the while structure
– Condition for repetition tested after the body of
the loop is performed
• All actions are performed at least once
– Generic Format:
do {
statement;
} while ( condition );
Dr. Yousaf, PIEAS
The do while Loop
12. The do while Loop
• Standard repeat until loop
• Like a while loop, but with condition test at bottom.
• Always executes at least once.
• The semantics of do...while:
1. Execute statement
2. Evaluate condition
3. If condition is true go to step 1
4. Next statement
Dr. Yousaf, PIEAS
13. Flowchart of the do/while Loop
true
false
action(s)
condition
Dr. Yousaf, PIEAS
14. The do while Loop
#include <stdio.h>
int main()
{
int x;
x = 0;
do
{
/* The value of x will be printed at least one time
even though the condition is false*/
printf( "%dn", x );
x++;
} while ( x != 10 );
getcahr(); return 0;
}
Dr. Yousaf, PIEAS
16. Loops
for (i = 0; i < 12; i++)
{
dowork();
}
i = 0;
while ( i < 12)
{
dowork();
i++;
}
i = 0;
do
{
dowork();
i++;
} while (i < 12); // use of semicolon
Dr. Yousaf, PIEAS
18. The while Repetition Structure
Example:
int product = 2;
while ( product <= 1000 )
product = 2 * product;
product <= 1000 product = 2 * product
true
false
Dr. Yousaf, PIEAS
19. The while Repetition Structure
• 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
Dr. Yousaf, PIEAS
22. Write a complete and an efficient C program that takes an
integer number from the user and calculates its factorial.
#include <stdio.h>
int main()
{
int n, num=1,factorial=1;
printf(“Enter value of N: ”);
scanf(“%d”,&n);
for(num=1;num<=n;num++)
{
factorial=factorial*num;
}
printf(”The factorial of %d = %d “ ,n,factorial);
getchar(); return 0;
}
Dr. Yousaf, PIEAS
For Loop -- Example
23. Write a C program that gets a number from the user. If the number is even
the program will terminate otherwise the program will get another number.
The while loop should terminate only when an even number is provided.
Dr. Yousaf, PIEAS
while Loop -- Example
24. Write a C program that gets a number from the user. If the number is even
the program will terminate otherwise the program will get another number.
The while loop should terminate only when an even number is provided.
#include <stdio.h>
int main()
{
int a=1;
while(a%2!=0)
{
printf(“n Enter Value of a = “);
scanf(“%d”,&a);
}
printf(“nEnd of While loop”);
getchar(); return 0; } Dr. Yousaf, PIEAS
while Loop -- Example
25. Try to print even numbers between two given integer
numbers A and B such that A can be greater or smaller
than B. Numbers A and B are to be taken from the
user.
(Try it Yourself)
Dr. Yousaf, PIEAS
while Loop -- Example