0% found this document useful (0 votes)
12 views

Loop Structures

The document explains the concept of loops in C programming, detailing their purpose and structure, including three types: for, while, and do-while loops. It provides syntax, examples, and explanations of how each loop operates, along with the use of jump statements like break, continue, and goto. Additionally, it includes sample programs demonstrating the calculation of factorials and the printing of numbers.

Uploaded by

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

Loop Structures

The document explains the concept of loops in C programming, detailing their purpose and structure, including three types: for, while, and do-while loops. It provides syntax, examples, and explanations of how each loop operates, along with the use of jump statements like break, continue, and goto. Additionally, it includes sample programs demonstrating the calculation of factorials and the printing of numbers.

Uploaded by

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

What is a Loop?

A Loop executes the sequence of statements repeatedly 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. The purpose of the loop is to repeat the same
code a number of times.
Types of Loops in C
The C programming language provides us with three types of loop
constructs:
1. The while loop
2. The do-while loop
3. The for loop
The for loop
This is a deterministic loop in which the number of iterations of
such a loop are known in advance, even before the loop has
started.

Syntax of for loop:


for (initialization; condition test; increment or decrement)
{
//Statements to be executed repeatedly
}
How for loop works?
 The initialization statement is executed only once.
 Then, the condition test expression is evaluated. If the test
expression is evaluated to false, the for loop is terminated.
 However, if the condition test expression is evaluated to true,
statements inside the body of for loop are executed, and the
increment or decrement expression is performed.
 Again the condition test expression is evaluated.
 This process goes on until the condition test expression is false.
When the condition test expression is false, the loop terminates.
for loop Flowchart
Example: Write a program that prints numbers from 1 to 10

// Print numbers from 1 to 10


#include <stdio.h>
int main()
{ int i;
for (i = 1; i < 11; ++i)
{
printf("%d ", i);
}
return 0;
}

Output
1 2 3 4 5 6 7 8 9 10
Example: Write a program that calculates the factorial of a number
entered by the user

Factorial of n is the product of all positive descending integers. Factorial of n


is denoted by n!.

For example:

1. 5! = 5*4*3*2*1 = 120

2. 3! = 3*2*1 = 6
1.#include<stdio.h>
2.int main()
3.{
4. int i,fact=1,number;
5. printf("Enter a number: ");
6. scanf("%d",&number);
7. for(i=1;i<=number;i++){
8. fact=fact*i;
9. }
10. printf("Factorial of %d is: %d",number,fact);
11.return 0;
12.}

Output:
Enter a number: 5
Factorial of 5 is: 120
while loop
A while loop in C programming repeatedly executes a target
statement as long as a given condition is true.

Syntax

while(condition)
{
statement(s);
}
Here, statement(s) may be a single statement or a
block of statements. The condition may be any
expression, and true is any nonzero value.

The loop iterates while the condition is true.

When the condition becomes false, the program


control passes to the line immediately following the
loop.

It is important to note is that being a top tested


loop, the loop statements may 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 value of a:
Output:
11
#include <stdio.h> value of a:
int main () 12
{ value of a:
/* local variable definition */ 13
int a = 10; value of a:
/* while loop execution */ 14
while( a < 20 ) value of a:
{ 15
printf("value of a: %d\n", a); value of a:
a++; 16
} value of a:
return 0; 17
} value of a:
18
value of a:
19
do while loop
A do…while loop in C programming repeatedly executes a target
statement as long as a given condition is true.

Syntax
do
{
//Statements
}
while(condition test);
Notice that the conditional expression
appears at the end of the loop, so the
statement(s) in the loop executes once before
the condition is tested. This is called a
bottom tested loop
If the condition is true, the flow of control
jumps back up to do, and the statement(s) in
the loop executes again. This process
repeats until the given condition becomes
false.
Example: Write a program that Calculates the factorial of a number
#include<stdio.h> Working:
int main()
{ • First the computer reads the
int n,i=1,f=1;
printf("\n Enter The Number:");
number to find the factorial of
scanf("%d",&n); the number from the user.

//LOOP TO CALCULATE THE FACTORIAL OF A NUMBER • Then using do-while loop the
do value of ‘i’ is multiplied with
{ the value of ‘f’.
f=f*i;
i++;
• The loop continues till the
}while(i<=n);
value of ‘i’ is less than or
printf("\n The Factorial of %d is %d",n,f); equal to ‘n’.
return 0
} • Finally the factorial value of
the given number is printed.
How the code works Step by Step
2.3. do
Let us assume that the number entered by the user is 5.
f=f*i (f=2*3) So f=6
1.It assigns the value of n=5 , i=1 , f=1
i++ (i=i+1) So i=4
2.Then the loop continues till the condition of the do-
while loop is true. i<=n (4<=5) , do-while loop condition is true.

2.1. do 2.4. do

f=f*i (f=1*1) So f=1 f=f*i (f=6*4) So f=24

i++ (i=i+1) So i=2 i++ (i=i+1) So i=5

i<=n (2<=5) , do-while loop condition is true. i<=n (5<=5) , do-while loop condition is true.

2.2. do 2.5. do

f=f*i (f=1*2) So f=2 f=f*i (f=24*5) So f=120

i++ (i=i+1) So i=3 i++ (i=i+1) So i=6

i<=n (3<=5) , do-while loop condition is true. i<=n (6<=5) , do-while loop condition is false.
2.6. It comes out of the do-while loop.
3.Finally it prints as given below
The Factorial of 5 is 120
Jump Statements in C
Jump Statement makes the control move to another section of the program
unconditionally when encountered. It is usually used to terminate
the loop or switch-case instantly. It is also used to escape the execution of a
section of the program.
1. Break Jump Statement

A break statement is used to terminate the execution of the rest of the block
where it is present and takes the control out of the block to the next
statement.

It is mostly used in loops and switch-case to bypass the rest of the


statement and take the control to the end of the loop.

Another point to be taken into consideration is that the break statement


when used in nested loops only terminates the inner loop where it is used
and not any of the outer loops.

Let’s implement an example to understand how break statement works in C


language.
1. Break Jump Statement

#include <stdio.h>
int main()
{
int i;
for (i = 1; i <= 15; i++)
{
printf("%d\n", i);
if (i == 10)
break;
}
return 0;
}
2. continue Jump Statement
The continue jump statement like any other jump statement
interrupts or changes the flow of control during the execution of a
program. Continue is mostly used in loops.
Rather than terminating the loop it stops the execution of the
statements underneath and takes control to the next iteration.
Similar to a break statement, in the case of a nested loop, the
continue passes the control to the next iteration of the inner loop
where it is present and not to any of the outer loops.
2. continue Jump Statement
// continue loop example
#include <stdio.h>
int main ()
{
for (int n=10; n>0; n--)
{
if (n==5) continue;
printf(“%dn , “,n);
}
printf("FIRE!\n“);
return 0;
}
Output
10, 9, 8, 7, 6, 4, 3, 2, 1, FIRE!
3. goto Jump Statement
goto jump statement is used to transfer the flow of control to any part of the
program desired. The programmer needs to specify a label or identifier with
the goto statement in the following manner:

goto label;

This label indicates the location in the program where the control jumps to.
Let’s implement an example to understand how goto statement works in C
language.
3. goto Jump Statement
#include <stdio.h>
int main()
{
int i, j;
for (i = 1; i < 5; i++)
{
if (i == 2)
goto there;
printf("%d\n", i);
}
there:
printf("Two");
return 0;
}
1. Write a program which input principal, rate and time from user and
calculate compound interest. The program should also display the year
by year interests

2. Write a C Program to find Sum of the following Geometric Progression


Series while at the same time displaying the terms in the series.

3, 9, 27, 81…

You might also like