0% found this document useful (0 votes)
70 views24 pages

CHAPTER 5-LOOPING STATEMENTS - Dis2019

The document discusses different types of looping statements in C programming. It describes while, do-while, for, nested, continue, and break statements. The key types of loops are: 1) While loops execute a block of code repeatedly until a condition is false. 2) Do-while loops are similar but check the condition at the end of each iteration. 3) For loops allow initialization of a counter, condition to check on each pass, and increment/decrement of the counter. 4) Nested loops allow a loop to execute within the body of another loop. 5) Continue and break statements allow skipping the current iteration or exiting the loop entirely.

Uploaded by

Caroline Dame
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views24 pages

CHAPTER 5-LOOPING STATEMENTS - Dis2019

The document discusses different types of looping statements in C programming. It describes while, do-while, for, nested, continue, and break statements. The key types of loops are: 1) While loops execute a block of code repeatedly until a condition is false. 2) Do-while loops are similar but check the condition at the end of each iteration. 3) For loops allow initialization of a counter, condition to check on each pass, and increment/decrement of the counter. 4) Nested loops allow a loop to execute within the body of another loop. 5) Continue and break statements allow skipping the current iteration or exiting the loop entirely.

Uploaded by

Caroline Dame
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 24

DEC20012

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.

Types of Looping Statement :


while
do-while
for
nested for
Difference between Conditional and Looping statement?
Conditional statement executes only once in the program;
where as looping statements executes repeatedly several number of time.

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

Syntax: for (expression1; expression2; expression3)


statement;

Example: Initial value Loop continuation value


int i; incremental value
for (i = 0; i < 5; i++)
{
printf(“%d ”, i);
}

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 ;

for (i=0; i<=10; i++)


{
printf (“Hello %d\n, 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;

for (k=-5; k<25; k=k+5)


{
printf ("%d",k);
printf ("Good Morning\n");
}
return 0;
}
EXAMPLE CODE - CONTINUE
#include<stdio.h>
 OUTPUT:
int main ()
{
int k=-5;
while (k<25)
{
k=k+5;

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;

for (k=-5; k<25; k=k+5)


{
printf ("%d",k);
break;
printf ("Good Morning\n");
}
return 0;
}
GOTO STATEMENTS
 goto statement:
 When a goto statement is encountered in a C
program, the control jumps directly to the label
mentioned in the goto statement
GOTO STATEMENT

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”);
} }

Both Codes Same Output

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

Thank You Guys

DEC2012 FundamentalProgramming
EC201 Fundamental Programming

You might also like