While and for loop difference c sharp
While and for loop difference c#
For loops are appropriate loops when you know exactly how many times iteration you want in statements within the loop. For loop iterates a statement or a block of statements repeatedly until a specified expression evaluates to false.
Syntax: for (; Boolean-expression;) ….Statement }
For Example: for(int I =0; I <= 20; I++) if(I%2 == 0)
Console.WriteLine(“Print Even Numbers 0}”,I); } }
While loop:- A while loop statement in C# repeatedly executes a target statement as long as a given condition is true. Syntax The syntax of a while loop in C# is: while(condition) { statement(s); } Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true.