The "while" loop stands out as the most basic looping construct in C programming. This looping structure, known as the "while" loop, executes a set of instructions repeatedly as long as a specified condition remains true. The program continues to execute its commands within the "while" loop until the designated termination condition evaluates to false.
Upon reaching the "while" loop, the provided test condition is examined to determine its truth value; if true, the execution flow proceeds to the main body of the loop.
The commands within the main section of the "while" loop will be executed sequentially. The provided test condition and stopping criteria will be reassessed each time the counter updates to initiate the next iteration of the while loop. This sequence will continue until the given condition is evaluated as false. The initial stopping condition defines the behavior of the "while" loop, hence why the while loop in C is referred to as the entry-controlled loop.
Syntax:
while(termination condition)
{
// main body of the "while" loop
}
while( i <= 10) // initialization statement
{
printf("\n Value of I is %d", i) ;
i++ ; // modifying (increment/ decrement statement) counter value
}
Program:
#include "stdio.h"
#include "conio.h"
void main()
{
int n, i, ans; // stating an integer
printf("\n Enter No. to print Table : ");
scanf("%d", &n); // putting a value on the user-entered n
i = 1; // setting the counter value to 1
while(i <= 10) // condition for termination
{
ans = n * i;
printf("\n %d * %d = %d", n,i, ans) ;
i++; // changing the counter value with an increment/decrement statement
}
getch();
}
Output:
Enter No. to print Table : 5
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
For loop:
The "for" loop stands out as the most efficient and versatile looping mechanism in the C language. It empowers us to efficiently tackle any complex tasks within C programming.
Syntax:
for (initialization statement; termination condition; increment/decrement statement)
{
// main body of the "for" loop in C
}
The "for-loop" is made up of three primary components, which are described below:
- Initialization statement,
- Termination condition, and
- Increment or decrement statement .
With the aid of the initialization statement, a counter variable is set up at the start of the loop execution. The termination condition is then reviewed, which is specified in the expression part.
The body of the "for" loop will assume control once the exit condition is met. The instructions inside the "for" loop will then be carried out.
The last stage will entail transferring command to the third segment to adjust the counter value (either by incrementing or decrementing it). Once the value has been altered, the counter value will be evaluated against the termination condition to decide if the command can reenter the "for" loop and proceed with execution.
The described sequence will iterate repeatedly until the termination condition evaluates to false, signaling the conclusion of the process. In such an event, the flow of control will skip the entire "for" loop and proceed to execute the subsequent statement line that follows the "for" loop in cases where the test expression returns a false value.
Example:
for (i=0; i<5; i++)
{
printf("Hello world!!!") // main body of the "for" loop in C
}
Program:
The for loop in this program enables the user to display a multiplication chart for a positive integer.
#include "stdio.h"
#include "conio.h"
void main()
{
int n, a, ans; // variable declaration
printf("\n Enter No. to print Table : ");
scanf("%d", &n); // giving a value to a user-entered field
/* for(initialization condition,
termination condition, increment or decrement condition) */
for(a = 1; a <=10 ; a++ )
{
ans = n * a;
printf("\n %d * %d = %d", n,a, ans) ;
}
getch();
}
Output:
Enter No. to print Table : 8
8 * 1 = 8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80
Conclusion:
In summary, the entry control loop plays a vital role in the C programming language and provides a swift and efficient method for controlling loop execution.