سه شنبه ۲۰ شهريور ۱۴۰۳
Tut24 آموزش برنامه نویسی و مجله تخصصی فناوری ورود/عضویت

آموزش سی شارپ - دستور if...else

یک دستور if می تواند توسط یک دستور else اختیاری دنبال شود که هنگامی که عبارت منطقی غلط باشد، اجرا می شود.

نحو

نحو دستور if...else در C# به شکل زیر است:


if(boolean_expression) {
   /* statement(s) will execute if the boolean expression is true */
} else {
   /* statement(s) will execute if the boolean expression is false */
}

اگر عبارت منطقی به true تبدیل شود، سپس بلوک کد if اجرا می شود، در غیر این صورت بلوک کد else اجرا می شود.

نمودار جریان

C# دستور if...else

مثال


using System;

namespace DecisionMaking {
   class Program {
      static void Main(string[] args) {
         /* local variable definition */
         int a = 100;
         
         /* check the boolean condition */
         if (a < 20) {
            /* if condition is true then print the following */
            Console.WriteLine("a is less than 20");
         } else {
            /* if condition is false then print the following */
            Console.WriteLine("a is not less than 20");
         }
         Console.WriteLine("value of a is : {0}", a);
         Console.ReadLine();
      }
   }
}

زمانی که کد بالا کامپایل و اجرا می شود، نتیجه زیر تولید می شود:


a is not less than 20;
value of a is : 100

دستور if...else if...else

یک دستور if می تواند توسط یک دستور else if...else اختیاری دنبال شود که با استفاده از یک دستور if...else if، شرایط مختلفی را برای آزمایش تست کردن با یک دستور if...else if انجام دهیم.

هنگام استفاده از دستور if، else if، else چند نکته را به خاطر داشته باشید:

  • یک if می تواند صفر یا یک else داشته باشد و باید بعد از هر else if باشد.

  • یک if می تواند صفر تا بی نهایت else if داشته باشد و باید قبل از else باشند.

  • یکبار که یک else if موفق شد، هیچ یک از else if های باقیمانده یا else ها آزمایش نخواهند شد.

نحو

نحو دستور if...else if...else در C# به شکل زیر است:


if(boolean_expression 1) {
   /* Executes when the boolean expression 1 is true */
} 
else if( boolean_expression 2) {
   /* Executes when the boolean expression 2 is true */
} 
else if( boolean_expression 3) {
   /* Executes when the boolean expression 3 is true */
} else {
   /* executes when the none of the above condition is true */
}

مثال


using System;

namespace DecisionMaking {
   class Program {
      static void Main(string[] args) {
         /* local variable definition */
         int a = 100;
         
         /* check the boolean condition */
         if (a == 10) {
            /* if condition is true then print the following */
            Console.WriteLine("Value of a is 10");
         } 
         else if (a == 20) {
            /* if else if condition is true */
            Console.WriteLine("Value of a is 20");
         } 
         else if (a == 30) {
            /* if else if condition is true  */
            Console.WriteLine("Value of a is 30");
         } else {
            /* if none of the conditions is true */
            Console.WriteLine("None of the values is matching");
         }
         Console.WriteLine("Exact value of a is: {0}", a);
         Console.ReadLine();
      }
   }
}

هنگامی که کد بالا کامپایل و اجرا می‌شود، نتیجه زیر را تولید می‌کند −


None of the values is matching
Exact value of a is: 100