×

Loop Statement in C

Loop statement is used to execute one or more statement repeatedly multiple times. There are three types of loops in C language. Why use loop? We can use loop because it executes a block of code multiple times and it reduces the length of the code. Example:  Let suppose we have to print counting 1to 50 then we have to use 50 times lines of code but if we use loop then print it by 2 or 5 lines of code.
  • for loop
  • while loop
  • do while loop
Difference between conditional and looping statement: The conditional statement executes only once in the program but looping statements executes repeatedly several number of times. for Loop The for loop statement is used to execute the code until a condition is false. In this section, there are 3 parts initialization, condition and increment or Decrements. 

   

The following syntax of for Loop:
for(initialization;condition;incr/decr)
{
//code to be executed
}
 Example1
#include<stdio.h>
int main(){
int i;
 for(i=0;i<=5;i++){
 printf("%d \n", i);
 } 
 return 0;
}
Output
0 
1 
2 
3 
4 
5
Example 2 //To print any childish table of any number
#include<stdio.h>
#include<stdlib.h>
int main(){
int n,temp,i;
printf("Enter any number for Table :");
scanf("%d",&n);
for(i=1;i<=10;i++){
 temp=n*i;
printf("%d * %d = %d \n",n,i,temp);
}
return 0;
}
Output
Enter any number for Table :32764 * 1 = 32764 
32764 * 2 = 65528 
32764 * 3 = 98292 
32764 * 4 = 131056 
32764 * 5 = 163820 
32764 * 6 = 196584 
32764 * 7 = 229348 
32764 * 8 = 262112 
32764 * 9 = 294876 
32764 * 10 = 327640
While Loop
While loop is an entry-controlled loop statement. The basic format of the while statement is:

Syntax:
while( test condition{
body of loop;
}
Or
Assignment;
while(condition)
{
Statements;
......
Increment/decrements (++ or --);
}
    

Note: While loop is used when the number is uncertain or unknown.

Example:
#include<stdio.h>
int main(){
printf("To Print Counting by using While Loop \n");
int i=1;
while(i<5){
printf("\n%d",i);
i++;
}
 return 0;
}
Output
To Print Counting by using While Loop 

1
2
3
4
Do while Loop
Do while loop is used to execute the program. In do while loop, the first part of the execution is body then evaluate the text condition. If the condition is true, the body of the loop is executed again and again until the condition becomes false.

Syntax:
do{
Statement(s);
}while(condition);
Let us consider the flow chart of the C program 

   

Let us consider an example:
#include<stdio.h>
int main(){
 int a=1;
 do{
 printf("Couning: %d\n",a);
  a=a+1;
 }while(a<5);
return 0;
}
Output
Couning: 1
Couning: 2
Couning: 3
Couning: 4

Related Topics

Branching Statements in C

What is branching in c? Branching gets its name from the fact that the computer can select which branch to follow.Programs written in the C language execute statements one after the...

6 minutes read.

Projects on C language in 2024

Introduction Most aspiring programmers learn the C language as their first high-level programming language. The most flexible language utilized in every industry is, without a doubt, C. Even after 50 years...

9 minutes read.

SJF Scheduling Program in C

The SJF (shortest job first) or the shortest job next is the programming scheduling in the C. It is one of the CPU scheduling programming. The SJF is defined as...

4 minutes read.

Quick sort in C

Quick-sort, in the same way, like a merge sort, follows the principle of the divide and conquer algorithm. It then picks an element as pivot and partitions, and the given...

4 minutes read.

Keywords in C

A keyword is a word that has a predetermined meaning. The C compiler knows the purposes of the keywords. The objectives of these keywords cannot be modified. Keywords are the...

9 minutes read.

Derived Data Types in C

A variable in a program occupies some space in the computer's memory where some value is stored. Each variable in C has an associated data type. A value to be...

4 minutes read.

Consumer billing system in C

Introduction : Billing has always been taught to do perfectly, we know today's world is full of products, and We all are using multiple products. We are buying and selling various...

9 minutes read.

CRC Program in C

CRC (Cyclic Redundancy Check) is an error-detection algorithm that is used to detect any errors that may have occurred during the transmission or storage of data. The basic idea behind...

4 minutes read.

Associativity of Operators in C

What is an Associativity operator? Two operators with equal priority are connected by employing their association when they are present in an expression. There are two different types of associativity: left to rightright...

4 minutes read.

Dynamic memory allocation

Dynamic memory allocation is used to allocate the memory at runtime. It has following function within <stdlib.h> header file. Function Syntax malloc() malloc (number *sizeof(int)); calloc() calloc (number, sizeof(int)); realloc() realloc (pointer_name, number * sizeof(int)); free() free (pointer_name); malloc() function The process...

2 minutes read.

How to convert a string to hexadecimal in C

Converting a character array or any string to its respective hexadecimal form is simple. The only thing we have to do is to follow the below steps. Take each character from...

3 minutes read.

Find occurrence of substring in C using function

Introduction: In this article, we discuss finding the occurrence of a substring in C using function. This software takes strings and substrings as input and counts the occurrences of substrings within...

3 minutes read.

Difference between Array and List in C

C# Array vs List is where the abstraction and implementation of human computing meet.  Arrays are incredibly related to the hardware concept of contiguous and contiguous memory, where each part is...

3 minutes read.

Random Access - Lseek in C

Introduction lseek is a system call that is used to alter the location of the read/write pointer of a file descriptor. The lseek system call basically moves the current read/write location...

4 minutes read.

Error handling in C

Error handling in C: The C programming standard does not provide direct convenience for handling the errors. However, being a system programming language, it definitely will give access to handling...

4 minutes read.

fgets() function in C

fgets() function is present in standard input and output library i.e, stdio.h library. It is a built-in function or pre-define function. It is used to read the specified stream from...

4 minutes read.

Pascal Triangle in C

The pascal triangle in c is an array of binomial coefficients in triangular form. Here the nth row contains the binomial coefficient of ncr.In a pascal triangle, every number is...

2 minutes read.

#undef in C

#undef in C In the C programming language, the #undef directive is used to undefine the macro or any constant, which will be defined by the preprocessor directive #define. The #undef...

3 minutes read.

C Data type

Data Types in C with Examples Data types are used to define the type of data that a variable can store to perform a specific operation.ANSI C provides three types of...

2 minutes read.

Advantages of Dynamic Memory Allocation in C

Dynamic Memory Allocation Dynamic memory allocation is a process of assigning or allocating memory to a variable during the execution of a program. Dynamic memory allocation provides storage according to the...

3 minutes read.