C Do While Yes No Start All Over Again

In programming, information technology is frequently desired to execute certain block of statements for a specified number of times. A possible solution will be to blazon those statements for the required number of times. However, the number of repetition may not exist known in advance (during compile time) or maybe big enough (say 10000).

The all-time solution to such problem is loop. Loops are used in programming to repeatedly execute a certain block of statements until some condition is met.

In this article, nosotros'll larn to utilize while loops in C#.


C# while loop

The while keyword is used to create while loop in C#. The syntax for while loop is:

while (exam-expression) { 	// body of while }        

How while loop works?

  1. C# while loop consists of a examination-expression.
  2. If the test-expression is evaluated to true,
    1. statements within the while loop are executed.
    2. after execution, the test-expression is evaluated again.
  3. If the test-expression is evaluated to false, the while loop terminates.

while loop Flowchart

C# while loop
Working of C# while loop

Case one: while Loop

          using System;  namespace Loop { 	class WhileLoop 	{ 		public static void Chief(string[] args) 		{ 			int i=1; 			while (i<=5) 			{ 				Console.WriteLine("C# For Loop: Iteration {0}", i); 				i++; 			} 		} 	} }                  

When we run the program, the output volition be:

          C# For Loop: Iteration i C# For Loop: Iteration 2 C# For Loop: Iteration 3 C# For Loop: Iteration 4 C# For Loop: Iteration v        

Initially the value of i is 1.

When the program reaches the while loop argument,

  • the test expression i <=5 is evaluated. Since i is i and 1 <= 5 is true, it executes the body of the while loop. Hither, the line is printed on the screen with Iteration ane, and the value of i is increased by 1 to get 2.
  • Now, the examination expression (i <=5) is evaluated again. This time too, the expression returns truthful (2 <= 5), so the line is printed on the screen and the value of i is now incremented to three..
  • This goes and the while loop executes until i becomes 6. At this indicate, the test-expression will evaluate to faux and hence the loop terminates.

Case 2: while loop to compute sum of first 5 natural numbers

          using System;  namespace Loop { 	grade WhileLoop 	{ 		public static void Main(cord[] args) 		{ 			int i=1, sum=0;  			while (i<=5) 			{ 				sum += i; 				i++; 			} 			Console.WriteLine("Sum = {0}", sum); 		} 	} }                  

When nosotros run the program, the output will exist:

          Sum = xv        

This program computes the sum of first 5 natural numbers.

  • Initially the value of sum is initialized to 0.
  • On each iteration, the value of sum is updated to sum+i and the value of i is incremented past i.
  • When the value of i reaches 6, the examination expression i<=5 volition return false and the loop terminates.

Let's see what happens in the given programme on each iteration.

Initially, i = 1, sum = 0

While loop execution steps
Iteration Value of i i<=five Value of sum
i 1 true 0+i = one
two 2 truthful 1+2 = 3
3 3 truthful 3+3 = 6
four 4 truthful vi+4 = 10
5 5 true 10+v = xv
6 6 false Loop terminates

So, the final value of sum volition be 15.


C# do...while loop

The practice and while keyword is used to create a do...while loop. Information technology is similar to a while loop, however at that place is a major difference between them.

In while loop, the condition is checked before the body is executed. It is the exact opposite in do...while loop, i.e. status is checked later the body is executed.

This is why, the torso of exercise...while loop will execute at least once irrespective to the test-expression.

The syntax for do...while loop is:

exercise { 	// body of practise while loop } while (test-expression);        

How practice...while loop works?

  1. The body of exercise...while loop is executed at first.
  2. Then the test-expression is evaluated.
  3. If the test-expression is true, the body of loop is executed.
  4. When the test-expression is false, practice...while loop terminates.

exercise...while loop Flowchart

Do while loop in c#
Working of C# do...while loop

Case 3: exercise...while loop

          using Organization;  namespace Loop { 	class DoWhileLoop 	{ 		public static void Primary(cord[] args) 		{ 			int i = 1, n = 5, production;  			do 			{ 				product = n * i; 				Console.WriteLine("{0} * {1} = {two}", n, i, product); 				i++; 			} while (i <= 10); 		} 	} }                  

When we run the program, the output will be:

          5 * 1 = v 5 * two = 10 5 * 3 = 15 v * four = 20 5 * 5 = 25 five * 6 = 30 5 * 7 = 35 5 * eight = 40 5 * 9 = 45 5 * ten = 50        

As we can see, the to a higher place program prints the multiplication tabular array of a number (5).

  • Initially, the value of i is 1. The program, then enters the body of do..while loop without checking whatsoever condition (as opposed to while loop).
  • Inside the body, product is calculated and printed on the screen. The value of i is then incremented to 2.
  • Later the execution of the loop's torso, the test expression i <= x is evaluated. In total, the do...while loop volition run for 10 times.
  • Finally, when the value of i is 11, the exam-expression evaluates to false and hence terminates the loop.

Space while and do...while loop

If the test expression in the while and do...while loop never evaluates to false, the body of loop will run forever. Such loops are called space loop.

For instance:

Infinite while loop

while (true) { 	// body of while loop }        

Space do...while loop

do { 	// body of while loop } while (true);        

The infinite loop is useful when we need a loop to run every bit long as our program runs.

For case, if your program is an blitheness, yous will need to constantly run it until information technology is stopped. In such cases, an space loop is necessary to go on running the blitheness repeatedly.

hopkinsmanclook42.blogspot.com

Source: https://www.programiz.com/csharp-programming/do-while-loop

0 Response to "C Do While Yes No Start All Over Again"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel