Professional Documents
Culture Documents
Fundamental Programming
TOPIC 5:
Looping Statements
What is looping statements?
Looping, a program executes the sequence of statements many times until
the stated condition becomes false. A loop consists of two parts, a body of
a loop and a control statement.
The control statement is a combination of some conditions that direct the
body of the loop to execute until the specified condition becomes false.
Nested Loop :
In Nested Loop one loop is place within another loop body.
When we need to repeated loop body itself n number of times use
nested loops.
WHILE STATEMENT
Syntax:
while (expression)
statement;
Example:
int i = 0;
while(i < 5){
printf(“%d “, i);
i++;
}
Output: 0 1 2 3 4
DO…WHILE STATEMENT
Syntax: do
statement;
while (expression);
Example:
int i = 0;
do {
printf(“%d ”, i);
i++;
} while(i < 5);
Output: 0 1 2 3 4
FOR STATEMENT
Output: 0 1 2 3 4
NESTED LOOP STATEMENT
Example:
for(i=1; i<= 10; i++)
{ /* outer loop */
printf("%4d", i);
for(j=1; j<=i; j++)
{ /* inner loop */
printf("%4d", i*j);
}
printf("\n");
}
EXAMPLE: DISPLAY “HELLO” USING WHILE
LOOP
#include<stdio.h>
int main()
{
int i =10;
while (i>0)
{
printf (“Hello %d\n, i”);
i = i – 1;
}
return 0;
}
EXAMPLE: DISPLAY “HELLO” USING DO -
WHILE LOOP
#include<stdio.h>
int main()
{
int i =10;
do{
printf (“Hello %d\n, i”);
i = i – 1;
} while(i>10);
return 0;
}
EXAMPLE: DISPLAY “HELLO” USING FOR
LOOP
#include<stdio.h>
int main()
{
int i ;
return 0;
}
EXAMPLE: FINDS THE TOTAL OF 4
NUMBERS USING WHILE LOOP
#include<stdio.h>
void main()
{ int count =1,num,total = 0;
while (count <=4)
{
printf(“\nEnter a number:”);
scanf(“%d”,&num);
total = total + num;
printf(“The total is now=%d”,total);
count++;
}
}
CONTINUE AND BREAK STATEMENTS
continue statement:
The continue statement is used inside loops.
When a continue statement is encountered inside
a loop, control jumps to the beginning of the loop
for next iteration, skipping the execution of
statements inside the body of loop for the current
iteration
CONTINUE STATEMENT
SYNTAX FLOWCHART
continue;
EXAMPLE CODE
#include<stdio.h>
OUTPUT:
int main ()
{
int k;
if (k==10)
continue;
printf ("%d",k);
printf ("Good Morning\n");
}
return 0;
}
CONTINUE AND BREAK STATEMENTS
break statement:
Itis used to come out of the loop instantly. When a
break statement is encountered inside a loop, the
control directly come out of loop and the loop gets
terminated
BREAK STATEMENT
SYNTAX FLOWCHART
break;
EXAMPLE CODE - BREAK
#include<stdio.h>
OUTPUT:
int main ()
{
int k;
SYNTAX FLOWCHART
goto label_name;
..
..
label_name: C-statements
EXAMPLE CODE - GOTO
#include<stdio.h>
OUTPUT: a=10
int main ()
{
int a=10;
LOOP:
do{
if (a==15)
{
a=a+1; OUTPUT: a=15
goto LOOP;
}
printf ("value of a:%d\n",a);
a++;
}while (a<20);
return 0;
}
WHILE WITH BREAK - EXERCISE
#include<stdio.h> #include<stdio.h>
void main() void main()
{ {
int j =50; int j =50;
while (j < 80) while (j < 80)
{ {
j += 10; j += 10;
if (j == 70) if (j == 70)
{ {
break; }
} printf(“j = %d\n”,j);
printf(“j = %d\n”,j); break;
} }
printf(“We are out of the loop.\n”); printf(“We are out of the loop.\n”);
} }
j = 60
We are out of the loop.
WHILE - HOMEWORK
Write a C program code to enter 1 number and
then computes the factorial for that number. Your
code must use while statement.
???
End of Topic 5
DEC2012 FundamentalProgramming
EC201 Fundamental Programming