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# While Loop

by suresh

C# While loop is a condition-based loop and it will check the condition before executing the block of code. The C# while loop syntax is as follows.

while(<Boolean expression>)
{
      Statements;
}

Here, the expression returns a boolean value. When the expression returns true, the Control enters into the block, and its statements will execute. Once the statements in the block get executed, the Control again returns to the condition.

This process gets continued until the condition returns false. Once the Boolean expression returns false, the C# Control skips the block of code in the while loop. It starts executing the statements after the closing brace.

While writing statements inside the block, we should make sure to update variables associated with the expression so that the loop gets ended when we want to, to avoid keeping on iterating inside the loop infinitely.

C# While loop Example

Let us demonstrate the code using a while loop to print i value from 2 to 10 until the condition i <=10 satisfied by incrementing i with 3.

using System;

namespace CSharp_Tutorial
{
    class Program
    {
        static void Main()
        {
            int i = 2;
            while (i <= 10)
            {
                Console.WriteLine(i);
                i += 3;
            }
            Console.WriteLine("Control is out of loop");
            Console.ReadLine();
        }
    }
}

OUTPUT

C# While Loop 1

In this C# While Loop example, i initialized with 2.

Boolean expression i <= 10, i.e., 2 <= 10 which returns true. So control enters into block and executes the statements i.e., 2 printed.

And then i incremented by 3. So, i = 2 + 3 = 5

Again condition checking, 5<=10 returns true. So, 5 printed.

i = 5+3 = 8

Again 8<=10 returns true. So 8 printed.

i = 8+3 = 11

Again 11<= 10 returns false. The Control comes out of the loop and executes the statements after closing brace, i.e., Control is out of the loop.

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