Skip to main content

While and Do while Loop in C Program #DOTC_Mdb

While and Do while Loop Lop in C Program.


Hi Everyone,

In Previous blogs we discussed about for loop and nested for loop with some programs. Today we will discuss about other loops that are while and do while loop in C program. . To see how for loop and nested for loop is working you can see my below blogs for your reference.

https://dheeraj60.blogspot.com/2020/06/loop-concept-in-c-with-examples-dotcmdb.html

https://dheeraj60.blogspot.com/2020/06/nested-for-loop-with-simple-pattern.html


Let’s start with while and do –while loop in C Program.

Loop: A loop is mainly used for executing a block of statements repeatedly until a given condition gives false same as we discussed in for loop.But Syntax and process is different from For Loop. Let’s Understand While loop first with Syntax and how it works .

While Loop: While loop evaluates the test expression inside the parenthesis ().If the expression is true, statements inside the body of while loop are executed. Then, test expression is evaluated again. The process goes on until the test expression is evaluated to false. If the test expression is false, the loop will terminate.

Syntax of While Loop in C.

while (condition test)

{

      //Statements that needs to be executed repeatedly

      // Increment (++) or Decrement (--) Operation

}

 

Simple Program to understand how while loop work in C. If I want to print number from 1 to 10 then we use below program using while loop.

 

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

 int i=1;

   while (i <= 10)

   {

            printf("%d \n", i);

            i++;

   }

       return 0;

}

 

Here variable i is initialized with value 1 and then it has been tested for the condition that is i <= 10. If  condition returns true then the statements inside the body of while loop are executed else control comes out of the loop. Value of i is incremented using ++ operator then it has been tested again for the loop condition. It will run till the condition satisfied and print number from 1 to 10 . You can change the condition as per your requirement.

 

Below is the output of above program in Compiler Code Block.

 

 

Let’s see one more Program for Table using while loop. We will take input from user for any number and give the output of multiplication of that table . We already done this by using for loop but here we using while loop and syntax is different as first we give the condition if it’s true then statement and last increment.

Below is the Program .

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

 

 int mult, i = 1;

    printf("Please Enter any Number:");

    scanf("%d", &mult);

    printf("Multiplication table of %d: ", mult);

 

    while (i <= 10) {

        printf("%d x %d = %d", mult, i, mult * i);

        i++;

    }

       return 0;

}

 

In above program we use first check the  condition while (i <= 10) then statement and in last we do the increment , here what the user will give any input number output will be displayed for that number table.

Below is the Output of above program in Code Block.

Here you can enter any number to print the table as per your need.


DO –While loop : Let’s understand do –While loop which is very similar to while loop  but While loop is executed only when  condition is true. Whereas, do-while loop is executed for first time irrespective of the condition is true or false. After executing while loop for first time, then condition is checked. So simply we can say that if a condition is false at the first place then the do while would run once, but the while loop would not run at all if condition is false.

 

Syntax of Do-while loop:

do

{

    //Statements

 

}

while(condition test);

 

Here in Syntax only we see condition is checked after the statement so first time it will execute only, but then it will check the condition. Let’s understand with program.

 

Program by using do-while loop  which will give the value of variable 0 to 10;

 

#include <stdio.h>

#include <stdlib.h>

int main()

{

 int i=0;

            do

            {

                        printf("Value of  i is: %d\n", i);

                        i++;

            }

            while (i<=10);

 

       return 0;

}

 

Note : In Do while program will run for first time and if we make any changes in condition that not satisfy then program terminates , but first time it will run only .

 

Output of above program in compiler Code block is as below :


Let’s take one more example of table which we have written in while loop let’s do with do-while loop.

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

 

int i=1,mult=0;

printf(" Please enter any number: ");

scanf("%d",&mult);

do{

printf("%d \n",(mult*i));

i++;

}while(i<=10);

 

       return 0;

}

 

Here variable mult pass by user with any number and it will give the table of that number.

Below is the output of above Program in compiler code block.


As I mentioned in the beginning of this blog that do-while runs at least once even if the condition is false because the condition is evaluated, after the execution of the body of loop . But while loop only executes if condition is true.


Now we are familiar with all the loops in C and also the conditional statements . For reference you can see my previous blog of C. Please go through this blog and previous blogs and start with  Simple Mathematical programs like check whether number is odd or even , prime number, other basic program and pattern related program I already written for your reference . I am sure you can easily write the program in C now. But I want you to write some complex programs by using loops and conditional statements and if any issue let me know. In next blog I will come up with some interesting topics in C program which will be useful for you to write some complex program with your own control. Please feel free to reach me anytime.

 

Thanks


Comments

Post a Comment

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