Codes Academy

use of if and switch statement in c++

Flow Of Control

if statement
syntax of the if statement
if (condition)
{
  statement(s);
}
From the flowchart it is clear that if the if condition is true, statement is executed; otherwise it is skipped. The statement may either be a single or compound statement.
if else statement
syntax of the if - else statement
if (condition) 
  statement1; 
else 
  statement2;
From the above flowchart it is clear that the given condition is evaluated first. If the condition is true, statement1  is executed. If the condition is false, statement2 is executed. It should be kept in mind that statement and statement2 can be single or compound statement.
if example
if else example
if (x == 100)
    cout << "x is 100";
if (x == 100)
    cout << "x is 100";
else
    cout << "x is not 100";
Nested if statement
The if block may be nested in another if or else block. This is called nesting of if or else block.
syntax of the nested if statement
if(condition 1) 

  if(condition 2) 
  { 
    statement(s); 
  } 
}
if(condition 1) 
  statement 1; 
else if (condition 2) 
  statement2; 
else
  statement3;

if-else-if example
if(percentage>=60)
     cout<<"Ist division";
else if(percentage>=50)
     cout<<"IInd division";
else if(percentage>=40)
     cout<<"IIIrd division";
else
     cout<<"Fail" ;
switch statement
The if and if-else statements permit two way branching whereas switch statement permits multiple branching. The syntax of switch statement is:
switch (var / expression) 

   case constant1 : statement 1; 
   break; 
   case constant2 : statement2; 
   break; 
   .
   .
   default: statement3; 
   break; 
}
The execution of switch statement begins with the evaluation of expression. If the value of expression matches with the constant then the statements following this statement execute sequentially till it executes break. The break statement transfers control to the end of the switch statement. If the value of expression does not match with any constant, the statement with default is executed.
Some important points about switch statement
·         The expression of switch statement must be of type integer or character type.
·         The default case need not to be used at last case. It can be placed at any place.
·         The case values need not to be in specific order.



Related Posts:

  • looping statements in c++ Looping statement It is also called a Repetitive control structure. Sometimes we require a set of statements to be executed a number of times by changing the value of one or more variables each time to obtain a different r… Read More
  • CLASSES AND OBJECT IN C++ C++ CLASSES AND OBJECTS Class: The building block of C++ that leads to Object Oriented programming is a Class. It is a user defined data type, which holds its own data members and member functions, which can be a… Read More
  • use of if and switch statement in c++ Flow Of Control if statement syntax of the if statement if (condition) {   statement(s); } From the flowchart it is clear that if the if condition is true, statement is executed; otherwise it is skipped. The state… Read More
  • c++ functions C++ Standard Library Function  The C++ Standard Library provides a rich collection of functions for performing common mathematical calculations, string manipulations, character manipulations, input/output, error chec… Read More
  • OOP CONCEPT IN C++ C++ and Object Oriented Programming Object Oriented programming is a programming style that is associated with the concept of Class, Objects and various other concepts revolving around these two, like Inheritance, Polym… Read More

0 comments:

Post a Comment