Introduction
- Group of statements that are executed repeatedly while some condition remains true
- Each execution of the group of statements is called an iteration of the loop
The while Loop
Example1:Find the sum of digits of a number
void main() { int n, sum=0; scanf ("%d",&n); while (n != 0) { sum = sum + (n % 10); n = n / 10; } printf ("The sum of digits of the number is %d \n", sum); }
Example2:Find the sum of Square of digits of a number
void main() { int N, count, sum; scanf ("%d", &N) ; sum = 0; count = 1; while (count <= N) { sum = sum + count * count; count = count + 1; } printf ("Sum = %d\n", sum) ; return 0; }
Example-3
The For Loop
-
Initialization, loop-continuation test, and update can contain
arithmetic expressions
for( k = x; k <= 4 * x * y; k +=y / x )
-
Update may be negative (decrement)
for (num = 10; num >= 0; --num)
- If loop continuation test is initially 0 (false):
- body of for structure not performed
- Program proceeds with statement after for structure
-
No statement executed
|
|
Example:Computing Factorial
void main () { int N, count, prod; scanf ("%d",&N) ; prod = 1; for (count = 1;count <= N; ++count) { prod = prod * count; } printf ("Factorial = %d\n", prod) ; }
Nesting of for Loop
void main() { int row,i,j,k; printf("Enter number of row for pattern:"); scanf("%d",&row); for(i=1;i<=row;++i) { for(j=1;j<=i;j++) { printf("*"); } printf("\n"); } }
Output:
Enter number of row for pattern:5 * ** *** **** *****
void main() { int row, col,ROWS; printf("Enter number of row for pattern:"); scanf("%d",&ROWS); for (row=0; row < ROWS; ++row) { for (col=1; col<=row; ++col) printf(" "); for (col=1; col<=ROWS-row; ++col) printf("* "); printf ("\n"); } }
Output:
Enter number of row for pattern:10 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Equivalance of while and for Loop
The do-while Loop
Example:Decimal to binary conversion(prints binary in reverse order)
void main() { int dec; scanf ("%d", &dec); do { printf ("%2d", (dec % 2)); dec = dec / 2; } while (dec != 0); printf ("\n"); }
Echo characters typed on screen until end of line
void main () { char echo ; do { scanf ("%c", &echo); printf ("%c",echo); } while (echo != '\n') ; }
The continue statement
The continue statement in C programming works somewhat like the break
statement.
Instead of forcing termination,it forces the next iteration of the
loop to take place,skipping any code in between.
For the for loop, continue statement causes the conditional test and
increment portions of the loop to execute.
For the while and do...while loops, continue statement causes the
program control to pass to the conditional tests.
Syntax
The syntax for a continue statement in C is as follows:
continue;

Example of continue in do-While loop
#include <stdio.h> int main() { int j=0; do { if (j==7) { j++; continue; } printf("%d ", j); j++; }while(j <10); return 0; }
Output: 0 1 2 3 4 5 6 8 9
An Example with break and continue
void main() { int fact = 1; int i = 1; while (1) { fact = fact * i; ++i; if ( i <=10 ) continue; /* not done yet ! Go to loop and perform next iteration*/ break; }
Chapter 5 Quiz
1.What are the primary iterations in C programming?
2.What is a while loop?
3.Which of the following is true in the case of c programming?
4.What does a do-while loop do?
5.Which of the following cannot be checked in a switch-case statement?