Understand the concepts of loops and functions in C programming language. C programming is a general programming language that is widely used for developing various software. In this article, I will discuss what is C programming for the loop, how to use it, and why it is important. If you’ve ever written code in C, chances are you’ve used a for loop—one of the most fundamental constructs of the language.
We all have a basic understanding of what a loop is, but sometimes we have no idea how to use one effectively. This tutorial is designed to show you how to use a loop in the context of C language. C is a general-purpose programming language used to create applications and games. As a language, it is not restricted to a specific industry. It is, however, commonly used for its efficiency and portability.
Best C Programming for Loops to Learn
Loops are the most powerful programming tools that help us to perform several functions. Here, I am going to discuss the all c programming for loops that will help you to perform multiple tasks.
1. While Loop in C Programming
A while loop is used to execute a particular statement repeatedly until the condition is fulfilled. Here, you have to use a variable called flag to check the condition. While loop is a kind of loop which is used to repeat a specific action at specified intervals. In this article, we will discuss the while loop in C Programming language.
What is the while loop?
The While loop is used to repeat a specific action at specified intervals. Let’s see the different types of while loops.
1. Simple while loop:
Simple while loop is the simplest form of while loop. The code for this type of while loop is given below:
while (condition) {
}
Here, the condition is required to be checked before execution of the loop.
2. While loop with an explicit statement:
In this while loop, a statement is provided after the while keyword. This statement is executed every time the loop is executed. Let’s see an example:
while(condtion) {
statement;
}
For example:
int count = 0;
while(count<10){
printf("\n");
count++;
}
Here, the count variable is incremented in each iteration of the loop.
3. While loop with a Boolean expression:
In this while loop, the Boolean expression is used as the condition. This Boolean expression will be evaluated each time the loop is run. Let’s see an example:
#include
#include
#include
int main() {
int i = 0;
while(i == 1) {
printf("Hello!\n");
printf("I'm will kill you.\n");
i = i + 1;
}
return 0;
}
Here, the expression is evaluated only once. So, the execution of the loop is stopped when the condition is satisfied.
4. While loop with a jump statement:
In this while loop, the condition is not evaluated before the loop is run. This means that the loop will be repeated until the condition is true. The while loop is one of the most common loops in the programming language. It is used for repeating a block of code until a certain condition becomes true. Here is an example of how you can use a while loop with a Boolean expression.
#include
void main() {
int i = 0, j;
j = 1;
while (i < 10) {
if (j == 0)
printf("%d\n", i);
i = i + 1;
}
}
In the above program, we have declared a variable named i. We have also declared a variable named j. We have initialized the value of the variable i as 0 and the value of variable j as 1. We have also used the while loop. The condition is if(j==0) statement.
The control flow starts executing the while loop when the condition is true. After the execution of the while loop, the next instruction is the statement if(j==0).
The control flow is directed to the statement if(j==0) only when the value of variable j is equal to 0. Since the value of the variable j is 1, the control flow is directed to the statement printf(“%d\n”, i).
5. While loop with a jump statement and an explicit statement:
There are two major ways of breaking the while loop. They are the jump statement and the explicit statement. There are two major ways of breaking the while loop. They are the jump statement and the explicit statement.
Jump Statement – The jump statement breaks the while loop. It is used to control the flow of the program. The most important thing is that jump statement is used to break the loop condition or not.
- Jump statement is used to break the loop condition.
- It is very useful for the cases where it is required.
while (x <= 10) {
cout << x << " is less than ten";
x++;
}
Explicit Statement – The explicit statement is used to break the while loop without using the jump statement. It is usually used for the cases where it is needed.
- Explicit statement is used to break the loop condition.
- It is not necessary to use the explicit statement.
while (x <= 10) {
cout << x << " is less than ten";
x++, y++;
}
Now let us understand the difference between the jump statement and the explicit statement and which one is preferred over the other.
For example:
while(count > 0) {
printf("\n");
}
Here, the statement is provided after the jump statement. This statement is executed after the condition is satisfied.
6. While loop with a jump statement and a Boolean expression:
There are many conditions for a while loop, but here I will discuss about the while loop with a jump statement and a boolean expression.
The while loop is the most popular while loop, as it is the easiest to use and understand. It works based on two statements, which are while(conditional) and repeat_statement.
The while loop is used for repeating a specific task until the condition becomes false. The repeat_statement is a conditional statement, which will be executed every time the conditional statement is true. If the conditional statement is false, then the loop will stop and the repeat_statement will not be executed.
Let’s see an example: Create a program to print the number of days of a month.
#include
int main()
{
int d, m;
printf("Enter the number of day of the month: ");
scanf("%d", &m);
printf("\n");
printf("The number of days of the month is %d", m);
printf("\n");
while(m!= 0)
{
d = m / 7 + 1;
printf("\n");
printf("%d", d);
m -= 7 * d;
}
return 0;
}
2. For Loop C Programming
The ‘for loop’ is used in almost all programming languages and its syntax is the same. However, the usage of this loop differs from language to language. Here I am discussing about for loop in C.
What is a for loop?
The for loop is used to repeat the same statement multiple times. It helps to increase the performance of our program. For example, if you have a loop that will run 1000 times, then you will need to perform 1000 operations. If you use a for loop instead of a while loop, then you will only need to perform 1000 operations and not 1000+1000 operations.
For loop is the simplest form of looping that is used to repeat the same statement multiple times. It works as follows:
int i=1; //initialization
for (i = 1; i<=1000; i++)
{
//statements
}
The for loop will repeat the statements until the condition is false. The condition is usually written in the brackets of the for loop. In the above code, the for loop will start from the value of 1 and will go until the condition is false, i.e., i<=1000.
A for loop is used to execute a set of statements or instructions multiple times. In simple words, it is used to run the loop until a specific condition becomes true. The following code is an example of for loop.
int i = 0;
for(i=0; i<10; i++){
printf("%d\n",i);
}
In this code, the for loop is used to print the numbers from 0 to 9. If you see the for loop, then you will be aware that it will run only once. The variable i is used to keep the count of the number of times the loop will execute.
Example:
If you want to write a loop that will print the number 1 to 10, then you need to use the following code.
int i;
for(i=1; i<=10; i++){
printf("%d\n",i);
}
Here, the for loop is used to loop the number 1 to 10.
We can use for loops for a variety of reasons. Some of the common uses are:
- To check the validity of a variable
- To calculate the sum of a series of numbers
- To assign a value to a variable
- To print out the contents of a variable
- To initialize a variable
- To read a file and store its contents into a variable
- To convert an integer to a string and store it in a variable
- To initialize the index of an array
- To copy the elements of a variable to another variable
- To declare a variable
- To set the value of a variable
- To change the value of a variable
- To write a file
- To make a loop in a program
- To control the execution of a program
- To check the validity of a condition
- To exit a loop
- To execute a function
- To exit a program
- To repeat a section of a program
- To make a variable global
- To make a variable local
- To read a file
- To set the value of a variable
- To make a loop
What is the difference between for and while loops?
For loop and while loop are the two basic loops. While loop executes a set of instructions at a time whereas the for loop runs the set of instructions multiple times.
3. Do While Loop C Programming
Do while loop is similar to while loop, but it executes the loop until the condition is fulfilled and then stops. The Do While loop is similar to the for loop except that for the next execution of the code, the value of the condition is checked before starting the block of code.
Do while loop is a basic construct that helps us to repeat a certain block of statements as many times as possible. The syntax of the do while loop is as follows:
do {
Statement1;
Statement2;
} while(condition);
There are two parts in the above syntax, first is do statement and second is while statement. The do statement is used for repeating the block of statements and the while statement checks the condition before executing the block of statements.
We will write a simple program to display the number of times the ‘do while loop’ is repeated.
#include
int main() {
printf("Number of times loop repeats = %d", count_do_while_loop());
return 0;
}
void count_do_while_loop(){
int i;
for(i=0;i<5;i++)
printf("%d",i);
printf("\n");
}
Here, is another example
#include
int main()
{
int i, j, k;
do
{
printf("%d\n", i);
} while(i < 5);
return 0;
}
In the above example, the first line has a declaration of the variable ‘i’ of type int. The second line is the statement for the loop. The third line has an assignment to the variable ‘j’ of type int. The fourth line contains the condition for the loop. The fifth line is the loop body which is a block of statements. The sixth line is the end of the loop and the seventh line is the return statement.
Do While loop is a combination of the while loop and the for loop.
#include
int main()
{
int i, j, k;
for (i = 0; i < 5; i++)
{
printf("%d\n", i);
}
do
{
printf("%d\n", i);
} while(i < 5);
return 0;
}
Here, the program will execute the block of code in the for loop and check whether the condition is true or false. If it is true, the next statement will be executed. If it is false, the loop will be ended.
Do While loop is a combination of the while loop and the for loop.
4. Until Loop C Programming
Until loop is used to execute a block of code and then stop executing it once the condition is satisfied.
The loop is a fundamental part of the programming language in which a program repeats an instruction as many times as specified. The loop is a mechanism that can repeat a block of code multiple times. You can also say that the loop is used to execute a certain code block for multiple times. In this tutorial, I will teach you how to use the until loop in c programming.
What is until loop?
Until loop is a special type of loop in which a condition must be satisfied before the loop is exited. This loop is commonly used in case-control structure. The until loop can be written as follows:
while(condition)
{
code to be executed
}
until(condition)
{
code to be executed
}
Example: Let’s consider an example that will show you how to use the until loop in c programming.
#include
#include
int main()
{
int i=0;
printf("Enter the number of times you want to repeat the loop\n");
scanf("%d",&i);
while(i>=1) {
printf("The value of i is %d",i);
i--;
}
5. For Each Loop C Programming
The for each loop is similar to for loop, but it iterates through the collection and it is very useful when you have to access the elements of a collection. Each Loop in C programming language is different, but they are mostly used to run a loop for a certain number of times. The condition that decides the loop will be executed is specified by the loop control variable.
The basic concept of a for loop is it will loop through each element of an array, the syntax is given below:
for( int i = 0 ; i < array.size() ; i++ )
{
//code goes here
}
So, as you can see that the for loop starts with the keyword for, followed by the condition of the loop. After that, you will have to write the loop body, and finally, after writing the loop body, you need to write the condition of the loop.
Let us see the sample code for understanding the concept.
#include
int main()
{
int a[10],i,j;
printf("Enter 10 elements\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("The elements are:\n");
for(i=0;i<10;i++)
printf("%d ",a[i]);
return 0;
}
Output: Enter 10 elements
2 1 4 5 6 7 8 9
The elements are: 2 1 4 5 6 7 8 9
Another Example:
#include
int main()
{
int i=5;
for(i=0;i<10;i++)
printf("The loop will execute %d times\n",i);
return 0;
}
6. Continue Statement in C programming
Continuing statement is used in C programming language to execute a particular block of code even when the flow is switched to another statement. In continuation, we are talking about the next execution after the execution of the current statement.
Let’s take an example that explains what continue statement does. The main() function has two statements and after executing the second statement it is required to go back to the first statement. So, it is used to return to the execution of the current statement.
The continue statement is defined as a keyword and its syntax is given below:
continue [label]
Syntax:
continue label
Example:
int x=10;
if(x==5)
{
//code to be executed
continue;
}
else
{
//code to be executed
continue;
}
Continue statement is used to continue the execution of a loop until it reaches the end of the loop. If a statement is not present inside the loop then the execution will be stopped after it reaches the end of the loop. Continue statement is used in case of an error while executing a loop. There are various types of loops that are used in C programming language. In the below given figure, you can see a sample code of the for loop.
for ( i = 0; i < 10; i++ )
{
/**Statement 1**/
}
If any error occurs in the above code then the execution will stop at the point where the error has occurred. If the execution of the above code is not stopped then the continue statement will be used.
Continue statement is not a part of any programming language. It is used in C programming language. The syntax of the continue statement is as follows:
Continue statements can be placed inside the loop or can be placed at the end of the loop. For example:
for ( i = 0; i < 10; i++ ) {
//statement 1
}
for ( i = 0; i < 10; i++ ) {
//statement 2
}
for ( i = 0; i < 10; i++ ) {
if( i == 10)
//statement 3
else
//statement 4
}
If the statement 3 is not present then the execution will be stopped after the end of the loop.
Goto Statement in C programming
GOTO statement is one of the most important features of the C programming language. There are a lot of people who don’t understand its working. The C program is divided into statements and statements are grouped into blocks. The C program is also divided into declarations and main function.
There are three types of statements in C programming and they are:
- Conditional
- Repeat
- Break
The conditional statement is used to compare the values of two variables. If the value of variable A is greater than the value of variable B then the conditional statement will execute the code written in the block.
Let’s see an example.
#include
main() {
int a, b;
printf("Enter the values of A and B");
scanf("%d %d",&a, &b);
if(a>b) {
printf("a is greater than b");
} else {
printf("b is greater than a");
}
return 0;
}
Explanation:
The above program is divided into three parts: the main function, conditional statement and block.
The main function is the first thing executed when the program starts. In the above program, the main function is the same as the name of the program.
The conditional statement is the most important thing in the C programming language. Here we can write any condition. Here we can use the conditional statement for comparing the values of two variables. The conditional statement is also called as if-else statement.
The block is a group of statements. In the above program, the statements are separated by the curly braces.
Let’s see another example.
#include
main() {
int a, b;
printf("Enter the values of A and B");
scanf("%d %d",&a, &b);
if(a>b) {
printf("a is greater than b");
} else {
printf("b is greater than a");
}
return 0;
}
The above program is divided into three parts: the main function, conditional statement and block.
The main function is the first thing executed when the program starts. In the above program, the main function is the same as the name of the program.
The conditional statement is the most important thing in the C programming language. Here we can write any condition. Here we can use the conditional statement for comparing the values of two variables. The conditional statement is also called as if-else statement. The block is a group of statements. In the above program, the statements are separated by the curly braces.
There are a lot of things that are associated with the C programming language, but the most important thing is that you must know how to use GOTO statement. There are a lot of people who don’t know the importance of this statement in the C programming. So, the C programmers must learn the usage.
Conclusion:
These are the best c programming language for loops that will help you to become a professional programmer. So, try to practice these best c programming for loops and you will surely be able to solve any coding problem easily.