Basic usage of Switch-case

Source: Internet
Author: User

The If statement handles two branches and uses the IF-ELSE-IF structure when dealing with multiple branches, but if there are more branches, the more nested if statement layers, The procedure is not only large but also difficult to understand. Deeply nested else-if statements tend to be syntactically correct, but logically do not correctly reflect the programmer's intentions. For example, an incorrect else-if match can easily be ignored. Adding new conditional and logical relationships, or making other changes to statements, makes it difficult to guarantee correctness. As a result, the C + + language provides a conditional selection statement specifically for processing a multi-branch structure called a switch statement, also known as a switch statement. It is easy to implement deep nested if/else logic.

The general form of a switch statement is:

Switch ( expression )
{
case constant Expression 1:
Statement 1;
Break

Case constant Expression 2:
Statement 2;
Break

......
case constant Expression N:
Statement N;
Break

Default
Statement n+1;
Break
}

A few days earlier to do a switch statement related job problems, then inexplicably think of the case can be followed by the range expression , because the feeling is also a constant expression bar . Search for a long time to find a few related issues, most of the answer or not, the results in a few answers said is the GCC standard compile can, the code is as follows (this is I run the compilation on the DEVC, success, TC3.0 will not be):

1#include <stdio.h>2 intMain ()3 { 4 CharMy_string[] ="Hello there"; 5 Charch;6 7printf"Please input a char:");8CH =GetChar ();9 Ten Switch(CH) One  { A    Case '0'...'9':  -printf"You enter a digit\n"); -  Break; the    Case 'a'...'Z': -printf"You enter a lower case letter\n"); -        Break; -    Case 'A'...'Z': +printf"You enter a upper case letter\n"); -        Break; +   default: Aprintf"I don ' t know what do you enter:-) \ n"); at        Break; -  } -}

Operation Result:

DEVC run successfully, while TC3.0 does not allow "..." This symbol:

It depends on the compiler.

But see so many people categorically say no is also the heart has feelings, should all know the compiler has a variety of it, but still can be so sure of the negative, especially in the upstairs has been released successfully run the case of the answer. There are people who tell the story clearly said no, but this is not going to make sense?

The principle of the machine, thinking is now also can be said to be ignorant, since not familiar with, it should not be easy to say the absolute words. The book never said "..." The use of this thing, so today for me is like discovering a new world, even if only a small symbol of the use of

"END"

Basic usage of Switch-case

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.