What is Loop in C?
- Siddharth Sharma
- Nov 5, 2024
- 3 min read
"A loop is a control structure that allows you to repeat a block of code multiple times based on certain conditions. Loops are essential for executing repetitive tasks without having to write the same code over and over. In C, there are three main types of loops"
Loops
जब तक एक specified condition पूरी हो जाती है, Loops code के एक block को execute कर सकते हैं।
Loops उपयोगी होते हैं क्योंकि वे समय बचाते ( save time )हैं, त्रुटियाँ कम (reduce errors) करते हैं और code को अधिक पठनीय (more readable) बनाते हैं।
Flow chart of Loop

While Loop
The while loop loops through a block of code as long as a specified condition is true:

In the example below, the code in the loop will run, over and over again, as long as a variable (i) is less than 5:
Example

Note: Do not forget to increase the variable used in the condition (i++), otherwise the loop will never end!
For Loop
When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:
The for loop is used when the number of iterations is known beforehand. It has three main components in its syntax: initialization, condition, and increment/decrement.

Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.
The example below will print the numbers 0 to 5:
Example

Example explained
Statement 1 loop starts होने से पहले एक variable sets करता है (int i = 0)।
Statement 2 loop के चलने की स्थिति को परिभाषित करता है (i को 5 से कम होना चाहिए)। यदि condition true है, तो loop फिर से शुरू हो जाएगा, यदि यह false है, तो loop समाप्त हो जाएगा। Statement 3 प्रत्येक बार loop में code block executed होने पर एक मान (i++) बढ़ाता है।
Another Example
This example will only print even values between 0 and 10:
#include<stdio.h>
int main()
{
for (int i = 0; i == 10; i++) {
printf("%d\n", i);
}
return 0;
}The Do/While Loop
The do/while loop is a variation of the while loop. In this loop, the code block is executed once before the condition is checked, and then the loop is repeated as long as the condition remains true.
“do/while loop, while loop का एक प्रकार है। यह loop एक बार code block को execute करेगा, यह जांचने से पहले कि क्या स्थिति true है, उसके बाद यह loop को तब तक दोहराएगा जब तक कि स्थिति true है।”

#include<stdio.h>
int main()
{
int i = 0;
do {
printf("%d\n", i);
i++;
} while (i < 5);
return 0;
}Break:
It was used to "exit" a switch statement.
The break statement is also capable of exiting a loop.
In this instance, the code exits the for loop when i is equal to 6:

Continue :
The continue statement interrupts the current iteration within a loop when a specific condition is met, then proceeds with the next iteration.
For instance, the following example demonstrates skipping the value of 6:

goto Statement in C :
•C goto statement एक jump statement है जिसे कभी-कभी बिना शर्त (unconditional) jump statement के रूप में भी जाना जाता है।
•goto statement का उपयोग किसी function के भीतर कहीं से भी कहीं भी जाने के लिए किया जा सकता है।


above syntax, में, पहली line compiler को लेबल के रूप में चिह्नित statement पर go to or jump के लिए कहती है। यहां, label एक user-defined identifier है जो target statement को indicates करता है। ‘label' के ठीक बाद आने वाला कथन गंतव्य कथन है। उपरोक्त statement में ‘label:' goto label' statement से पहले भी दिखाई दे सकता है।
Examples:
Type 1: In this scenario, we will encounter a situation akin to what is demonstrated in Syntax1 above. Let's consider a scenario where we have to develop a program to verify whether a number is even or odd and display the result using the goto statement. The program below illustrates how this can be achieved:

Type 2:
Here, we will encounter a scenario akin to what was illustrated in Syntax2 earlier. Let's say we have to create a program that displays numbers from 1 to 10 by utilizing the goto statement. The program below elucidates the process.





Comments