Detailed explanation of conditional judgment statement if-else and switch usage in C + + programming _c language

Source: Internet
Author: User
Tags case statement constant terminates

If-else statement
control Condition Branch.
Grammar

  if (expression)
 statement1
[else
 Statement2]

Note
If the value of the expression is Non-zero, the statement1 is executed. If the option else exists, Statement2 is executed if the value of expression is zero. An expression must be an arithmetic or pointer type, or it must be a class type that defines an explicit integer or pointer type conversion. For information about converters, see Standard conversions.
Evaluated in two-form if statements and expression statements, you can have any value other than the struct, including all side effects. A control can be passed from an if statement to the next statement in the project unless one of the statement contains a break, continue, or Goto.
The ELSE clause of the IF...ELSE statement is associated with the closest preceding if statement in the same range without the corresponding Else statement.
To make this example explicit about If...else pairing, uncomment the braces.

If_else_statement.cpp
#include <stdio.h>

int main () 
{
 int x = 0;
 if (x = = 0)
 {
  printf_s ("x is 0!\n");
 }
 else
 {
  printf_s (' x is not 0!\n ');//This statement would not be executed
 }

 x = 1;
 if (x = = 0)
 {
  printf_s (' x is 0!\n ');//This statement won't be executed
 }
 else
 {
  printf_ S ("X is not 0!\n");
 }

 return 0;
}

Output:

X is 0!
x is not 0!

Switch statement
allows you to select from multiple code snippets based on the value of an integer expression.
Grammar

 switch (expression) case
constant-expression:statement
[default:statement]

Note
Expression must belong to an integral type or to an explicitly converted class type that exists to an integral type. An integral elevation is performed in the manner described in the integral type elevation.
The switch statement body consists of a series of case labels and an optional default label. Two constant expressions in a case statement cannot be evaluated with the same value. The default label can appear only once. Tag statements are not syntactic requirements, but if they do not exist, the switch statement is meaningless. The default statement does not need to be displayed at the end, and it can be displayed anywhere in the switch statement body. The case or default label can only be displayed within a switch statement.
The constant-expression in each case label is converted to the expression type and is equivalent to the expression comparison. Controls the delivery of statements that match the case constant-expression to the value of expression. The generated behavior is shown in the following table.
Switch statement behavior

condition Operation
The converted value matches the value of the promoted control expression. The converted value matches the value of the promoted control expression. Control is transferred to the statement following the tag.
No constants match the constants in the case label; Control is transferred to the default label.
No constants match the constants in the case label; Controls the statement that will be transferred after the switch statement.

If a matching expression is found, subsequent case or default labels will not interfere with control. The break statement is used to stop execution and transfer control to the statement after the switch statement. If there is no break statement, each statement from the matching case label to the end of the switch is executed, including default. For example:

Switch_statement1.cpp
#include <stdio.h>

int main () {
 char *buffer = "any character stream";
 int CAPA, Lettera, Nota;
 char c;
 CAPA = Lettera = Nota = 0;

 while (c = *buffer++)//walks buffer until NULL
 {
  switch (c)
  {case
   ' A ':
   capa++;
   break;
   Case ' a ':
   lettera++;
   break;
   Default:
   nota++;
  }
 }
 printf_s ("\nuppercase A:%d\nlowercase A:%d\ntotal:%d\n",
  Capa, Lettera, (capa + Lettera + nota));


In the example above, if C is uppercase A, the CAPA is incremented. The break statement after capa++ terminates execution of the switch statement body and transfers control to the while loop. If there are no break statements, Lettera and Nota will also be incremented. The break statement of case ' a ' can also achieve a similar goal. If c is lowercase a, the lettera is incremented and the break statement terminates the switch statement body. If c is not a or a, the default statement is executed.
The inner block of a switch statement can contain a definition with initialization, provided that you can access them-that is, all possible execution paths do not bypass them. Names introduced with these declarations have a local scope. For example:

Switch_statement2.cpp
//C2360 expected
#include <iostream>
using namespace std;
int main (int argc, char *argv[])
{
 switch (ToLower (*argv[1))
 {
  //Error. Unreachable declaration.
  Char szchentered[] = "Character entered was:";

 Case ' A ':
  {
  //declaration of szchentered OK. Local scope.
  Char szchentered[] = "Character entered was:";
  cout << szchentered << "a\n";
  }
  break;

 Case ' B ':
  //Value of szchentered undefined.
  cout << szchentered << "b\n";
  break;

 Default:
  //Value of szchentered undefined.
  cout << szchentered << "Neither a nor b\n";
  break;
 }
}

Switch statements can be nested. In such cases, the case or default label is associated with the most recent switch statement that encapsulates them.

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.