官术网_书友最值得收藏!

if...else if...else statement

The if...else statement is very important when you need to test multiple conditions. In this statement, the if statement evaluates first, then the else if statement, and at last the else block executes. Here, the if statement may or may not have the if...else statement or block; if...else always comes after the if block and before the else block. The else statement is the final code block in the if...else...if else...else statement, which indicates that none of preceding conditions evaluate to true.

Take a look at the following code snippet:

private static void IfElseIfElseStatementExample() 
{ 
WriteLine("if statement example."); 
Write("Enter character:"); 
char inputChar = Convert.ToChar(ReadLine()); 
 
if (char.ToLower(inputChar) == 'a') 
{ WriteLine($"Character {inputChar} is a vowel.");} 
elseif (char.ToLower(inputChar) == 'e') 
{ WriteLine($"Character {inputChar} is a vowel.");} 
elseif (char.ToLower(inputChar) == 'i') 
{ WriteLine($"Character {inputChar} is a vowel.");} 
elseif (char.ToLower(inputChar) == 'o') 
{ WriteLine($"Character {inputChar} is a vowel.");} 
elseif (char.ToLower(inputChar) == 'u') 
{ WriteLine($"Character {inputChar} is a vowel.");} 
else 
{ WriteLine($"Character '{inputChar}' is a consonant.");} 
} 

In the preceding code snippet, we have various if...else if...else statements that evaluate the expression: whether inputchar is equivalent to comparative characternot. In this code, if you enter a character other than a,e,i,o,u that does not fall in any of the preceding condition, then the case else code block executes and it produces the final result. So, when else executes, it returns the result by saying that the entered character is a consonant.

主站蜘蛛池模板: 理塘县| 鄂尔多斯市| 红桥区| 府谷县| 梁平县| 敦化市| 广饶县| 裕民县| 阳江市| 军事| 自治县| 城市| 远安县| 泸水县| 黑山县| 凤台县| 宁远县| 疏附县| 通化县| 南江县| 长岛县| 建宁县| 平度市| 福建省| 枣庄市| 永泰县| 邵武市| 兰考县| 高唐县| 文水县| 且末县| 德庆县| 新兴县| 筠连县| 常宁市| 屏东市| 合川市| 富顺县| 余干县| 太白县| 邵武市|