Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

C# Do while loop

by suresh

C# Do while loop is quite similar to the while loop except for one thing. In do while loop, the statements inside the loop execute once, and then it checks the condition. So do while guarantees to execute the statements of the loop at least once.

The syntax of the C# Do while loop is

do
{
   statements;
} while<boolean expression>

C# Do while loop Example

Let us see an example code using do while loop to read the value of integer n from user and print integers until n <= 5 returns true.

using System;

namespace CSharp_Tutorial
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter an integer {0}", " ");
            int n = int.Parse(Console.ReadLine());
            Console.WriteLine();
            do
            {
                Console.WriteLine(n);
                n++;
            } while (n <= 5);
            Console.ReadLine();
        }
    }
}

First output: input is 2

C# Do While Loop 1

Here, control asks the user to enter some integer, and the input has given 2.

Now it prints the integer 2.

2++, which is 3

It will check the condition 2 <= 5, which returns true. Again the loop repeats by printing the n value, i.e., 3

3++, which is 4.

It will check the condition 3 <= 5, which returns true….

..

Until n value is 5, the loop will repeat. When n = 5, control prints the value five and then increments

5++ = 6

Now the condition 6 <= 5 returns false. So the C# control comes out of the loop

Second output: input is 6

C# Do While Loop 2

Here, in this case, as usual, control reads the input six and enters the do loop.

The compiler prints the value 6 and increments, i.e., 6++ = 7.

Now it checks for condition 7 <= 5, which returns false. So it quits the loop by printing six as output.

Placed Under: C#

  • Dot Net Framework
  • C# Basic Example Program
  • C# Variables
  • C# Constant
  • C# Keywords
  • C# Regular Expressions
  • C# Built in Data Types
  • C# Nullable Types
  • C# Data type Conversion
  • C# Date and Time Format
  • C# Enum or Enumerator
  • C# Value and Reference types
  • C# Operators
  • C# Arithmetic Operators
  • C# Assignment Operators
  • C# Bitwise Operators
  • C# Logical Operators
  • C# Null Coalescing operator
  • C# Relational Operators
  • C# Ternary Operator
  • C# Unary Operators
  • C# If Statement
  • C# If Else Statement
  • C# Else if Statement
  • C# Nested If Statement
  • C# Break Statement
  • C# Continue Statement
  • C# goto statement
  • C# Switch Case
  • C# While Loop
  • C# Do while loop
  • C# For Loop
  • C# Foreach loop
  • C# String Builder
  • C# String
  • C# String Functions
  • C# Array
  • C# Array Functions
  • C# Multi Dimensional Arrays
  • C# Jagged Array
  • C# OOPS Introduction
  • C# Constructor
  • C# Destructor
  • C# Access Modifiers
  • C# Inheritance
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy