In this tutorial, we will discuss Loops in C programming language
In some cases, if you want to execute a block of the statement multiple times according to a condition, such case loops are used to perform this task.
Loop statement in programming languages is a feature which helps to execute a block of instruction(continuous instruction or task) repeatedly while some conditions and evaluate until becomes false.
Loops in C programming language
C language contains some loops which are functioning similarly. it used performing some continuous task until the given condition is fulfilled.
We will discuss in this part, all Loops in C programming language
1) While loop – A “while loop” is a loop statement (control flow statement). It allows the code( loop control ) to be executed repeatedly to perform based given Boolean expression to a certain number of time.
Syntax
initilaization; while(boolean_Expression) { //loop body looping statements increment/decrement statements }
Flowchart
First the “while loop” starts with the initialization of statement, then it checks the Boolean expression. If it is evaluated as true then the loop body of statements are executed. If it is evaluated as false, the loop control exits from the loop body and moves to the end of the loop.
When the loop is evaluated true. the body of statements are executed and the loop control goes to updating statements and then, test boolean expression is evaluated again to executed the body of the statement.
This process is followed until the boolean expression returns false.
Example
//Java program to illustrate while loop class Demo_While{ public static void main(String args[]){ int i=0;//declare variable "i" and initialize as zero while(i<=5){ System.out.println("Value of i: "+i); //this statement execute until this condition(become greater than 5) performed i++; //increment the value of i for next iteration } } }
When the above code is executed, it produces the following result
value of i=0 value of i=1 value of i=2 value of i=3 value of i=4 value of i=5
Know more about this
Nested while loop in C language
2) for loop – The for loop is a loop statement. It allows their code (flow of control) to be executed repeatedly to perform based given boolean expression for the certain number of time., as same as while loop but with some differences.
Although for loop and while loop both have same loop control, but the for loop structure distinguished with the while loop.
In the for loop, initialization statements, boolean expression, updating statements are defined as the same line
Syntax
for(initialization_statements; boolean_Expression; increment/decrement){ //loop body looping statements }
Flowchart
First, the “for loop” starts with the initialization statement. and then checks the boolean-expression. If it is evaluated as true then the loop body of statements are executed or If it evaluated as the false loop control exits from the loop body and goes to the end of the loop.
When the loop is evaluated true. the body of statements is executed, then the flow of control goes for updating statements and test boolean expression is evaluated again to executed the body of the statement.
This process follows until the boolean expression returns false.
Example
//Java program to illustrate for loop class Demo_For1{ public static void main(String args[]){ int i;//declare variable "i"; for(i=0; i<=5; i++){ System.out.println("Value of i: "+i); //this statement execute until this condition(become greater than 5) performed //increment the value of i for next ikteration } } }
When the above code is executed, it produces the following result
value of i=0 value of i=1 value of i=2 value of i=3 value of i=4 value of i=5
Know more about this
3) do-while loop – The do while loop is similar to while loop with the only difference that it starts with the execution of statements before the test condition is checked. If the test expression returns false as the first time, the statements execute only once.
do while is a loop statement that allows their loop control access repeatedly to perform based given boolean expression as same as while loop but with some differences. The do-while loop structure distinguishes with the while loop structure.
In the do -while loop, initially, statements are executed only once then loop control moves for updating-statements. Then it starts to evaluate test condition and it(flow of control )begins to work as a normal while loop.
Syntax
for(initialization_statements; boolean_Expression; increment/decrement){ //loop body looping statements }
Flowchart
Example
#include <stdio.h> #include <stdlib.h> int main() { int i=1; do{ printf("%d\n",i); i++; }while(i<=10); getch(); return 0; }
When the above code is executed, it produces the following result
1 2 3 4 5 6 7 8 9 10
Know more about this
Nested do-while loop in C language
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…