How to implement while loop in C# ?

CK CK 3788 
Created at
Updated at  
1,342 0 0

A while loop will check a condition and then continues to execute a block of code as long as the condition evaluates to a boolean value of true. Its syntax is as follows: while (<boolean expression>) { <statements> }.  The statements can be any valid C# statements. The boolean expression is evaluated before any code in the following block has executed. When the boolean expression evaluates to true, the statements will execute. Once the statements have executed, control returns to the beginning of the while loop to check the boolean expression again.

When the boolean expression evaluates to false, the while loop statements are skipped and execution begins after the closing brace of that block of code. Before entering the loop, ensure that variables evaluated in the loop condition are set to an initial state. During execution, make sure you update variables associated with the boolean expression so that the loop will end when you want it to. Listing 4-1 shows how to implement a while loop. 

Following is the example.

int myInt = 0;

while (myInt < 10)
{
  Console.Write("{0} ", myInt);
  myInt++;
}
Console.WriteLine();
Tags C# boolean expression C# while loop while loop Facebook X
Comments 0
  1. Conversation Multi-Agent Chatbot with On-the-Fly Code Execition via Microsoft AutoGen
    296
  2. Getting Started with FAISS: A CentOS 7 Installation Guide
    280
  3. CMake - a cross-platform build system generator that creates Makefiles used in C++ projects
    295
  4. How To Check If Your Printer Is Connected Using C#
    1,026
  5. KMP String Matching Algorithm
    672
  6. How to get process id in C# like windows task manager ?
    870
  7. How to implement switch statement in C# ?
    987
  8. How to delete file ?
    1,192
  9. How to convert integer to text string ?
    697
  10. How to convert text string to integer ?
    610
  11. How to set an certain color as transparent color on bitmap
    512
  12. How to resize and save PNG or Jpeg Image file ?
    631
  13. How to display message box in C#
    1,109
  14. How to convert bitmap image to jpeg image ?
    356
  15. To load image from file
    450
  16. URL Encode, Decode function for MFC
    604
  17. How to load HTML resource on MFC ?
    841
  18. How to get GM Time as String ?
    422
  19. Makefile to automate the build process for C++
    279
  20. Finding process name on OS
    407
  21. Convert System.DateTime to UNIX timestamp
    381
  22. Retrieve system information
    495
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    39
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    34
  3. How do I create animated background for Google Meet?
    20,611
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    244
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    94
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    157
  7. Array of arguments passed to script in PHP
    90
  8. Invalid Auction Storms in Ad Tech
    98
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    202
  10. The Two Clocks of Ad Server Architecture
    70
  11. Frequency Cap Architecture Overview
    514
  12. Collecting Proof-of-Play from 100,000 Screens
    146
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    287
  14. What is RedisVL?
    109
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    230
  16. TPU & GPU Cluster Management Tools
    243
  17. The Concept of Scaffolding in AI Agents
    233
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    583
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    258
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    339
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    202
  22. Machine Learning: Training vs. Ranking
    221
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    222
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    195
  25. Designing a Data Curation System Around a Vector Database
    220
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    256
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    656
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    195
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    216
  30. Understanding Data Lineage
    260