АкушерствоАнатомияАнестезиологияВакцинопрофилактикаВалеологияВетеринарияГигиенаЗаболеванияИммунологияКардиологияНеврологияНефрологияОнкологияОториноларингологияОфтальмологияПаразитологияПедиатрияПервая помощьПсихиатрияПульмонологияРеанимацияРевматологияСтоматологияТерапияТоксикологияТравматологияУрологияФармакологияФармацевтикаФизиотерапияФтизиатрияХирургияЭндокринологияЭпидемиология

Loops in C#

looping - the ability to repeat a block of code N times.

Do{} While{}. The loop’s test condition is evaluated after the body of the loop has been executed. The do loop evaluates the condition after the loop has executed, which makes sure that the code block is always executed at least once.

do{ Console.WriteLine(number); number = number +1; } while(number <5);

The for loop is a bit different. It's preferred when you know how many iterations you want, either because you know the exact amount of iterations, or because you have a variable containing the amount.

for (initializer; condition; iterator)

statement(s)

where: The initializer is the expression evaluated before the first loop is executed (usually initializing a local variable as a loop counter).The condition is the expression checked before each new iteration of the loop (this must evaluate to true for another iteration to be performed). The iterator is an expression evaluated after each iteration (usually incrementing the loop counter).The iterations end when the condition evaluates to false.

The while loop Unlike the for loop, the while loop is most often used to repeat a statement or a block of statements for a number of times that is not known before the loop begins. Like the for loop, while is a pretest loop. The syntax is similar, but while loops take only one expression:

while(condition)

statement(s);

The foreach loop allows you to iterate through each item in a collection. Syntax:

foreach (int temp in arrayOfInts) { Console.WriteLine(temp); }

Ex: foreach (int var in x){ var+=10;}

 


Дата добавления: 2015-09-18 | Просмотры: 486 | Нарушение авторских прав



1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |



При использовании материала ссылка на сайт medlec.org обязательна! (0.004 сек.)