Here I wrote a program for While Loop in c#
The while statement continually executes a block of statements until a specified expression evaluates to false . The expression is evaluated each time the loop is encountered and the evaluation result is true, the loop body statements are executed.
Like if statement the while statement evaluates the expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false.
Syntex:-
while(condition)
{
statement(s);
}
While Loop process flow diagram:-
while(Condition_true)
An empty while-loop with this condition is by definition an infinite loop. You can implement an infinite loop using the while statement as follows:
while (Condition_true){
Source Code:-
Output:-
While Loop With Break Statement
While Loop With Continue Statement
Output:-
The while statement continually executes a block of statements until a specified expression evaluates to false . The expression is evaluated each time the loop is encountered and the evaluation result is true, the loop body statements are executed.
Like if statement the while statement evaluates the expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false.
Syntex:-
while(condition)
{
statement(s);
}
While Loop process flow diagram:-
while(Condition_true)
An empty while-loop with this condition is by definition an infinite loop. You can implement an infinite loop using the while statement as follows:
while (Condition_true){
// statements
}
Source Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleDemoApp
{
class Test
{
static void Main(string[] args)
{
int Count = 1;
while (Count<6)
{
Console.WriteLine("While loop Iterate number is - " + Count);
Count++;
}
Console.ReadKey();
}
}
}
Output:-
While Loop With Break Statement
static void Main(string[] args)
{
int Count = 1;
while (Count<6)
{
if (Count == 3)
{
Console.WriteLine("Breaked at
Iteration 3rd");
Break;
}
Console.WriteLine("While loop
Iterate number is - " + Count);
Count++;
}
Console.ReadKey();
}
Output:-
While Loop With Continue Statement
static void Main(string[] args)
{
int Count = 1;
while (Count<6)
{
if (Count == 3)
{
Console.WriteLine("Continued at
Iteration 3rd");
Count++;
continue;
}
Console.WriteLine("While loop
Iterate number is - " + Count);
Count++;
}
Console.ReadKey();
}
Output:-
No comments:
Post a Comment