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.
2
Q
Write a for loop from 1 to 100.
A
for (int k = 1; k <= 100; k++)
Console.WriteLine(k);
3
Q
Write a do…while loop from 1 to 100.
A
int k = 1;
do {
Console.WriteLine(k);
k++;
} while (k <= 100);
4
Q
What type of loop is guaranteed to run at least once?
A
do…while
5
Q
What is a nested loop?
A
A loop inside another loop.
6
Q
What happens if loop condition is always true?
A
Infinite loop.
7
Q
How to calculate sum of multiples of 3 from 1 to 10?
A
Use modulus: if (k % 3 == 0) sum += k;
8
Q
Correct the loop: for (int k = 0; k <= arr.Length; k++)
A
k < arr.Length
9
Q
How do you break out of a loop early?
A
Use break;
10
Q
How do you skip an iteration?
A
Use continue;