what is loops (Iteration):-
Iteration is nothing but repetition of one or more statement in c language .looping construct are used to repeat one or more statement again and again until some condition is met .C language provides following three types of loops.
- While
- do-while
- for
' while ' and ' do-while ' are the Non deterministic loops or indeterminate loops and the ' for ' loop is called Deterministic loop.
Entry Controlled loops:-
In this loop condition is check before entering the loop.
ex:- while and for loop.
Exit controlled loops:-
In exit control loop the loop condition is check at the exit of loop.
ex:- do-while.
1) For() :-
-------------------------
-------------------------
for( initialization , condition ,increment/ decrement )
{
first statement
-----------------
-----------------
last statement
}
next statement;
------------------
------------------
Example:- first initialization is executed . after initialization condition is checked if it is true then the statement written in the body of loop will be executed . after execution of all the statement increment / decrement will be executed. then again condition will be checked. if it is true then flow of control goes inside the loop otherwise the statements after the loops will be executed.
example:-
#include<stdio.h>
int main()
{
int i;
for(i=0;i<5;i++)
printf("Hello world!\n");
return 0;
}
Output:-
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
2) while()
----------------------
----------------------
Initialization;
while(condition)
{
first statement;
------------------
------------------
increment / decrement;
}
next statement
------------------
------------------
Explanation:- first we initialize the variable then after check the condition of the loop if condition is true then statement written in the body of the loop will be executed . and after execution of the statement increment / decrement will be executed then again condition will be checked if it is true then flow of control goes inside of the loop and if condition is false then the statements after the while loop will be executed.
example:-
#include<stdio.h>
int main()
{
int i;
//initialization
i=0;
while(i<5) // condition
{
printf("Hello world!\n");
i++; // increment/decrement
}
return 0;
}
Output:-
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
3) do-while()
------------------
-------------------
initialization;
do
{
first statement;
-----------------
-----------------
increment/ decrement
}while(condition);
next statement;
-------------------
--------------------
Explanation:- first we initialize the variable and then start the execution of the statements within the loop and after execution of all the statement increment / decrement will be executed and this condition written in the while loop will checked if it is true then again flow of control goes inside of the loop and all the process will be repeated. and if it is false then the statement next to the loop will be executed.
example:-
#include<stdio.h>
int main()
{
int i;
//initialization
i=0;
do
{
printf("Hello world!\n");
i++; // increment/decrement
}while(i<5);// condition
return 0;
}
Output:-
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Recommended Post:
Hackerearth Problems:-
- Very Cool numbers | Hacker earth solution
- Vowel Recognition | Hackerearth practice problem solution
- Birthday party | Hacker earth solution
- Most frequent | hacker earth problem solution
- program to find symetric difference of two sets
- cost of balloons | Hacker earth problem solution
- Chacha o chacha | hacker earth problem solution
- jadu and dna | hacker earth solution
- Bricks game | hacker earth problem
- Anti-Palindrome strings | hacker earth solution
- connected components in the graph | hacker earth data structure
- odd one out || hacker earth problem solution
- Minimum addition | Hackerearth Practice problem
- The magical mountain | Hackerearth Practice problem
- The first overtake | Hackerearth Practice problem
Hackerrank Problems:-
- Playing With Characters | Hackerrank practice problem solution
- Sum and Difference of Two Numbers | hackerrank practice problem solution
- Functions in C | hackerrank practice problem solution
- Pointers in C | hackerrank practice problem solution
- Conditional Statements in C | Hackerrank practice problem solution
- For Loop in C | hackerrank practice problem solution
- Program to find cycle in the graph
- Implementation of singly link list
- Implementation of queue by using link list
- Algorithm of quick sort
- stack by using link list
- program to find preorder post order and inorder of the binary search tree
- Minimum weight of spanning tree
- Preorder, inorder and post order traversal of the tree
- How to set limit in the floating value in python
- What is boolean data type
- How to print any character without using format specifier
0 Comments