Shared Flashcard Set

Details

C#
Chapter 5 - Loops
6
Computer Science
Undergraduate 4
10/21/2024

Additional Computer Science Flashcards

 


 

Cards

Term
While Loop
Definition

Executes a body of statements continuously as long as the loop’s test condition continues to be true. Can either hold a simple boolean expression or a compound expression using AND or OR. 

Ex.)

int numLoops = 0;

while (numLoops <= 10) 

{

WriteLine(numLoops);

numLoops++;

}

 

Output: 0, 1, 2.... 10

Term
Infinite Loop
Definition

A loop that never ends. The body of the loop must take some action that alters the value of the loop control variable so that the expression eventually evaluates false and ends the loop.

Ex.) 

[image]

Term
Definite and Indefinite Loop
Definition

Definite: Also known as a counted loop, it is a loop that has a predetermined number of iterations. 

Ex.)

for (int i = 0; i < 5; i++)

{

    Console.WriteLine(i); // Runs exactly 5 times: 0 to 4

}

 

Indefinite: The value of a loop control variable is not altered by arithmetic, but instead is altered by user input

Ex.) 

int counter = 0;

 

while (counter < 5) {

    Console.WriteLine(counter);

    counter++; // If this was omitted or altered, the loop might

    run indefinitely

}

 

 

Term
Sentinel Value
Definition

A special value that signifies the end of input or the termination of a loop, it controls when to stop processing data or iterating through a collection. Ex.)

[image]

 

As long as the user continues to enter Y or y as the sentinel value, the loop will keep iterating and adding one to numLoops

Term
For Loop
Definition

syntax:

for (initialization; condition; increment/decrement)

{

statements

}

 

Ex.)

for (int i = 1; i <= 5; ++i)   // Increment comes AFTER body

        {

            Console.WriteLine(i);

        }

Output: 1 2 3 4 5

Term
Scope
Definition

You can initialize more than one variable by placing commas between separate initializations:

for (g=0, h=1; g < 6; ++g)

You can also declare a new variable within the for statement

for (int k = 0; k < 5; ++k)

When the variable is declared within the loop however; as above, it can only be accessed within that loop, as it is in _____, or the area where it is known and can be accessed. The variable is unusable or out of _____ outside the loop.

Supporting users have an ad free experience!