← Back to C# Course | Chapter 4: Control Flow | Lesson 3 of 7

for Loop कैसे लिखें

एक for loop किसी चीज़ को एक तय संख्या में दोहराता है, जैसे गिनते हुए बिल्कुल दस push-ups करना। आप बताते हैं कि कहां से शुरू करें, कब रुकें, और कैसे गिनें।

In this page:

  1. for Loop
Syntax
csharp
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

markup
using System;

class Program
{
    static void Main(string[] args)
    {
        for (int i = 1; i <= 5; i++)
        {
            Console.WriteLine($"Count: {i}");
        }
    }
}
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. #}
आम गलतियां
  1. किसी array length के साथ <= उपयोग करना, जैसे i <= arr.Length, जो IndexOutOfRangeException फेंकता है।
  2. loop body के अंदर loop variable बदलना और iterations skip करना।
  3. for (...); के बाद एक semicolon लगाना, loop body को खाली बनाते हुए।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.