C# While Loop Statement Video Tutorial and Source code

C# While Loop Statement Video Tutorial and Source code

Introduction 

This lesson is all about the repetition control structures in C#, specifically the WHILE LOOP statement. The lesson includes sample programs to better understand how if statement works in C#.

A while loop is a repeating code block in computer programming that executes a certain number of times according to the number you specify as the parameter.

Objectives

By the end of this lesson, students should be able to:

  1. demonstrate essential understanding on the repetition control structures.
  2. use while loop statement in a C# program
  3. solve problems using the concept of while loop statements in C#.
C# While Loop Statement Video Tutorial and Source code
C# While Loop Statement Video Tutorial and Source code

Lesson Proper

Loops are used in programming to repeat a piece of code until a certain condition is met.

C# programming has three types of loops.

  • for loop
  • while loop
  • do while loop

In this tutorial, we will focus on the while loop statement of C#.

The while loop statement allows you to repeat a block of code as long as a certain condition is true. The code block will be executed until the condition is no longer true. This makes it ideal for scenarios in which you need to repeat a task a set number of times or until a condition is met. A while loop, for example, might be used to count down from a given number or to verify the status of a variable every time it changes.

How it works

C# while loop Statement - source code
C# while loop Statement – source code

Line 1 – To add namespaces and class files, we commonly use the using keyword. It then makes all of the classes, interfaces, and abstract classes, as well as their methods and properties, available on the current page. Console is a member function or method in the System namespace. Because we need the console function to show and read user inputs, we must include it on the first line of our program.

Line 3 – this line indicates the name of the program (CSharpWhileLoop).

Line 7 – the entry point of C# program is the main method.

Line 9 – this line is a declaration of variable named number of integer type. We have also set the initial value of the variable to zero.

Line 1014  – this is the scope of the while loop statement.

Line 10 – the while loop starts in this line with the expression that needs to be tested. There are cases that the while loop may not be executed if the expression returns false.

Line 12-13 – this is the body of the while loop. The number of lines may vary depending on the scope of the loop and depending on the complexity of the program in general. In our case the body of the while loop only contains 2 lines or statements.

Line 12 – the body of the while loop will display the value of the number variable. WriteLine method of the Console class is used to display a text on the console.

Line 13 – this line will increment the number variable by 1. It is consider as the update statement since we need to test the expression with the new value of the variable.

Line 15 – the purpose of this line is to prevent the console from closing unless the user will press a key in the keyboard.

using System;

namespace CSharpWhileLoop
{
class Program
{
static void Main(string[] args)
{
int number = 0;
while (number < 5)
{
Console.WriteLine(number);
number = number + 1;
}
Console.ReadKey();

}
}
}

Video Tutorial

FREE DOWNLOAD SOURCE CODE

Summary

In programming, a repetition control structure is a code block that allows a set of code to be run multiple times. The most common repetition control structures are for and while loops.

In computer programming, the while loop is a programming technique that allows code to be executed continuously while a specific condition is met. It is useful for actions that need to be performed a specified number of times or until a specific condition is met. The code included within the while loop will be performed for as long as the condition is true, which is indefinite. Once the condition is no longer true, the code contained within the while loop will be terminated.

We hope you found this tutorial to be helpful! Wishing you the best of luck with your projects! Happy Coding!

For the step by step tutorial on how to create the while loop statement in C#, please watch the video uploaded on our YouTube Channel.

You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive  free capstone projects resources and computer programming tutorials.

Hire our team to do the project.

Related Topics and Articles:

C# NESTED IF Statement Video Tutorial and Source code

C# IF ELSE IF Statement Video Tutorial and Source code

Delete a MySQL table Using C# Tutorial and Source code

C# IF ELSE Statement Video Tutorial and Source code

C# IF Statement Video Tutorial and Source code

, , , , , , , , , ,

Post navigation