Skip to content
  • General
  • Programming
  • DS & Algo
  • System Design
  • Interview Questions
  • Home
  • YouTube
  • About
  • Contact
Learn to Code and Code to Learn

Learn to Code and Code to Learn

Your Journey to Code Mastery

  • General
    • Setup
  • Programming
    • C++
    • C++-11
    • c++-14
    • Python
  • DS & Algo
    • DS
    • Algo
      • Competitive Programming
        • Leetcode Problems
  • System Design
    • Design Pattern
    • SOLID Principle
  • Interview Questions
    • C++
    • Company Wise
  • Toggle search form

Day – 2/Part – 4 : Loops in C programming

Posted on December 17, 2023December 17, 2023 By thecodepathshala No Comments on Day – 2/Part – 4 : Loops in C programming

A loop statement allows programmers to execute a statement or group of statements multiple times without repetition of code. In other hand, Loops are used to repeat a block of code until the specified condition is met. 

Why we need loops?

If we need to print the table of 2, we can use multiple print statement.
// C program to illustrate need of loops
#include <stdio.h>
int main()
{
printf( "2\n");
printf( "4\n");
printf( "6\n");
printf( "8\n");
printf( "10\n");
printf( "12\n");
printf( "14\n");
printf( "16\n");
printf( "18\n");
printf( "20\n");

return 0;
}


In other hand, we can use loop and print the table. There will be less line of code.
// C program to illustrate need of loops
#include <stdio.h>
int main()
{
for(int i=1; i<=10; i++)
{
printf( "i*2\n");
}
return 0;
}


There are mainly two types of loops:

  1. Entry Controlled loops: In Entry controlled loops the test condition is checked before entering the main body of the loop. For Loop and While Loop is Entry-controlled loops.
  2. Exit Controlled loops: In Exit controlled loops the test condition is evaluated at the end of the loop body. The loop body will execute at least once, irrespective of whether the condition is true or false. do-while Loop is Exit Controlled loop.
Types of Loop in C

There are 3 types of Loop in C language, namely:

  1. for loop : first Initializes, then condition check, then executes the body and at last, the update is done.
  2. while loop : first Initializes, then condition checks, and then executes the body, and updating can be inside the body.
  3. do while loop : do-while first executes the body and then the condition check is done.
1. for loop

The for loop in C is used to execute a set of statements repeatedly until a particular condition is satisfied. We can say it is an open ended loop. 

Syntax :

for(initialization; condition; increment/decrement)
{
statement-block;
}

The for loop is executed as follows:

  1. It first evaluates the initialization code.
  2. Then it checks the condition expression.
  3. If it is true, it executes the for-loop body.
  4. Then it evaluate the increment/decrement condition and again follows from step 2.
  5. When the condition expression becomes false, it exits the loop.

    Flowchart:

We first initialize our iterator. Then we check the condition of the loop. If it is false, we exit the loop and if it is true, we enter the loop. After entering the loop, we execute the statements inside the for loop, update the iterator and then again check the condition. We do the same thing unless the test condition returns false.

Example :
Program to print first 10 natural numbers using for loop

#include<stdio.h>
void main( )
{
    int x;
    for(x = 1; x <= 10; x++)
    {
        printf("%d\t", x);
    }
}

/*
Output:
1 2 3 4 5 6 7 8 9 10
*/

Nested for loop :

We can also have nested for loops, i.e one for loop inside another for loop in C language. This type of loop is generally used while working with multi-dimensional arrays.

Syntax:

for(initialization; condition; increment/decrement)
{
for(initialization; condition; increment/decrement)
{
statement ;
}
}

Example:
Program to print half Pyramid of numbers using Nested loops

#include<stdio.h>

void main( )
{
    int i, j;
    /* first for loop */
    for(i = 1; i < 5; i++)
    {
        printf("\n");
        /* second for loop inside the first */
        for(j = i; j > 0; j--)
        {
            printf("%d", j);
        }
    }
}

/*
Output :

1
21
321
4321
54321
*/
2. While Loop

While loop does not depend upon the number of iterations. If the condition will become false then it will break from the while loop else body will be executed.

The while loop is an entry controlled loop. It is completed in 3 steps.

  • Variable initialization.(e.g int x = 0;)
  • condition(e.g while(x <= 10))
  • Variable increment or decrement ( x++ or x-- or x = x + 2 )

Syntax:
variable initialization;
while(condition)
{
statements;
variable increment or decrement;
}

Flowchart for while loop:

We can see that firstly, we initialize our iterator. Then we check the condition of while loop. If it is false, we exit the loop and if it is true, we enter the loop. After entering the loop, we execute the statements inside the while loop, update the iterator and then again check the condition. We do the same thing unless the condition is false.

Program to print first 10 natural numbers using while loop

#include<stdio.h>
void main( )
{
    int x;
    x = 1;
    while(x <= 10)
    {
        printf("%d\t", x);
        /* below statement means, do x = x+1, increment x by 1 */
        x++;
    }
}

/*
Output:
1 2 3 4 5 6 7 8 9 10
*/
3. do-while Loop

There is some situation where we execute body of the loop once before testing the condition. Such situations can be handled with the help of do-while loop.
The do-while loop is similar to a while loop but the only difference lies in the do-while loop test condition which is tested at the end of the body. In the do-while loop, the loop body will execute at least once irrespective of the test condition.

Syntax:

initialization_expression;
do
{
    // body of do-while loop
    
    
    update_expression;

} while (test_expression);

Flowchart for do-while loop:

We initialize our iterator. Then we enter body of the do-while loop. We execute the statement and then reach the end. At the end, we check the condition of the loop. If it is false, we exit the loop and if it is true, we enter the loop. We keep repeating the same thing unless the condition turns false.

Example :
Program to print first 10 natural numbers using while loop

#include<stdio.h>
void main( )
{
    int x;
    x = 1;
    do
    {
        printf("%d\t", x);
        /* below statement means, do x = x+1, increment x by 1 */
        x++;
    }
    while(x <= 10);
}

/*
Output:
1 2 3 4 5 6 7 8 9 10
*/

Infinite Loops

We come across infinite loops in our code when the compiler does not know where to stop. It does not have an exit. This means that either there is no condition to be checked or the condition is incorrect.

// for loop

#include <stdio.h>
int main()
{
    for(int i = 0; ; i++)
        printf("Infinite loop\n");
    return 0;
}

// while loop

#include <stdio.h>
int main()
{
    int i = 0;
    while(i == 0)
        printf("Infinite loop\n");
    return 0;
}

// do-while loop
#include <stdio.h>
int main()
{
    do{
        printf("Infinite loop\n");
    } while(1);
    return 0;
}

Jumping Out of Loops

while executing a loop, it becomes necessary to skip a part of the loop or to leave the loop as soon as certain condition becomes true.
For jumping out fro the loop, we use break and continue statement.
To learn about break and continue Please check break and continue statement.
7. Jump Statements
7.1 Break
7.2 Continue

Mastering in C programming (Crash Course) Tags:Mastering C Programming in 15 Days

Post navigation

Previous Post: Day – 2/Part – 3 : Decision Making and Control Statements in C
Next Post: Day 3 : Questions list

More Related Articles

Day-2 / Part – 1 : Basic Input and Output in C Mastering in C programming (Crash Course)
Array in C programming Array in C
Strings in C C++
Day 3 : Questions list Mastering in C programming (Crash Course)
Day – 2/Part – 2 : Operator and expressions Mastering in C programming (Crash Course)
Day – 2/Part – 3 : Decision Making and Control Statements in C Mastering in C programming (Crash Course)

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Archives

  • August 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • September 2023
  • February 2023
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021

Categories

  • Algo
  • Array in C
  • C Programming
  • C++
  • C++
  • Company Wise
  • Competitive Programming
  • Design Pattern
  • DS
  • DS & Algo
  • Fast and Slow Pointer
  • fixed size sliding window
  • General
  • GFG
  • GFG PTOD
  • Interview Questions
  • Leetcode Problems
  • Leetcode PTOD
  • Leetcode Top Interview 150
  • LLD
  • Low-level design
  • Mastering in C programming (Crash Course)
  • Programming
  • Roadmap
  • Setup
  • Setup
  • sliding window
  • SOLID Principle
  • STL
  • string in c
  • System Design
  • Top X

Tags

algorithm array bactracking basic c++ coding interview C Programming Crash Course data structure and algorithm design pattern dsa easy Fixed size sliding window fubctions GFD gfg GFG PTOD hard jump game LC PTOD leetcode Leetcode PTOD Leetcode Top Interview 150 LLD loop loops Low-level design Mastering C Programming in 15 Days matrix medium recursion rotate array searching&sorting sliding window solid STL string string in c sunction in c system design Template in C++ Top Top 20 coding patterns to master MAANG Interview Top interview 150

Real life example | Abstract factory | Design pattern #designpatterns #lowleveldesign #interview
Advantage of Abstract factory design pattern #interview #lld #google #apple #meta #facebook
Abstract factory design pattern | what? Why? How? #interview #lld #google #apple #meta #facebook
Master Abstract Factory Design Pattern in C++ | Real-World Examples & Code Explanation

In this video, we break down the Abstract Factory Design Pattern in C++ step-by-step. You’ll learn:
✅ What is Abstract Factory Pattern
✅ When & why to use it in C++
✅ UML diagram explanation
✅ Real-world examples for better understanding
✅ Complete C++ code implementation

Whether you’re preparing for FAANG interviews, learning Design Patterns, or improving your Object-Oriented Programming skills, this tutorial will help you write clean, scalable, and maintainable C++ code.

Keywords:
abstract factory c++, abstract factory design pattern c++, abstract factory design pattern example, creational design patterns in c++, design patterns in c++ with examples, faang interview preparation, c++ oops concepts
abstract factory pattern, c programming, design patterns in c, object oriented design, system architecture, software design, c language tutorial, creational patterns, software engineering, c programming tutorial, factory method pattern, design principles, object creation, software development, programming concepts
master abstract factory design pattern in c,
abstract factory design pattern php,
abstract factory design pattern c#,
abstract factory design pattern vs factory pattern,
abstract factory design pattern js,
abstract factory design pattern c++,
abstract factory design pattern example,
factory and abstract factory design pattern in java,
factory method design pattern php,
abstract factory and factory design pattern,
factory pattern and abstract factory pattern,
abstract factory design pattern example c#,
java abstract factory design pattern,
abstract factory method design pattern,
abstract factory design pattern java,
abstract factory design pattern example java,
abstract factory design pattern typescript,
abstract factory design pattern in java,
abstract factory design pattern in c#,
abstract factory design patterns in java,
factory pattern design pattern,
abstract factory design pattern python

#Cpp #DesignPatterns #AbstractFactory #Programming #codewithme 
#masterabstractfactorydesignpatterninc++ #abstractfactorydesignpatterninc++ #masterabstractfactorydesignpatterninc++hindi #masterabstractfactorydesignpatterninc++and #masterabstractfactorydesignpatterninc++andc
Master Abstract Factory Design Pattern in C++ | Real-World Examples & Code Explanation
LLD | Design pattern types | Types of design pattern | C++ #designpatterns #lld #interview
Singleton Design Pattern | c++ code #softwareengineering #lowleveldesign #codinginterview
Design pattern | Adapter design pattern | C++ code
🔌 Adapter Design Pattern Explained | Real-Life Analogy + Code Examples

Ever wanted two incompatible interfaces to work together? That’s exactly what the Adapter Pattern is for!

In this video, you’ll learn how the Adapter Design Pattern helps you connect mismatched interfaces using real-world analogies (like charging adapters!) and clear code demos.

🎯 What You’ll Learn:
✅ What is the Adapter Pattern?
✅ Real-life analogies that make it easy to remember
✅ How to implement it in code (Class Adapter vs Object Adapter)
✅ When to use it and common pitfalls to avoid

💡 Part of the Structural Design Patterns, the Adapter is perfect when:

You want to integrate legacy code with modern systems

You need to work with incompatible APIs

You're preparing for coding interviews or system design questions

👨‍💻 Great for:

Software Developers

Interview Prep (especially for FAANG roles)

Students learning OOP & Design Patterns

📌 Don’t forget to Like, Subscribe, and hit the Bell for more clear, beginner-friendly design pattern tutorials!

#AdapterPattern #DesignPatterns #SoftwareEngineering #SystemDesign #OOP #StructuralPattern #CodingInterview #TechWithRaushan

adapter design pattern, adapter design pattern c, design pattern adapter, adapter design pattern java, how adapter design pattern works, how to use adapter design pattern, adapter design pattern c example, adapter design patter, adapter design pattern javascript, adapter design pattern real world example, review adapter design patter, honest opinion adapter design patter, how to implement adapter design patter, adapter design pattern advantages and disadvantages

adapter design pattern,adapter pattern java,adapter pattern tutorial,adapter pattern explained,class diagram adapter pattern,adapter pattern implementation,structural patterns,design patterns in java,software design,java programming,coding tutorial,programming patterns,object-oriented design,software architecture,pattern design
🔌 Adapter Design Pattern Explained | Real-Life Analogy + Code Examples
🔒 Singleton Design Pattern Explained | Real-World Examples & Use Cases

Struggling to understand the Singleton Pattern? In this video, we’ll break it down step-by-step with real-world examples and easy-to-follow code!

🎯 What You'll Learn:
✅ What is the Singleton Design Pattern?
✅ When and why to use it
✅ Real-world analogies to simplify understanding
✅ Thread-safe implementation tips (Lazy, Eager, Double-Checked Locking)
✅ Common mistakes to avoid

💡 The Singleton Pattern ensures a class has only one instance and provides a global point of access to it. It’s widely used in logging, configuration management, driver objects, and more!

👨‍💻 Ideal for:

Beginners learning design patterns

Interview prep (especially for FAANG!)

Developers writing scalable and maintainable code

🔔 Like 👍 | Subscribe 🔴 | Comment 💬 your questions below!

#SingletonPattern #DesignPatterns #SoftwareEngineering #SystemDesign #CodingInterview #CreationalPattern #TechWithRaushan 

singleton pattern,design pattern,creational design pattern,singleton implementation,thread-safe singleton,java design pattern,singleton class,singleton in java,singleton example,singleton tutorial,double locking singleton,singleton design,pattern for singleton,singleton instance,singleton pattern java

singleton design pattern, hoc singleton design pattern, singleton design pattern javascript, singleton pattern, singleton pattern js, design pattern, simple design pattern, what is singleton pattern, singleton pattern tutorial, singleton pattern explained, design patterns, singleton pattern javascript, design pattern examples, creational design pattern, singleton pattern implementation, best design patterns, design patterns fast, design patterns tutorial, how to use design patterns, design patterns explained
🔒 Singleton Design Pattern Explained | Real-World Examples & Use Cases
Load More... Subscribe

Recent Posts

  • Palindrome Linked List
  • Find the Duplicate Number
  • Remove Nth Node From End of List
  • Linked List Cycle II
  • Decode the string | GFG PTOD | 01 Mar| Medium level | STACK

    Recent Comments

    1. reebjnhzey on GFG PTOD | 23 Feb | Next Greater Element | Medium level | STACK
    2. 서울여성전용마사지 on C program to check Leap Year
    3. http://boyarka-inform.com/ on C program to enter basic salary and calculate gross salary of an employee
    4. Denny on C program to enter basic salary and calculate gross salary of an employee
    5. Cabanon Eco on C program to check Leap Year

    Copyright © 2025 Learn to Code and Code to Learn.

    Powered by PressBook Blog WordPress theme