Using while loop in C#


In C# loop statements are use for execute set of commands at specific time .you can set the loop,how long it will be execute.today we will use while loop for execute statement five times and in output we get how many time loop execute

while Loop in C#


IN C# we use Loop staements for execute specified command as need of program.While statement is one of them. The while statement execute a set f stsement as long as the test condition specified in the while statement return a true value.

Syntax of while loop



while(test condition)
{
set of statements
}

IN above syntax,test condition is expression,a variable or value returned by function that evaluate to Boolean value.the while statement along with statements enclosed within {} forms in while block.
The execution of the while loop starts with evaluation of test condition.When the value of the test condition is true,the set of statements enclosed in braces are executed.

Given bellow program show you use of while statment in C#:

using system;

class whileloop

{

public static void Main()

{

int z=12345;

int x=0;

int y=z;

while(z>0)

{

++x;

z=z/10;

}

Console.Writeline(Number {0}.contains{1} digits.",y,x);

}

}


In above code ,the while loop is used to find number of digits presents in the numbers.The given number is z=12345,in this loop will be execute five times and hence the value of x will be five at the end of loop.
The output of this code will be as below:
number 12345 contains 5 digits.
Press any key to continue ...


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: