Selection Structure (if..else / switch)

Selection control for Making Decision

if..else 

Basic Statement

if (condition)
{
// statement;
// statement;
}

else
// statement;

Example

Pseudocode for if..else statement


    1.  START
    2.  READ NUM1, NUM2
    3.  IF (NUM1 > NUM2)
    3.1   START IF
       3.1.1   DISPLAY “FIRST NUMBER ENTERED BIGGER”
    3.2   END IF
    4.  ELSE
    4.1   DISPLAY “SECOND NUMBER IS BIGGER”
    5.  END


C++ Program



Single-if

Basic Statement


if (condition)
    // statement ;
if (condition)
    // statement ;
if (condition)
    // statement;

else
   // statement ;

Example

Input : Mark
Process : If mark greater than and equal to 75, score will be A
    If mark less than 75 and greater than and equal to 60, score will be B
    If mark less than 60 and greater than and equal to 45, score will be C
    If mark less than 30, score will be D
Output : Print the grade of your score

C++ Program


Nested-if



Basic Statement


if (condition)
{
   if (condition)
   {
      // statement ;
      // statement ;
   } 
   if (condition)
      // statement;
   else
     // statement ;
}
else
    // statement ;

Example

Ask for two numbers. Assign the numbers to bigNumber and smallNumber. If bigNumber is bigger than smallNumber,check if they are evenly divisible. If they are, check if they are the same number.


C++ Program

 

switch

Basic Statement

switch (condition)
{
   case 1 :
      // statement ;
      // statement ;
   case 2 :

      // statement ;
      // statement ;
   case 3 :

      // statement ;
      // statement ;
  default :

      // statement ;
}


C++ Program





 
Copyright © C++ Lab