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)!
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
istarting at1. - हिस्सा 1: काउंटर
iको1से शुरू करता है। i <= 5;- Part 2: Runs as long as
iis less than or equal to5. - हिस्सा 2: जब तक
i5 तक है, लूप जारी रखता है। i++- Part 3: Adds
+1toion 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
5to8to 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 रोबोट दुश्मन स्पॉन करो!
- Write
for (int i = 1; i <= 5; i++) {.for (int i = 1; i <= 5; i++) {लिखो। - Inside
{ }, writeConsole.WriteLine(i);.
अंदरConsole.WriteLine(i);लिखो। - 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); likhoDo this task in the classroom: your code is checked automatically and you get hints if something is missing.
Recap
for (int i = 1; i <= 5; i++)has 3 parts: start; keep-going check; step.- The parts are separated by semicolons
;, not commas. i++means "add 1 to i" after every round.
for (int i = 1; i <= 5; i++)के 3 हिस्से: शुरुआत; चलते रहने की जाँच; कदम।- हिस्सों के बीच semicolon
;लगता है, comma नहीं। i++का मतलब है हर चक्कर के बाद i में 1 जोड़ो।
Quick quiz
What does i++ mean inside for (int i = 1; i <= 5; i++)?
for (int i = 1; i <= 5; i++) में i++ का क्या मतलब होता है?
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
- Predict: Enemy Waves (for loop + int division)
बताओ: दुश्मनों की लहरें (for loop + int division)
Solve them in the Practice Zone, where each answer is checked automatically and two hints are available.