string input;
do {
Console.Write("Enter password: ");
input = Console.ReadLine();
} while (input!= "secret123");
Break & Continue
for (int i = 1; i <= 10; i++) {
if (i == 3) continue; // Skip 3, go to 4
if (i == 7) break; // Stop loop at 7
Console.WriteLine(i); // Prints: 1,2,4,5,6
}
Real Example: Retry API Call
int maxRetries = 3;
for (int i = 0; i < maxRetries; i++) {
try {
var data = CallAPI();
Console.WriteLine("Success");
break; // Exit loop on success
} catch {
if (i == maxRetries - 1) throw; // Last retry failed
Thread.Sleep(1000); // Wait 1 sec before retry
}
}
Quick Check ๐ง
EasyQ1: Which loop for iterating a List?
MediumQ1: How many times does this run? int i=0; do { i++; } while(i < 0);
No comments yet. Be the first to share your thoughts!