C# Statements

Introduction to C# Statements

एक program अलग अलग तरह के statements की sequence से बना होता है। कोई program क्या task perform करेगा यह आप statements द्वारा ही define करते है। Program में हर statement एक separate action को define करता है जो CPU द्वारा perform किया जाएगा।

Statements दो प्रकार के होते है।

  • Single Line – Single line statements एक ही line में लिखते जाते है जैसे की variable declaration या function call आदि।
  • Statement Block – Statement blocks single statements से अलग होते है। एक statement block कई statements का set होता है जिन्हें curly brackets में define किया जाता है। जैसे की Loop statement या function definition आदि।

जिस order में statements execute होते है वह program का flow of control या flow of execution कहलाता है। Program के flow (execution) को control करने के लिए control flow statements use किये जाते है। Selection statements, looping statements और jumping statements control flow statements की श्रेणी में आते है।

C# में available सभी प्रकार के statements के बारे में आगे detail से बताया जा रहा है।

Declaration Statements

Declaration statements वे statements होते है जो आप variables या constants को declare करने के लिए लिखते है। ये statements normally program की शुरुआत में ही define किये जाते है। Declaration statements का उदाहरण निचे दिया जा रहा है।

int a = 50;  //Variable declaration statement
const int a = 50;  //Constant declaration statement

Expression Statements

Expression statements वे statements होते है जिनमें कोई value calculate की जाती है। उदाहरण के लिए यदि आप किसी statement में कोई mathematical operation perform करते है तो वह expression statement होता है।

int areaCircle = 3.14*r*r;

Selection Statements

Selection statements control flow statements की श्रेणी में आते है। Selection statements के द्वारा आप statements को किसी condition के आधार पर execute करवा सकते है। C# में available सभी selection statements के बारे में आगे बताया जा रहा है।

If Statement

If statement एक statement block होता है जिसके statement किसी condition के true होने पर execute होते है। Condition को if keyword के बाद brackets में define किया जाता है। Condition एक boolean expression होता है जिसका result या तो true या false होता है।

Condition के true होने पर if block के statements execute हो जाते है। यदि condition true नहीं होती है तो if block का कोई भी statement execute नहीं होता है। उदाहरण के लिए Num variable की value 10 से अधिक होने पर आप greater print करना चाहते है तो ये आप if statement द्वारा कर सकते है।

Syntax:

if(condition)
{
   //Statements to be executed only when condition is true.
}

Example:

if(Num >10)
{
    Console.WriteLine(“Greater”);
}

If Else Statement

If else statement if statement में else part जोड़कर बनता है। जिस प्रकार if block में वे statements लिखे जाते है जो condition true होने पर execute होंगे उसी प्रकार else block में वे statements लिखे जाएंगे जो condition false होने पर execute होंगे।

Syntax:

if(condition)
{
    //Statements to be executed only when condition is true.
}
else
{
    //Statements to be execute only when condition is false
}

Example:

if(Num >10)
{
   Console.WriteLine(“Greater”);
}
else
{
   Console.WriteLine(“Lesser”);
}

Switch Statement

Switch statement भी if statement की तरह ही होता है लेकिन switch statement में आप statements को execute करने के लिए एक से अधिक conditions define कर सकते है। Switch में condition की बजाय एक choice pass की जाती है और block के अंदर अलग अलग cases define किये जाते है।

आपकी choice जिस case से match होती है वही case execute हो जाता है। बाकी के cases को skip कर दिया जाता है। हर case के बाद break statement define किया जाता है। ऐसा इसलिए किया जाता है ताकि उस case के statements execute होने के बाद switch case terminate हो जाए और बाकी के cases के statements execute ना हो।

सभी cases के आखिर में default case define किया जाता है जो सही choice नहीं pass करने पर execute होता है।

Syntax:

switch(choice)
{
     case 1 :
                   //Statements to be executed when choice is one.
//break statement;

     case 2 :
                  //Statements to be executed when choice is two.
//break statement.
...
...
...
     default :
                    //Statements to be executed when no appropriate choice is passed
//break statement
}

Example:

switch(2)
{
      case 1 :
                   Console.WriteLine(“First Case Executed.”);
                    break;

      case 2 :
                    Console.WriteLine(“Second Case Executed.”);
                    break;

       default :
                    Console.WriteLine(“Please pass 1 or 2 only as choice”);
                    break;
}

Looping Statements

कई बार आपको एक या एक से अधिक statements को बार बार execute करने की आवश्यकता होती है। इसके लिए आप looping statements को use करते है। Looping statements block statements होते है। Looping block में दिए गए statements तब तक repeatedly execute होते रहते है जब तक की कोई condition true होती है।

Looping statements में condition के अलावा एक loop control variable भी define किया जाता है। इस variable को loop की हर iteration में increase या decrease किया जाता है। ऐसा तब तक होता है जब तक की condition false नहीं हो जाती है। Loop condition भी इस variable द्वारा ही define की जाती है।

C# में available looping statements के बारे में निचे बताया जा रहा है।

While Loop

While loop block में दिए गए statement या statements को तब तक बार बार execute करता है जब तक की condition false नहीं हो जाती है।

Syntax:

loop control var declaration

while(condition with loop control var)
{
     //Statements to be executed while condition is true.
     //Increment or decrement loop control var
}

Example:

int Num = 1;

while(n>5)
{
    Console.WriteLine(“Num is : {0}”,Num)
    Num++;
}

do While Loop

Do while loop भी तब तक execute होता है जब तक की condition false नहीं हो जाती है। While और do while loop में फर्क सिर्फ इतना होता है की do while loop चाहे condition true हो या false एक बार जरूर execute होता है। While loop की ही तरह do while loop के लिए भी loop control variable और increment part define किया जाता है।

Syntax:

do
{
    //Statements to be executed while condition is true.
    //Increment or decrement loop control var
}while(condition with loop control var);

Example:

int Num = 1;

do
{
     Console.WriteLine(“Num is : {0}”,Num);
     Num++;
}while(Num>5)

For Each Loop

For each loop का प्रयोग array या collection को traverse करने के लिए किया जाता है। For each loop के द्वारा आप array या collection elements को access और modify कर सकते है।

For each loop में एक variable define किया जाता है। इस variable के माध्यम से ही आप array elements को access और modify करते है। इसके अलावा foreach loop में in operator के बाद उस array या collection का नाम भी लिखा जाता जिसे आप traverse करना चाहते है।

Syntax:

array declaration

foreach(variable in array-name)
{
       //Code to access or modify array elements
}

Example:

int[] myArray = new int[]{1,2,3,4,5}

foreach(int var in myArray)
{
    Console.WriteLine(var);
}

For Loop

For loop सभी loops में सबसे अधिक use किया जाने वाला loop होता है। इस loop में आप एक ही बार में condition, loop control variable और increment part define करते है।

Syntax:

for(loop control var; condition ; increment)
{
     //Statements to be executed while condition is true.
}

Example:

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

Jumping Statements

Jumping statements का प्रयोग program में एक जगह से दूसरी जगह jump करने और block statement के execution को terminate करने के लिए किया जाता है। C# में available jumping statements के बारे में आगे बताया जा रहा है।

goto Statement

C# में goto statement program के execution को एक labeled statement पर transfer करता है। Labeled statement program में एक नाम और उसके आगे colon (:) लगाकर define किया जाता है।

जैसे ही compiler को कोई goto statement मिलता है वह execution को label पर transfer कर देता है इसके बाद program का execution उस label के बाद से ही शुरू होता है और उससे पहले के statements skip कर दिए जाते है।

Syntax:

...
...
goto label-name;
...
...
label-name :
...
...

Example:

...
...
goto myLabel;
...
...
myLabel :
...
...

continue Statement

Continue statement को loop के अंदर किसी iteration को skip करने के लिए प्रयोग किया जाता है। Loop की जिस iteration में continue statement execute होता है वह iteration pass हो जाती है और control next iteration पर चल जाता है।

Continue statement की मदद से किसी खास condition पर loop की किसी iteration को skip किया जा सकता है। उदाहरण के लिए आप चाहते है की loop की 3rd iteration execute ना हो तो इसके लिए आप continue statement को use कर सकते है।

Syntax:

loop
{
    continue;
}

Example:

for(int i=1;i<=5;i++)
{
     if(i==3)
     {
         Continue;
     }
     Console.WriteLine(i);
}

break Statement

C# में break statement का प्रयोग किसी loop या switch statement में किया जाता है। जब compiler को break statement मिलता है तो वह control उस loop या switch statement से बाहर transfer कर देता है।

Syntax:

loop
{
   break;
}

Example:

for(int i=1;i<=5;i++)
{
      if(i==3)
      {
          Console.WriteLine(i);
      }
}

return Statement

C# में return statement का प्रयोग method को terminate करने और value return करने के लिए किया जाता है।

Syntax:

method-name
{
    ...
    return;
}

Example:

int add(int a, int b)
{
      return a+b;
}