for Loop कैसे लिखें
एक for loop किसी चीज़ को एक तय संख्या में दोहराता है, जैसे गिनते हुए बिल्कुल दस push-ups करना। आप बताते हैं कि कहां से शुरू करें, कब रुकें, और कैसे गिनें।
In this page:
Syntax
for (int i = start; i <= end; i++)
{
// body
}
for Loop
एक for loop एक ज्ञात संख्या में एक block को दोहराता है, एक initializer, एक condition, और एक increment step से नियंत्रित होते हुए।
यह standard choice है जब आपको बिल्कुल पता हो कि कितने iterations चाहिए, जैसे indices में चलना। for header के तीनों हिस्से optional हैं लेकिन conventionally साथ उपयोग होते हैं।
Note:
for का उपयोग करें जब आपको loop index खुद चाहिए, सिर्फ हर element नहीं।उदाहरण: for Loop
using System;
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 5; i++)
{
Console.WriteLine($"Count: {i}");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- किसी array length के साथ
<=उपयोग करना, जैसेi <= arr.Length, जोIndexOutOfRangeExceptionफेंकता है। - loop body के अंदर loop variable बदलना और iterations skip करना।
for (...);के बाद एक semicolon लगाना, loop body को खाली बनाते हुए।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: