- Learn C# in 7 days
- Gaurav Aroraa
- 183字
- 2021-07-08 09:51:29
Switch statement
This is a statement that provides a way to select an expression using switch statement that evaluates the conditions using case blocks when code does not fall in any of the case blocks; then, the default block executes (default block is an optional block in switch...case statement).
Switch statement is also known as an alternative to if...else if...else statement. Let's rewrite our examples used in the previous section to showcase the if...else if...else statement:
private static void SwitchCaseExample() { WriteLine("switch case statement example."); Write("Enter character:"); charinputChar = Convert.ToChar(ReadLine()); switch (char.ToLower(inputChar)) { case'a': WriteLine($"Character {inputChar} is a vowel."); break; case'e': WriteLine($"Character {inputChar} is a vowel."); break; case'i': WriteLine($"Character {inputChar} is a vowel."); break; case'o': WriteLine($"Character {inputChar} is a vowel."); break; case'u': WriteLine($"Character {inputChar} is a vowel."); break; default: WriteLine($"Character '{inputChar}' is a consonant."); break; } }
In the preceding code, the default block will execute if none of the case evaluates to true. The switch...case statement will be discussed in detail on day three.
There is a slight difference when you're choosing between switch...case and if...else. Refer to https://stackoverflow.com/questions/94305/what-is-quicker-switch-on-string-or-elseif-on-type for more details.
- 現(xiàn)代C++編程:從入門到實(shí)踐
- 編程卓越之道(卷3):軟件工程化
- AWS Serverless架構(gòu):使用AWS從傳統(tǒng)部署方式向Serverless架構(gòu)遷移
- 64位匯編語言的編程藝術(shù)
- Mastering Python Networking
- Big Data Analytics
- Android項(xiàng)目實(shí)戰(zhàn):手機(jī)安全衛(wèi)士開發(fā)案例解析
- Node Cookbook(Second Edition)
- Java程序設(shè)計案例教程
- HTML+CSS+JavaScript網(wǎng)頁設(shè)計從入門到精通 (清華社"視頻大講堂"大系·網(wǎng)絡(luò)開發(fā)視頻大講堂)
- Learning JavaScript Data Structures and Algorithms(Second Edition)
- FFmpeg開發(fā)實(shí)戰(zhàn):從零基礎(chǔ)到短視頻上線
- Python計算機(jī)視覺和自然語言處理
- OpenCV with Python Blueprints
- 算法精解:C語言描述