C# · Chapter 3.9 · Intermediate level
List<T> + while Loop + break/continue (Game Shop & Inventory)
List<T> + while लूप + break/continue (गेम शॉप और इन्वेंटरी)
An array (string[]) is like a train with a fixed number of bogies — you cannot add one more. A List is like your school bag: you can add and remove things any time. List<string> bag = new List<string>(); makes an empty bag that holds only text (<string>). bag.Add("Potion") puts an item in, bag.Remove("Stone") takes it out and bag.Count tells how many items there are (arrays use .Length, lists use .Count!). A while loop repeats as long as a condition is true — like buying potions while you still have coins. Inside any loop, break; means stop the loop now and continue; means skip the rest of this round and go to the next one. Lists need using System.Collections.Generic; at the top.
Array (string[]) तय डिब्बों वाली ट्रेन जैसा है — उसमें एक और डिब्बा नहीं जुड़ सकता। List आपके स्कूल बैग जैसी है: कभी भी चीज़ें डालो और निकालो। List<string> bag = new List<string>(); एक खाली बैग बनाता है जिसमें सिर्फ़ टेक्स्ट (<string>) रहेगा। bag.Add("Potion") चीज़ डालता है, bag.Remove("Stone") निकालता है और bag.Count बताता है कि कितनी चीज़ें हैं (array में .Length, List में .Count!)। while लूप तब तक दोहराता है जब तक शर्त सच है — जैसे सिक्के बचे हों तब तक potion खरीदते रहना। किसी भी लूप में break; का मतलब लूप अभी रोको और continue; का मतलब इस चक्कर का बाकी हिस्सा छोड़ो, अगले चक्कर पर जाओ। List के लिए सबसे ऊपर using System.Collections.Generic; लिखना ज़रूरी है।
Words and symbols in this chapter
using System.Collections.Generic;- Lets you use
List<T>(andDictionary). List<T>(औरDictionary) इस्तेमाल करने देता है।List<string> bag = new List<string>();- An empty list that can grow and holds only strings.
- खाली list जो बढ़ सकती है और जिसमें सिर्फ़ strings रहती हैं।
bag.Add("Potion"); / bag.Remove("Stone");- Add an item at the end / remove the first matching item.
- आख़िर में item जोड़ो / पहला मिलता हुआ item हटाओ।
bag.Count / bag[0]- Number of items (not
.Length!) / the first item (counting starts at 0). - कितने items हैं (
.Lengthनहीं!) / पहला item (गिनती 0 से)। while (coins >= potionPrice) { ... }- Repeats the block as long as the condition stays true.
- जब तक शर्त सच है, block को दोहराता है।
break; / continue;breakleaves the loop at once;continuejumps to the next round.breakलूप से तुरंत बाहर निकालता है;continueअगले चक्कर पर कूदता है।
Ready example
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> bag = new List<string>();
bag.Add("Wooden Sword");
bag.Add("Bread");
bag.Add("Stone");
bag.Remove("Stone");
Console.WriteLine("Items: " + bag.Count + " | First item: " + bag[0]);
int coins = 150;
int potionPrice = 30;
while (coins >= potionPrice) {
if (bag.Count == 5) {
Console.WriteLine("Bag full! Stop shopping.");
break;
}
bag.Add("Potion");
coins -= potionPrice;
Console.WriteLine("Bought a Potion. Coins left: " + coins);
}
foreach (string item in bag) {
if (item == "Bread") {
continue;
}
Console.WriteLine("Ready for battle: " + item);
}
}
}
What you will see: The 🎒 inventory HUD fills up as potions are bought, and the console shows the shop log.
Potion खरीदते ही 🎒 inventory HUD भरता जाता है, और console में दुकान का पूरा हिसाब दिखता है।
Line by line
List<string> bag = new List<string>();- Creates an empty bag (list).
- खाली बैग (list) बनाता है।
bag.Remove("Stone");- Stone is taken out — the bag now has 2 items.
- Stone निकाल दिया — अब बैग में 2 items हैं।
while (coins >= potionPrice) { ... }- Keeps buying while there are enough coins: 150 → 120 → 90 → 60.
- जब तक सिक्के काफ़ी हैं, खरीदता रहता है: 150 → 120 → 90 → 60।
if (bag.Count == 5) { ... break; }- When the bag is full the loop stops at once, even though coins are left.
- बैग भरते ही लूप तुरंत रुक जाता है, भले ही सिक्के बचे हों।
if (item == "Bread") { continue; }- Bread is skipped; every other item is printed.
- Bread छोड़ दी जाती है; बाकी हर item छपता है।
Common mistake
✗ Wrong
List<string> bag = new List<string>();
Console.WriteLine(bag.Length);✓ Right
List<string> bag = new List<string>();
Console.WriteLine(bag.Count);A List uses .Count (arrays use .Length) — bag.Length gives error CS1061.
List में .Count लगता है (array में .Length) — bag.Length से error CS1061 आती है।
Your task
Buy arrows: inside while (coins >= 25), stop with break when the quiver has 3 arrows, otherwise quiver.Add("Arrow") and coins -= 25. The last line must print Arrows: 3, Coins left: 25.
तीर खरीदो: while (coins >= 25) के अंदर, तरकश में 3 तीर होते ही break से रुको, वरना quiver.Add("Arrow") और coins -= 25 करो। आख़िरी लाइन में Arrows: 3, Coins left: 25 छपना चाहिए।
- Below the TODOs write
while (coins >= 25) {and close it with}.
TODO के नीचेwhile (coins >= 25) {लिखो और}से बंद करो। - Inside:
if (quiver.Count == 3) { break; }, thenquiver.Add("Arrow");andcoins -= 25;.
अंदर:if (quiver.Count == 3) { break; }, फिरquiver.Add("Arrow");औरcoins -= 25;। - Run — the last line should say
Arrows: 3, Coins left: 25.
Run करो — आख़िरी लाइन मेंArrows: 3, Coins left: 25आना चाहिए।
Starter code for the task
using System;
using System.Collections.Generic;
class Program {
static void Main() {
int coins = 100;
List<string> quiver = new List<string>();
// TODO 1: while (coins >= 25) { ... } loop banao
// TODO 2: loop ke andar: agar quiver.Count == 3 ho to break;
// TODO 3: warna quiver.Add("Arrow"); aur coins -= 25;
Console.WriteLine("Arrows: " + quiver.Count + ", Coins left: " + coins);
}
}Do this task in the classroom: your code is checked automatically and you get hints if something is missing.
Recap
List<string>grows and shrinks:Add,Remove,Contains,bag[0],bag.Count.while (condition) { }repeats while the condition is true — something inside must change it.break;leaves the loop now;continue;skips to the next round.
List<string>बढ़ती-घटती है:Add,Remove,Contains,bag[0],bag.Count।while (condition) { }शर्त सच रहने तक दोहराता है — अंदर कुछ ऐसा हो जो शर्त बदले।break;लूप से अभी बाहर;continue;अगले चक्कर पर।
Quick quiz
What does break; do inside a loop?
लूप के अंदर break; क्या करता है?
Show the answer
Answer: B (Stops the loop at once and continues after it)
break exits the loop immediately. continue is the one that skips to the next round.
break लूप से तुरंत बाहर निकालता है। अगले चक्कर पर कूदने का काम continue करता है।
Practice problems for this chapter
- Game Shop (List<T> + while + break)
गेम शॉप (List<T> + while + break)
Solve them in the Practice Zone, where each answer is checked automatically and two hints are available.