Info for C# Loops

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

"increment" (For Loop)

After the body of the for loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.

Example: Foreach Loop

ArrayList list = new ArrayList(); list.Add("Phi Huynh"); list.Add("Dennis Jeon"); list.Add("Justin Bowser"); foreach(string name in list) { Console.WriteLine(name); }

Continue statement (Loop Control Statements)

Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

For Loop

It executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.

Do...While Loop

It is similar to a while statement, except that it tests the condition at the end of the loop body

While Loop

It repeats a statement or a group of statements while a given condition is true. It tests the condition before executing the loop body.

Break statement (Loop Control Statements)

Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.

After "init," "condition," and "increment"

The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again testing for a condition). After the condition becomes false, the for loop terminates.

Nested Loops

You can use one or more loop inside any another while, for or do..while loop.

Syntax: Nested 'Do...While Loop'

do { statement(s); do { statement(s); } while( condition ); } while( condition );

Syntax: Do...While Loop

do { statement(s); } while( condition );

Syntax: Nested 'For Loop'

for ( init; condition; increment ) { for ( init; condition; increment ) { statement(s); } statement(s); }

Syntax: For Loop

for ( init; condition; increment ) { statement(s); }

Example: Infinite Loop

for (; ; ) { Console.WriteLine("Hey! I am Trapped"); }

Syntax: Foreach Loop

foreach (<datatype> <identifier> in <list>) { //one or more statements; }

Example: Do...While Loop

int i = 0; do { Console.WriteLine("Value of i = " + i); i++; } while ( i < 5);

Example: While Loop

int i = 0; int number = 10; while (i < number) { i+=1; Console.WriteLine("while loop"); }

Example: Nested Loops w/ 'For Loop'

int numRows = 5; int numCols = 3; for (int i = 0; i < numRows; i++) { for (int j = 0; j < numCols; j++) { Console.WriteLine("Row: " + i); } }

Example: For Loop

int number = 0; for (int i = 0; i < i <= 100; i++) { number = number + i; }

Loop Control Statements

statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.

"condition" (For Loop)

this next step is evaluated after the "init". If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.

"init" (For Loop)

this step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.

Infinite Loop

A loop becomes __________ if a condition never becomes false. The for loop is traditionally used for this purpose. Since none of the three expressions that form the for loop are required, you can make an endless loop by leaving the conditional expression empty.

Foreach Loop

we evaluate each element individually. An index is not needed. With no indexes, loops are easier to write and programs are simpler; uses no integer index. Instead, it is used on a collection—it returns each element in order.

Syntax: While Loop

while(condition) { statement(s); }

Syntax: Nested 'While Loop'

while(condition) { while(condition) { statement(s); } statement(s); }


संबंधित स्टडी सेट्स

Queen Elizabeth II/The Royal British Family

View Set

Chapter 15: Monetary Policy, GDP, and the Price Level

View Set

CH 7: Solar System Big Idea Questions Class Set

View Set

Chemistry Final 2017 (T/F, Multiple Choice, Fill in the Blank, Short Answer)

View Set