Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
MATLAB - Loops
Next article icon

C#- Nested loops

Last Updated : 14 Oct, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

Nested loops are those loops that are present inside another loop. In C#, nesting of for, while, and do-while loops are allowed and you can also put any nested loop inside any other type of loop like in a for loop you are allowed to put nested if loop.

for Loop: The functionality of for loop is quite similar to while loop. It is basically used when the number of times loop statements are to be executed is known beforehand. Nesting of for loop is allowed, which means you can use for loop inside another for loop.

Syntax:

for(variable initialization; testing condition; increment / decrement)  {      for(variable initialization; testing condition;                                    increment / decrement)      {          // Statements       }  }  

Example: 

C#




// C# program to illustrate nested for loop 
using System; 
  
class GFG{
      
public static void Main() 
{ 
      
    // for loop within another for loop 
    // printing GeeksforGeeks 
    for(int i = 0; i < 4; i++) 
        for(int j = 1; j < i; j++) 
            Console.WriteLine("GeeksforGeeks!!"); 
} 
}
 
 

Output:

GeeksforGeeks!!  GeeksforGeeks!!  GeeksforGeeks!!  

while Loop: In a while loop, the test condition is given at the beginning of the loop, and all statements are executed till the given boolean condition satisfies and when the condition becomes false, the control will be out from the while loop. Nesting of a while loop is allowed, which means you can use while loop inside another while loop. However, it is not recommended to use nested while loop because it is difficult to maintain and debug.

Syntax:

while(condition)   {     while(condition)     {        // Statements      }      // Statements   }  

Example:

C#




// C# program to illustrate nested while loop 
using System; 
  
class GFG{
      
public static void Main() 
{ 
    int x = 1, y = 2;
      
    while (x < 4)
    {
        Console.WriteLine("Outer loop = {0}", x);
        x++;
      
        while (y < 4)
        {
            Console.WriteLine("Inner loop = {0}", y);
            y++;
        }
    }
} 
}
 
 

Output:

Outer loop = 1  Inner loop = 2  Inner loop = 3  Outer loop = 2  Outer loop = 3  

do-while Loop: In C#, the do-while loop is similar to the while loop with the only difference that is, it checks the condition after executing the statements. Nesting of a do-while loop is allowed, which means you can use a do-while loop inside another do-while loop.

Syntax:

do  {     // Statements     do      {        // Statements     }     while(condition);  }  while(condition);  

Example:

C#




// C# program to illustrate nested do-while loop 
using System; 
  
class GFG{
      
public static void Main() 
{ 
    int x = 1;
    do
    {
        Console.WriteLine("Outer loop = {0}", x);
        int y = x;
      
        x++;
                  
        do
        {
            Console.WriteLine("Inner loop: {0}", y);
            y++;
        } while (y < 4);
  
    } while (x < 4);
} 
}
 
 

Output:

Outer loop = 1  Inner loop: 1  Inner loop: 2  Inner loop: 3  Outer loop = 2  Inner loop: 2  Inner loop: 3  Outer loop = 3  Inner loop: 3  


Next Article
MATLAB - Loops
author
ankita_saini
Improve
Article Tags :
  • C#
  • CSharp-ControlFlow
  • CSharp-Decision Making

Similar Reads

  • C# Loops
    Looping in a programming language is a way to execute a statement or a set of statements multiple times depending on the result of the condition to be evaluated to execute statements. The result condition should be true to execute statements within loops. Loops are mainly divided into two categories
    5 min read
  • C# - Break statement
    In C#, the break statement is used to terminate a loop(for, if, while, etc.) or a switch statement on a certain condition. And after terminating the controls will pass to the statements that present after the break statement, if available. If the break statement exists in the nested loop, then it wi
    4 min read
  • C# Arrays
    An array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float, etc. and the elements are stored in a contiguous location. Length of the array spe
    8 min read
  • Loops in Rust
    A loop is a programming structure that repeats a sequence of instructions until a specific condition is satisfied. Similar to other programming languages, Rust also has two types of loops: Indefinite Loop: While loop and LoopDefinite Loops: For loopLet's explore them in detail. While LoopA simple wa
    3 min read
  • MATLAB - Loops
    MATLAB stands for Matrix Laboratory. It is a high-performance language that is used for technical computing. It was developed by Cleve Molar of the company MathWorks.Inc in the year 1984.It is written in C, C++, Java. It allows matrix manipulations, plotting of functions, implementation of algorithm
    2 min read
  • How to Read From a File in C?
    File handing in C is the process in which we create, open, read, write, and close operations on a file. C language provides different functions such as fopen(), fwrite(), fread(), fseek(), fprintf(), etc. to perform input, output, and many different C file operations in our program. In this article,
    2 min read
  • C Multiple Choice Questions
    C is the most popular programming language developed by Dennis Ritchie at the Bell Laboratories in 1972 to develop the UNIX operating systems. It is a general-purpose and procedural programming language. It is faster than the languages like Java and Python. C is very versatile it can be used in both
    4 min read
  • How to Read Input Until EOF in C?
    In C, reading input until the End of the File (EOF) involves reading input until it reaches the end i.e. end of file. In this article, we will learn various methods through which we can read inputs until EOF in C. Reading Input Until EOF Read Input Until EOF Using getchar()Read Input Until EOF Using
    5 min read
  • Array of Pointers to Strings in C
    In C, arrays are data structures that store data in contiguous memory locations. Pointers are variables that store the address of data variables. We can use an array of pointers to store the addresses of multiple elements. In this article, we will learn how to create an array of pointers to strings
    2 min read
  • Examination Management System in C
    Problem Statement: Write C program to build a Software for Examination Management System that can perform the following operations:   Add/Delete the Details of the StudentsAttendance Monitoring of the studentsSet/Edit Eligibility criteria for examsCheck Eligible Students for ExamsPrint all the recor
    9 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences