- Learn C# in 7 days
- Gaurav Aroraa
- 238字
- 2021-07-08 09:51:28
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.
- Mastering RabbitMQ
- Microsoft Application Virtualization Cookbook
- React Native Cookbook
- 算法精粹:經(jīng)典計(jì)算機(jī)科學(xué)問題的Java實(shí)現(xiàn)
- RTC程序設(shè)計(jì):實(shí)時(shí)音視頻權(quán)威指南
- Mastering Julia
- Mastering Rust
- Learning Python by Building Games
- 學(xué)Python也可以這么有趣
- Learning Vaadin 7(Second Edition)
- 軟件測試綜合技術(shù)
- 計(jì)算機(jī)應(yīng)用基礎(chǔ)項(xiàng)目化教程
- Web App Testing Using Knockout.JS
- Natural Language Processing with Python Quick Start Guide
- Building Business Websites with Squarespace 7(Second Edition)