دستور if else در سی شارپ
دستور if else در سی شارپ مشابه دستور if می باشد با این تفاوت که در صورت درست نبودن شرط بررسی شده در if ، کدهای موجود در بلوک else اجرا می شوند.
نحوه نوشتن دستور if else
در زیر Syntax یک دستور شرطی if else را مشاهده می کنید:
1 2 3 4 5 | 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 اجرا خواهند شد.
دیاگرام دستور if else
مثال
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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(); } } } |
زمانی که کد بالا توسط کامپایلر سی شارپ ، کامپایل و اجرا شود، نتیجه زیر را تولید خواهد کرد:
1 2 | a is not less than 20; value of a is : 100 |
دستور شرطی if else if else
از این دستور شرطی در مواقعی استفاده می شود که قصد ارزیابی چند شرط مختلف را داریم.
زمانی که از دستور if else if else استفاده می کنید، باید به نکات زیر توجه داشته باشید:
- یک دستور if می تواند صفر یا یک بخش else داشته باشد و بخش else همیشه باید بعد از else if ها نوشته شود.
- یک دستور if می تواند صفر یا چند بخش else if داشته باشد و بخش else if همیشه باید قبل از بخش else نوشته شود.
- زمانی که یکی از else if ها درست باشد و اجرا شود، سایر else if ها و else ارزیابی نمی شوند.
نحوه نوشتن if else if else
در زیر Syntax یک دستور شرطی if else if else در زبان سی شارپ را مشاهده می کنید:
1 2 3 4 5 6 7 8 9 10 11 | 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 */ } |
مثال
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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(); } } } |
زمانی که کد بالا کامپایل و اجرا شود، نتیجه زیر را تولید خواهد کرد:
1 2 3 | None of the values is matching Exact value of a is: 100 |
هیچ نظری ثبت نشده است