Skip to main content

Loop Concept in C with examples #DOTC_Mdb

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. Also you can make Triangle , Rectangle etc by using any value which you want you can do this by using for loop please try to make some program that shows you shape of triangle.


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

Popular posts from this blog

SQL and Classification of SQL in Oracle Database #Diksha Oracle Training Centre

  SQL and Classification of SQL in Oracle Database.   SQL is Structured Query Language , which is used for storing, manipulating and retrieving data stored in a relational database .SQL is the standard language for RDBMS. All the Relational Database Management Systems (RDMS) like Oracle, MySQL, Sybase, Informix, IBM DB2 and Microsoft SQL Server use SQL as their standard database language. Oracle is one of the more secured database as compared to other databases. Importance of   SQL : SQL and PL/SQL is a backend process where all data is stored and retrieved in GUI which created either by any programming languages like Java, C++, PHP etc. so we need to have very secure database so that there will be no impact for users. SQL allows users to access data in the relational database management systems. SQL is used to communicate with a database.SQL and PL/SQL allows users to create and drop databases tables , views , stored procedures , functions , packages , trigger etc. SQL allows

Materialized View uses and Benefits in Database #DOTC_Mdb

Materialized View uses and Benefits in Database. Hello Everyone, Today we will discuss about Materialized view as it’s play important role in database. We already discussed about Simple Views and complex views in my previous blog. Before Materialized view go through my previous blog which related to simple view. https://dheeraj60.blogspot.com/2020/05/benefits-of-creating-and-using-view-in.html As we know View is not a database object and not like table which is stored in database, but view can be created from base table a view is an SQL statement that’s stored in the database. This statement, or view, has a name.A view looks and acts a lot like a table. It has columns and rows, and can be included in SELECT queries just like a table. In other word we can say View is a virtual/logical table which is basically used for security purpose. Let’s understand   about   Materialized view : A materialized view is a view that stores the results of the view’s query. Whenever you query the ma

Top 50 Interview Questions On SQL and PL/SQL #DOTC_Mdb

                    Top 50 Interview Questions On SQL and PL/SQL. Today we will Discuss Top 50 interview questions and answers of SQL and PL/SQL which is frequently asked in interview.     Question 1: What is SQL and Classification of SQL? Answer SQL is a Structure Query Language which is vastly used in RDBMS database like Oracle, Sybase, DB2 , Microsoft SQL server etc.   Classification of SQL is as below: DDL (Data Definition Language):  Commands are  create , alter , drop , truncate etc DML (Data Manipulation Language) : Commands are  insert,update and delete . TCL (Transaction Control Language ) : Commands are  Commit , Rollback and Save point. DCL (Data Control Language) : Commands are Grant , Revoke Question 2:    What is meant by Joins? What are the types of join? Answer Joins are basically used to extract/get data from multiple tables using some common columns or conditions and also by using alias to fulfill the condition.   There are various types of Joins as li