Selection Statements (If, Switch) for Flow Control

C/C++ supports two types of selection statements: if and switch. In addition, the ? operator is an alternative to if in certain circumstances for flow control.
if
The Syntax of  if statement is

if (expression) statement;
else statement;

if

where a statement may consist of a single statement, a block of statements, or nothing in the case of empty statements). The else clause is optional. If an expression evaluates to true (anything other than 0), the statement or block that forms the target off if is executed; otherwise, the statement or block that is the target of else will be executed if it exists. Remember, only the code associated with if or the code associated with else executes, never both.

In case of branch of activity, statement are written in a parenthesis {} like that:

Syntax:
if(expreassion)
{
//statement 1
   //…….
   //statement n
}
else
{
//statement 1
   //…….
   //statement n
}

 

 if ( TRUE )
{

    /* Execute these statements if true */
}
else
{

    /* Execute these statements if FALSE */
}

It has the ability to control the flow of your program. The if statement allows you to control if a program enters a section of code or not based on whether a given condition is true or false. One of the important functions of the if statement is that it allows the program to select an action based on the user’s input. For example, by using an if statement to check a valid parameter like age is greater than 18+ or not to execute or enter the segment.

When programming, the aim of the program will often require the checking of one value stored in a variable against another value to determine whether one is larger, smaller, or equal to the other.

There are a number of operators that allow these checks.

Here are the relational operators, as they are known, along with examples:

>     greater than              10 > 9 is TRUE
<     less than                 15 < 19 is TRUE
>=    greater than or equal     10 >= 10 is TRUE
<=    less than or equal        1 <= 3 is TRUE
==    equal to                  7 == 7 is TRUE
!=    not equal to              15 != 7 is TRUE

 

Else if

Another use of else is when there are multiple conditional statements that may all evaluate to true, yet you want only one if statement’s body to execute. You can use an “else if” statement following an if statement and its body; that way if the first statement is true, the “else if” will be ignored, but if the if statement is false, it will then check the condition for the else if statement. If the if statement was true the else statement will not be checked. It is possible to use numerous else if statements to ensure that only one block of code is executed.

Let’s look at a simple program for you to try out on your own.

 #include <stdio.h> 

int main() 
{
 int sub; 
 
 printf( "Please enter any subject marks" ); /* Asks for marks obtain in any one subject */
 scanf( "%d", &sub ); /* The input is put in sub */
 if ( sub < 50 ) { /* check given no is less then 50 */
 printf ("You obtain D\n" ); /* Just to show you it works... */
 }
 else if ( sub <60 ) { /* I use else just to show an example */ 
 printf( "You obtain C\n" ); 
 }
 else if ( sub <70 ) { /* I use else just to show an example */ 
 printf( "You obtain B\n" ); 
 }
 else if ( sub <80 ) { /* I use else just to show an example */ 
 printf( "You obtain A\n" ); 
 }
 else {
 printf( "Woow! you get A+\n" ); /* Executed if no other statement is */
 }
 return 0;
}

 

Nested ifs

 A nested if is an if that is the target of another if or else. Nested ifs are very common in programming. In a nested if, an else statement always refers to the nearest if statement that is within the same block as the else and that is not already associated with an else. For example

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
   int magic; /* magic number */
   int guess; /* user's guess */
   magic = rand(); /* get a random number */
   printf("Guess the magic number: ");
   scanf("%d", &guess);
   if (guess == magic) {
      printf("** Right **");
      printf(" %d is the magic number\n", magic);
   }
   else {
      printf("Wrong, ");
      if(guess > magic) printf("too high\n");
      else printf("too low\n");
   }
return 0;
}

 

switch

C/C++ has a built-in multiple-branch selection statement, called switch, which successively tests the value of an expression against a list of integer or character constants. When a match is found, the statements associated with that constant are executed. The general form of the switch statement is

switch (expression) 
{case constant1: statement sequence break;
 case constant2: statement sequence break;
 case constant3: statement sequence break;
 ..
 default statement sequence
}

 The expression must evaluate to a character or integer value. Floating-point expressions, for example, are not allowed. The value of an expression is tested, in order, against the values of the constants specified in the case statements. When a match is found, the statement sequence associated with that case is executed until the break statement or the end of the switch statement is reached. The default statement is executed if no matches are found. The default is optional and, if it is not present, no action takes place if all matches fail.

void menu(void)
{
   char ch;
   printf("1. Check Spelling\n");
   printf("2. Correct Spelling Errors\n");
   printf("3. Display Spelling Errors\n");
   printf("Strike Any Other Key to Skip\n");
   printf(" Enter your choice: ");
   ch = getchar(); /* read the selection from
                    the keyboard */
   switch(ch) {
      case '1':
      printf("You have selected Check spelling option);
      break;
      case '2':
      printf("You have selected Correct Spelling Errors option);
      break;
      case '3':
      printf("You have selected Display Spelling Errors option);
      break;
      default :
      printf("No option selected");
  }
}

 

The following two tabs change content below.

Subroto Mondal

Chief Coordinator HR&CR
I like Programming and New Technologies. And work with Linux.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.