Explain the usage of if else and switch in C++ programming

  • 2020-05-07 20:09:59
  • OfStack

if - else statements
controls conditional branches.
grammar


  if ( expression )
 statement1
[else
 statement2]

note
If the value of expression is not zero, statement1 is executed. If the option else exists, and if the value of expression is zero, statement2 is executed. The expression must be an arithmetic or pointer type, or it must be a class type with a well-defined integer or pointer type conversion. For information about converters, see standard conversions.
Calculated in two forms, if and expression statements, it can have any value other than structure, including all side effects. The control cannot be passed from the if statement to the next statement in the project unless one of the statement contains break, continue, or goto.
if... The else clause of the else statement is related to the nearest if statement in the same range that does not have a corresponding else statement.
To make this example clear about if... else pair, 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 will not be executed
 }

 x = 1;
 if (x == 0)
 {
  printf_s("x is 0!\n"); // this statement will not be executed
 }
 else
 {
  printf_s("x is not 0!\n");
 }

 return 0;
}

Output:


x  is  0 ! 
x  not  0!

switch statement
allows you to select between multiple code segments based on the value of an integer expression.
grammar


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

note
expression must belong to an integer or a class type that exists an explicit conversion to an integer. An integer lift is performed in the manner described in the integer lift.
The switch body consists of 1 series case tags and 1 optional default tag. Two constant expressions in the case statement cannot be evaluated with the same value. default tags can only appear once. Markup statements are not required by the syntax, but switch statements are meaningless if they do not exist. The default statement does not need to be displayed at the end; It can be displayed anywhere in the switch body. case or default tags can only be displayed in switch statements.
constant-expression in each case tag will be converted to expression type and will be compared with expression for equivalence. Control the passing of statements to whose case constant-expression values match expression. The generated behavior is shown in the following table.
switch statement behavior

条件 操作
转换后的值与提升的控制表达式的值匹配。转换后的值与提升的控制表达式的值匹配。 控制将转移到跟在该标签后面的语句。
没有常量与 case 标签中的常量匹配;default 标签存在。 控制将转移到 default 标签。
没有常量与 case 标签中的常量匹配;default 标签不存在。 控制将转移到 switch 语句之后的语句。

If a matching expression is found, the subsequent case or default tags do not interfere with control. The break statement is used to stop execution and transfer control after the switch statement. If there is no break statement, every statement from the matching case tag to the end of switch, including default, is executed. Such as:


// 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 above example, if c is uppercase A, capa is incremented. The break statement after capa++ terminates the switch body and transfers control to the while loop. If there is no break statement, lettera and nota are also incremented. The break statement of case 'a' serves a similar purpose. If c is lowercase a, lettera is incrementalized, and break statement terminates the switch body. If c is not a or A, an default statement is executed.
The internal blocks of the switch statement can contain definitions with initializations provided they are accessible - that is, all possible execution paths do not bypass them. The names introduced using these declarations have local scopes. Such as:


// 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 this case, the case or default tags are associated with the nearest switch statement that encapsulates them.


Related articles: