What is Looping in C & C++?
The looping can be defined as repeating the same process multiple times until a specific condition satisfies. It is known as iteration also.
- There are three types of loops used in the C,C++ language language.
- We are going to learn all the aspects of C, C++ loops.
Why looping?
The looping simplifies the complex problems into the easy ones. It enables to alter the flow of the program so that instead instead of writing writing the same code again and again, we can execute the same code for a finite number of times. For example, if we need to print ‘Bhopal’ 10-times then, instead of using the printf/cout statement 10 times, we can use printf in c and cout in C++ once inside a loop which runs up to 10 iterations.
What are the advantages of Looping?
- It provides code reusability.
- Using loops, we do not need to write the same code again and again.
- Using loops, we can traverse over the elements of data structures structures (array or linked lists).
Types of Loops
There are two type of loop in C and C++ Prog. language those are given below:
Entry Loop
- for
- while
Exit Loop
- do while
Essential components of a loop:-
- Counter
- Initialization of the counter with initial value
- Condition to check with the optimum value of the counter
- Statement(s) to be executed by iteration
- Increment/decrement
for loop in C and C++
The for loop in C and C++ language is used to iterate the statements or a part of the program several times. It is frequently used to traverse the data structures like the array and linked list.
- The syntax of for loop in c language is given below:
for (Expression1; Expression2; Expression3)
{
codes to be executed;
}
Expression 1 (Optional)
- Represents the initialization of the loop variable.
- More than one variable can be initialized.
Expression 2
- Expression 2 is a conditional expression. It checks for a specific condition to be satisfied. If it is not, the loop is terminated.
- Expression 2 can have more than one condition. However, the loop will iterate until the last condition becomes false. Other conditions will be treated as statements.
Expression 3
- Expression 3 is increment or decrement to update the valu of the loop variable
while loop in C
- The while loop in C and CPP is to be used in the scenario where the block of statements is executed in the while loop until the condition specified in the while loop is satisfied. It is also called a pre-tested loop.
- The syntax of while loop in c language is given below:
initialization;
while(condition)
{
block of statements to be executed ;
increment;
}
do-while loop in C
- The do-while loop continues until a given condition satisfies. It is also called post tested loop. It is used when it is necessary to execute the loop at least once (mostly menu driven programs)
- The syntax of The syntax of do-while loop in c language loop in c language is given below:
do
{
code to be executed ;
}
while(condition);
__________________________________________________________
Example of Loop in C Programming:
Program to print natural numbers 1 to 15 using for loop in C and CPP.
main()
{
int i;
for(i=1;i<=15;i=i+1)
{
printf("%d, ", i); // for C Labgungae
}
}
_______________________________________
main()
{
int i;
for(i=1;i<=15;i=i+1)
{
cout<<i<<endl; // for C++ prog language
}
}
-------------------------------------------------------------
output
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
-------------------------------------------------------------
Program to print table for the given number using while loop in C
#include<stdio.h>
main()
{
int i=1,number,b;
printf("Enter a number: ");
scanf("%d",&number);
while(i<=10)
{
b=number*i;
printf("%d \n", b);
i=i+1;
}
}
-------------------------------------------------------------
Output
Enter a number: 5
5
10
15
20
25
30
35
40
45
50
-------------------------------------------------------------
Program to print natural numbers 1 to 10 using do while loop.
#include<stdio.h>
main()
{
int i=1;
do
{
printf("%d \n",i);
i=i+1;
}
while(i<=10);
}
-------------------------------------------------------------
Output
1 2 3 4 5 6 7 8 9 10
-------------------------------------------------------------
#Happy_Codding with #MrAJ
Comments
Post a Comment