在跳到 if 語句之前,讓我們先來了解一下 C# 程式的基本結構。
印出 C# if 語句作為輸出。
using System; //declaring namespace class Example1 //declaring class { static void Main(string[] args) { //declaring class method Console.WriteLine("C# IF STATEMENT"); //print } }
輸出:
本文主要關注 C# IF 語句,所以讓我們一步一步來。
條件 if 語句接受布林表達式或括號內的條件或作為參數,後面接著單行或多行程式碼區塊。在運行時,當程式被執行時,括號內的條件將被評估。如果該布林表達式的結果為 true,則會執行 if 語句後面的程式碼區塊。
考慮以下範例,其中 if 條件包含 true 作為表達式。
if 語句的語法是 –
if(a conditional statement or boolean expression) { // the block of code to be executed if the expression results into true }
讓我們透過一個例子進一步理解這一點。
考慮 –
using System; class Ex2 { static void Main(string[] args) { { if(true) Console.WriteLine("True Condition: We are inside the for loop"); if(false) Console.WriteLine("False Condition: We will not be able to enter inside the for loop"); } } }
例如 –
using System; class Ex3 { static void Main(string[] args) { int R_age = 15, A_age = 12; if ( R_age > A_age) Console.WriteLine("Ravi is elder to Amar"); if (R_age <p><strong>輸出</strong> –</p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172534736646938.png?x-oss-process=image/resize,p_40" class="lazy" alt="C# if 語句" ></p> <p>請注意,第一個「if」語句中的布林表達式會作為參數給出,當 Ravi 的年齡(15)大於 Amar 的年齡(12)時,計算結果為 true。由於只有一個 if 語句成立,因此只會執行與第一個 if 條件相關的第一個區塊。 </p> <h3>if-else 語句</h3> <p>C# 提供的第二種條件語句是 if-else 語句。程式碼的第二部分(如果條件為假則需要執行)可以保留在 else 區塊內。 else 區塊不能獨立存在。這表示 else 語句必須跟在 if 語句或 else if 語句之後。 else 語句在 if-else 語句鏈中只能使用一次。 </p> <p><strong>if-else 語句的語法為 –</strong></p> <pre class="brush:php;toolbar:false">if(a conditional statement or boolean expression) { // the block of code to be executed if the expression results into true } else { // executes when “if” exp is false }
例如 –
using System; class Ex4 { static void Main(string[] args) { int R_age = 12, A_age = 15; if ( R_age > A_age) Console.WriteLine("Ravi is elder to Amar"); else Console.WriteLine("Ravi and Amar are of the same age"); } }
輸出:
現在,您一定已經注意到,作為參數給出的第一個「if」語句中的布林表達式的計算結果為 false,因為 Ravi 的年齡(12)小於 Amar 的年齡(15)。與 if 語句為 false 一樣,將執行第二個區塊,即與 else 條件關聯的程式碼區塊。
C# 提供的第二種條件語句是 else if 語句。如果要檢查的給定條件不只一個,那麼 else-if 條件就會出現。
Consider –
using System; class Ex5 { static void Main(string[] args) { int R_age = 12, A_age = 15; if ( R_age > A_age) Console.WriteLine("Ravi is elder"); else if (R_age <p><strong>Output:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172534737077082.png?x-oss-process=image/resize,p_40" class="lazy" alt="C# if 語句" ></p> <h3>Nested If</h3> <p>Nested if the statement is an if statement within an if statement.</p> <p><strong>For Example –</strong></p> <pre class="brush:php;toolbar:false">using System; class Ex6 { static void Main(string[] args) { int R_age = 12, A_age = 15; if(R_age != A_age) //yields true as 12 is not equal to 15 { if( R_age <p><strong>Output:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172534737228914.png?x-oss-process=image/resize,p_40" class="lazy" alt="C# if 語句" ></p> <h3>Conclusion</h3> <p>The if-else or else-if statement evaluates the boolean expression and, based on the result, controls the flow of the program.</p>
以上是C# if 語句的詳細內容。更多資訊請關注PHP中文網其他相關文章!