LOOP Concept in C.
In Previous Blog we discussed about conditional statement used in C with few Programs.Today we will discuss about Loop Concept in C.
Before Loop Concept if you see this blog first time please go through my previous blog where you able to know basic concept of C and able to use loop concept in your program .Below is the link for Blogs.
https://dheeraj60.blogspot.com/2020/06/introduction-to-c-program-dotcmdb.html
https://dheeraj60.blogspot.com/2020/06/c-basics-programs-with-with-different.html
https://dheeraj60.blogspot.com/2020/06/conditional-statementsif-if-else-nested.html
Let’s Start with Loop Concept in C.
Loop: A loop is mainly used for executing a block of statements repeatedly until a given condition gives false.
Types of Loop in C
For Loop
While Loop
Do While Loop
Before Starting Loop we should know why Loop is required. Loop executes the sequence of statements many times until the stated condition becomes false. For example suppose if we want to print my name 20 times then no need to write printf 20 times as we put the condition in loop for 20 in variable and in one printf statement we can display the required output.
Today we will discuss about for loop in C and in next blog we will go through another loop. For Loop is one of the most frequently used loop in C programming as in one statement only we can initialize , give the condition and do the increment or decrement.
Syntax of For loop:
for (initialization; condition; increment or decrement)
{
//Statements which needs to be executed repeatedly
}
Let’s Understand the Syntax first then we will write some program using for loop.
First we initialize the variable and In the second step the condition is checked, where the variable is tested for the given condition, if the condition returns true then the C statements inside the body of for loop gets executed, if the condition returns false then the for loop gets terminated and the control comes out of the loop. After successful execution of statements inside the body of loop, the counter variable is incremented or decremented by using Arithmetic operator (++ and --).
Let’s understand with one basic example where i want to print value from 1 to 10 by using for loop in single printf statement.
Below is the Program.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
for (i=1; i<=10; i++)
{
printf("%d\n", i);
}
return 0;
}
Hope you are able to understand syntax of this program from my previous blog , only thing we need to know about this program is for loop as we initializes i is variable with int data type and in for loop we initialize the value i=1 ; and in condition we given <=10 and last increment by 1 it means value start with 1 and till 10 it will give the value one by one by using increment once condition satisfied it will stop the program and give the value . We can change the condition value and initialize value as per our requirement.
Below is the output of program in code block compiler.
Using For Loop we can simply write one basic program which gives the table from 1 to 10 by simply using 2 for loop and initialize 3 variable in the program .
Below is the Program :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j, mult;
for(i=1;i<=10;i++)
{
for(j=1;j<=10;j++)
{
mult = i*j;
printf("%d x %d = %d\n", i, j, mult);
}
printf("\n");
}
return 0;
}
Here we simple initialize the 2 variables in which i used for loop and make condition till 10 and in 3rd variable we multiply the both variable since loop is incremented 1 by 1 so it’s satisfy the condition of table till 10 . if we need to make changes as per our requirement we need to make changes in condition only . The above program we can also called is Nested For Loop , we will discuss more about this Later.
Below is the output of program in compiler code block.
Program display the table from 1 to 10.
Nested For Loop in C :
Loop within Loop is known as Nested Loop , In Above program for displaying table we use the same.
Syntax for Nested for loop :
for ( init; condition; increment ) {
for ( init; condition; increment ) {
statement(s);
}
statement(s);
}
Let’s take one more example of this loop. If we want separate value from two variable for output in one line to see the conditional value we can use Nested for loop as below.
#include <stdio.h>
#include <stdlib.h>
int main()
{
for (int i=0; i<3; i++)
{
for (int j=0; j<5; j++)
{
printf("%d, %d\n",i ,j);
}
}
return 0;
}
Here we will able to see the value of both loop by using condition . In first for loop we initialize i value 0 and condition is <3 so it will display the value till 2 start with 0 and in second j value initialize from 0 and condition is j <5 so it will display the value till 4 start with 0.
See the output of this program for better understanding in compiler code-block.
If you see the output you will able to understand how nested inner and outer loop is working. We have a for loop inside another for loop, this is called nesting of loops we will use nesting of loops more in Array programming where it’s very useful for programmer to use nested loops in Array. We will use it in Array when we start Array we will able to write matrix program etc by using nested for loop.
Using Nested For loop you can several interesting programs to print the value n times like print prime number from 2 to 100 etc by using two variables and apply the logic and other programs related to for loop . We already written one program that displays table from 1 to 10 .. I will give you some important programs in coming blogs by using nested loop but for now please try to make some changes and write some another program by using for loop , If you need any assistance let me know.
This is all about for loop and nested for loop in C Program please go through this blog carefully and write some good programs as nested for loop widely used in Array program so try to make more practice on this Loop . In next blog i will come up with other loops in C programs and also overview about Logical Operator AND OR and NOT and will play with logical operator in loop and conditional statement in C program.
Please let me know if you have any doubts on today’s blog.
Thanks .
Comments
Post a Comment