Flow Control
Loops
A type of control structure that repeats a statement or set
of statements is known as looping structure (iterative
structure).
Loops are used to
▪ Execute a statement or number of statements for a
specified number of times.
▪ Use a sequence of values.
The loop depends upon the value of a variable known as
counter variable.
Three types of loops
➢ for
➢ while
➢ do-while
The for Statement (1)
The most important looping structure in C.
Generic Form:
for (initial ; condition ; increment )
statement
initial, condition, and increment are C expressions.
For loops are executed as follows:
1. initial is evaluated. Usually an assignment statement.
2. condition is evaluated. Usually a relational expression.
3. If condition is false (i.e. 0), fall out of the loop (go to step 6.)
4. If condition is true (i.e. nonzero), execute statement
5. Execute increment and go back to step 2.
6. Next statement
Increment /
decrement
The for Statement (2)
For statement examples /* 4. initialization outside of loop */
#include <stdio.h> count = 1;
int main () { for ( ; count < 1000; count++)
int count,x,y; printf("%d ", count);
int ctd;
/* 5. very little need be in the for */
/* 1. simple counted for loop */ count=1; ctd=1;
for (count =1; count <=20; count++) for ( ; ctd; ) {
printf ("%d\n", count); printf("%d ", count);
count++; ctd=count<1000;
/* 2. for loop counting backwards */ }
for (count = 100; count >0; count--) {
x*=count; /* 6. compound statements for
initialization and increment */
printf("count=%d x=%d\n", count,x);
for (x=0, y=100; x<y; x++, y--) {
}
printf("%d %d\n", x,y);
}
/* 3. for loop counting by 5's */
return 0;
for (count=0; count<1000; count += 5)
}
y=y+count;
Example no 1
Write a program that displays the following shapes using
for loop. (It will only display line of asterisks 10 times)
#include<conio.h>
int main()
{ int i;
for(i=1;i<=10;i++)
printf("**********\n");
getch();
}
Example No 2
// Program to calculate the sum of first n natural numbers
// Positive integers 1,2,3...n are known as natural numbers
#include<stdio.h>
#include<conio.h>
int main()
{
int num, count, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);
// for loop terminates when n is less than count
for(count = 1; count <= num; ++count)
{
sum += count;
}
printf("Sum = %d", sum);
getch();
}
Example No 3
Write a program that displays all 256 ASCII characters
#include<conio.h>
#include<stdio.h>
int main()
{
int i;
char ch=0;
for(i=0;i<=256;i++)
{
printf("%c“,ch);
ch+=1;
}
getch();
}
Example 4
Display A- Z
#include<stdio.h>
#include<conio.h>
int main()
{
char c;
for(c = 'A'; c <= 'Z'; ++c)
printf("%c ", c);
getch();
}
Program 5
Program that display A – Z with their ASCII values.
#include<conio.h>
#include<stdio.h>
int main()
{
char c;
for(c = 'A'; c <= 'Z'; c++)
{
printf("%c ", c);
printf("%i \n", c);
}
getch();
}
Write a program that inputs table number and the length
of the table and displays the table using for loop.
Write a program that enters a number from user and
finds its factorial.
Write a program that finds the sum of the squares of
integers from 1 to n. where n is a positive value entered
by the user.
The while Statement
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 ( ; condition ; ) is equivalent to while (condition)
stmt; stmt;
– for (exp1; exp2; exp3) stmt;
is equivalent to
exp1;
while(exp2) { stmt; exp3; }
This is an indefinite loop since i remains equal to 1 forever. The correct form will be
Flow chart
Here, the key point to note is that a while loop might
not execute at all.
When the condition is tested and the result is false, the
loop body will be skipped and the first statement after
the while loop will be executed.
Example
Example 1
Write a code that compare input number with 20 and
print it and incrementing the number.
#include <stdio.h>
int main ()
{ /* local variable definition */
int a = 10;
/* while loop execution */
while( a < 20 )
{
printf("value of a: %d\n", a);
a++;
}
getch();
Out put:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Example 2:
/ Program to find factorial of a number
// For a positive integer n, factorial = 1*2*3...n
#include <stdio.h>
int main()
{
int number;
Int factorial;
printf("Enter an integer: ");
scanf("%d",&number);
factorial = 1;
// loop terminates when number is less than or equal to 0
while (number > 0)
{
factorial *= number;
// factorial = factorial*number;
--number;
}
printf("Factorial= %d", factorial);
return 0;
}
Example 3
Write a program that enters an integer from the user (n).
The program should display the sum of all even numbers
and sum of all odd numbers from 1…n.
Example Solution
Example
Prompt the user for a positive integer. Display a series
from 1…n and its corresponding squares.
Example solution
The do ... while Loop (1)
Generic Form:
do
statement
while (condition);
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
The do ... while Loop (2)
#include <stdio.h> /* simple function get_menu_choice */
int get_menu_choice (void);
main() int get_menu_choice (void)
{ {
int choice; int selection = 0;
do do {
{ printf ("\n");
choice = get_menu_choice (); printf ("\n1 - Add a Record ");
printf ("You chose %d\n",choice); printf ("\n2 - Change a Record ");
} while(choice!=4); printf ("\n3 - Delete a Record ");
return 0; printf ("\n4 - Quit ");
} printf ("\n\nEnter a selection: ");
scanf ("%d", &selection);
} while ( selection<1 || selection>4);
return selection;
}
Example
Write a program that inputs a number from the user
and checks whether the number is palindrome or not
Example Solution
Do while loop
Unlike for and while loops, which test the loop condition at
the top of the loop, the do...while loop in C programming
checks its condition at the bottom of the loop.
A do...while loop is similar to a while loop, except the fact
that it is guaranteed to execute at least one time.
The body of do...while loop is executed once. Only then, the
test expression is evaluated.
If the test expression is true, the body of the loop is executed
again and the test expression is evaluated.
This process goes on until the test expression becomes false.
If the test expression is false, the loop ends.
Syntax
do
{
Statements;
}
While (condition);
Flow diagram
Example 1
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* do loop execution */
do
{
printf("value of a: %d\n", a);
a = a + 1;
}
while( a < 20 );
return 0;
}
Example 2
Write a program to find an even number input.
Example
Write a program that finds is a number is palindrome.
Its displays original number and the reverse number.
Program counts number of 1’s in that number and
displays that.
Program also subtracts 1 from each unit value and
displays result.