Loop Structures
Loop Structures
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
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.
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
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
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.
#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
3, 9, 27, 81…