iteration Flashcards

(10 cards)

1
Q

What is a loop?

A

A structure that repeats a block of code as long as a condition is met.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Write a for loop from 1 to 100.

A

for (int k = 1; k <= 100; k++)
Console.WriteLine(k);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Write a do…while loop from 1 to 100.

A

int k = 1;
do {
Console.WriteLine(k);
k++;
} while (k <= 100);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What type of loop is guaranteed to run at least once?

A

do…while

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a nested loop?

A

A loop inside another loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What happens if loop condition is always true?

A

Infinite loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to calculate sum of multiples of 3 from 1 to 10?

A

Use modulus: if (k % 3 == 0) sum += k;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Correct the loop: for (int k = 0; k <= arr.Length; k++)

A

k < arr.Length

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you break out of a loop early?

A

Use break;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you skip an iteration?

A

Use continue;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly