TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: C While and Do-While Loops
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
C Tutorials

C While and Do-While Loops

Last updated: Oct 13, 2023 10:39 pm
By Meenakshi Agarwal
Share
5 Min Read
C While and Do-While Loops
SHARE

In this C programming class, we’ll cover the C while and do…while loop statements. The loops are the main constructs to implement iterative programming in C.

Contents
While Loop in CFlowchartSyntaxWhile Loop ExampleDo…While Loop in CFlowchartSyntaxC Do…While Loop ExampleC Loops Exercises for PracticeWhile loop Sample ProgramDo…While Loop Sample Program

During your C coding journey, there will be instances when you will need to run a statement more than once. It could be required for taking multiple inputs or processing output. You can achieve this by using loops in C.

We also have a tutorial on “Setting up C Environment” available to help you get started quickly.

Loops in C Programming

A loop is an instruction given to the computer that it has to run a specific part of the code for a given number of times.

To perform a particular task or to run a specific block of code several times, the concept of LOOP comes into the picture.

Basically, there are three types of loops in C language. In this tutorial, we will see the first two loops in detail.

While Loop in C

The while loop begins by first checking the terminal condition. If the condition is true, the control enters the loop else not.

If the underlying condition is true, then it goes ahead and executes the block of code in the loop. After the first iteration, it again checks with the changed (increased/decreased) values of the variables (the condition operands) and decides the further course of execution.

Flowchart

The below flowchart will help you understand the functioning of the while loop.

C While Loop Flowchart

Syntax

While (condition)
{
    //Statement block
    //Increment/Decrement operation
}

While Loop Example

Here is a simple example of how a while loop works. This program prints numbers from 1 to 10 without actually using the ten printf statements but a while loop.

#include<stdio.h>
#include<conio.h>

void main()
{
    int n = 1;

    while(n <= 10)
    {
        printf("%d\n\n", n);
        n++;
    }

    getch();
}

Here, the “\n” in the printf call is used to move to the next line.

Its output should look something like this-

Sample Code Output

Do…While Loop in C

In this loop, the statement block gets executed first, and then the condition is checked.

If the underlying condition is true, then the control returns to the loop otherwise exit it.

Flowchart

The below flowchart will help you understand the functioning of the do-while loop.

C Do While Loop Flowchart

Syntax

do
{
    //statement block
}
While(condition);

C Do…While Loop Example

Here is a simple example to find the sum of 1 to 10 using the do-while loop

#include<stdio.h>
#include<conio.h>

void main()
{
    int i = 1,a = 0;

    do
    {
        a = a + i;
        i++;
    }
    while(i <= 10);

    printf("Sum of 1 to 10 is %d",a);
    getch();
}

Its output should be something like this-

Another Sample Code Output

Generally, the do-while loop is not preferred in applications as it first executes the block of statements and then checks the condition. It risks security which is like allowing an unauthorized person into a facility and then asking for his ID.

In the above sections, we gave an explanation of the while and do-while loops. We will see the for loop in detail in the next chapter.

There is an exercise you can perform on the next page which will help you understand these two loops nicely.

C Loops Exercises for Practice

Write a program in C to multiply two numbers without actually using the * operator but have to use both the while and do-while loops.

While loop Sample Program

#include<stdio.h>
#include<conio.h>

void main()
{
    int a, b, i = 0, c = 0;

    printf("Enter two numbers to multiply : ");
    scanf("%d %d",&a, &b);

    while(i < b)
    {
        c = c + a ;
        i++;
    }

    printf("The product of these numbers is %d", c);
    getch();
}

Do…While Loop Sample Program

#include<stdio.h>
#include<conio.h>

void main()
{
    int a, b, i = 0, c = 0;

    printf("Enter two numbers to multiply : ");
    scanf("%d %d",&a, &b);

    do
    {
        c = c + a ;
        i++;
    }
    while(i < b);

    printf("The product of these numbers is %d", c);
    getch();
}

Keep in mind that in a do-while loop, the semicolon always comes after a while statement but not in a while loop.

The output for both the following programs is the same, check from the below screenshot.

C While Loop Exercises Result

You Might Also Like

C Programming Language “Struct”

20 C Programs for Beginners to Practice

Learn C Programming – How To Guide for Beginners

Python vs C++ – Is Python or C++ Better?

The Best Top-Down Approach Guide for You in C

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article C Decision Making_ If, If-Else, Switch-Case C Decision Making: If, If-Else, Switch-Case
Next Article C For Loop Purpose, Flowchart, and Example C For Loop Purpose, Flowchart, and Example

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x