Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
  • Register
  • Login
  1. Home
  2. Blogpost

While Loop

Date- Dec 09,2023

3230

C# provides the while loop to repeatedly execute a block of code as long as the specified condition returns false. The while loop starts with the while keyword, and it must include a boolean conditional expression inside brackets that returns either true or false. It executes the code block until the specified conditional expression returns false. The condition is given in the beginning of the loop and all statements are executed till the given boolean condition satisfies when the condition becomes false, the control will be out from the while loop else It executes the code block until the specified conditional expression returns false. 

Syntax of While Loop

while (boolean condition) 
{ // code of block to be executed }

Example 1: while Loop

using System;
					
public class Program
{
	public static void Main()
	{
		int i = 1; // initialization
		int m = 2;
		while (i < 10) // condition
		{
			Console.WriteLine(m + " Multiply By " + i + " is - "  + m*i);
		
			i++; // increment
		}
	}
}

The output will be:

2 Multiply By 1 is - 2
2 Multiply By 2 is - 4
2 Multiply By 3 is - 6
2 Multiply By 4 is - 8
2 Multiply By 5 is - 10
2 Multiply By 6 is - 12
2 Multiply By 7 is - 14
2 Multiply By 8 is - 16
2 Multiply By 9 is - 18

Example 2: while Loop

using System;
					
public class Program
{
	public static void Main()
	{
		int i = 10; // initialization
		// Exit when i becomes less than 1 
		while (i >= 1) // condition
		{
			Console.WriteLine("The Value of i is  " + i);
		
			i--; // increment
		}
	}
}

The output will be:

The Value of i is  10
The Value of i is  9
The Value of i is  8
The Value of i is  7
The Value of i is  6
The Value of i is  5
The Value of i is  4
The Value of i is  3
The Value of i is  2
The Value of i is  1

S
Shubham Saini
Programming author at Code2Night โ€” sharing tutorials on ASP.NET, C#, and more.
View all posts โ†’

Comments

Tags

AspNet
C#
programming
AspNet MVC
c programming
AspNet Core
C
software development
tutorial
MVC
memory management
Paypal
coding
coding best practices
data structures
programming tutorial
tutorials
object oriented programming
Slick Slider
StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
By Language
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ยท  Terms
Translate Page