C# · Chapter 3.4 · Basic level

Loops in C# (Robot Enemy Spawner! )

C# में for लूप (रोबोट दुश्मन स्पॉनर! )

How do games spawn 5 Alien Invaders on screen in a split second? With C#'s for loop! Look at the 3 parts inside for (int i = 1; i <= 5; i++):
1. Start (int i = 1;) → Start counting from Enemy #1.
2. Stop check (i <= 5;) → Keep going while i is 5 or less.
3. Step (i++) → i++ is short for 'increase i by 1 after each enemy'!

गेम में एक सेकंड में 5 एलियन दुश्मन (👾) स्क्रीन पर कैसे पैदा (Spawn) होते हैं? C# के for लूप से! for (int i = 1; i <= 5; i++) के ब्रैकेट में 3 छोटे हिस्से होते हैं:
1. शुरुआत (int i = 1;) → गिनती 1 से शुरू करो।
2. कब तक (i <= 5;) → जब तक i 5 या उससे छोटा रहे, चलते रहो।
3. हर कदम (i++) → हर चक्कर के बाद i में 1 जोड़ दो (i++ मतलब i = i + 1)!

In the classroom you can run and edit this code in your browser, get hints and mark the chapter as done. क्लास में यह code ब्राउज़र में चलाकर बदल सकते हो।

Words and symbols in this chapter

for ( ... )
Starts a C# loop with 3 parts separated by semicolons ;.
3 हिस्सों वाला C# लूप शुरू करता है (बीच में ; लगते हैं)।
int i = 1;
Part 1: Creates counter i starting at 1.
हिस्सा 1: काउंटर i को 1 से शुरू करता है।
i <= 5;
Part 2: Runs as long as i is less than or equal to 5.
हिस्सा 2: जब तक i 5 तक है, लूप जारी रखता है।
i++
Part 3: Adds +1 to i on every turn (1 → 2 → 3 → 4 → 5).
हिस्सा 3: हर बार i को 1 से बढ़ाता है (1 → 2 → 3 → 4 → 5)।

Ready example

for (int i = 1; i <= 5; i++) {
    Console.WriteLine("Enemy Spawned #" + i);
}

What you will see: 5 animated Robot Alien Enemies (👾 👾 👾 👾 👾) spawn onto the 2D Game Stage!

जितनी बार लूप चलेगा (जैसे 5 बार), गेम स्क्रीन पर उतने ही क्यूट रोबोट दुश्मन (👾) उछलते हुए दिखेंगे!

Line by line

for (int i = 1; i <= 5; i++) {
Runs the block 5 times (try changing 5 to 8 to spawn 8 aliens!).
अंदर के कोड को 5 बार चलाता है (5 को 8 बदलकर 8 रोबोट स्पॉन करके देखो!)।
Console.WriteLine("Enemy Spawned #" + i);
Prints each spawned enemy number and adds a cute 👾 to the Game Stage!
हर दुश्मन का नंबर छापता है और गेम स्टेज पर एक नया 👾 उतारता है!

Common mistake

✗ Wrong

for (int i = 1, i <= 5, i++)

✓ Right

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

Inside C# for (...), the 3 parts are separated by semicolons ;, NOT commas ,!

C# के for लूप के तीनों हिस्सों के बीच कॉमा , नहीं बल्कि सेमीकोलन ; लगता है!

Your task

Run a for (int i = 1; i <= 5; i++) loop to print 1 through 5 and spawn 5 robot enemies!

for (int i = 1; i <= 5; i++) लूप चलाकर 1 से 5 तक प्रिंट करो और 5 रोबोट दुश्मन स्पॉन करो!

  1. Write for (int i = 1; i <= 5; i++) {.
    for (int i = 1; i <= 5; i++) { लिखो।
  2. Inside { }, write Console.WriteLine(i);.
    अंदर Console.WriteLine(i); लिखो।
  3. Close with } and click ▶ Run & Preview!
    } बंद करके ▶ Run & Preview दबाओ!
Starter code for the task
Console.WriteLine("Enemy wave aa rahi hai!");

// TODO: for (int i = 1; i <= 5; i++) { ... } loop banao
//       aur andar Console.WriteLine("Enemy Spawned #" + i); likho

Do this task in the classroom: your code is checked automatically and you get hints if something is missing.

Recap

Quick quiz

What does i++ mean inside for (int i = 1; i <= 5; i++)?

for (int i = 1; i <= 5; i++) में i++ का क्या मतलब होता है?

  1. Multiply i by 2
  2. Increase i by 1 each turn
  3. Stop the loop
  4. Reset i to 0
Show the answer

Answer: B (Increase i by 1 each turn)

i++ increments i by 1 after every loop cycle (i = i + 1).

i++ हर चक्कर के बाद i की गिनती में 1 जोड़ देता है।

Practice problems for this chapter

Solve them in the Practice Zone, where each answer is checked automatically and two hints are available.