Introduction to C++ Selection Structure

- February 12, 2018
Selection structures are how C++ programs
make decisions according to the conditional statement. When a control expression in an if statement is evaluated to be true, the statements associated with the structure are executed.
The selection structure is the type of control structure that is used for selecting a statement or set of statements for execution on the basis of a given condition.
Simply the statements of the program are written in a sequence but selection structure are used for making decisions. That's why, this structure is also called decision-making structure or conditional structure.
The condition is given within the selection structure that is tested by computer, if the given condition is true, then the set of statements under the selection statement is executed, otherwise it will be ignore.
An algorithm with selection structure is looks like this:
IF condition THEN
      Statement-1
ELSE
      Statement-2
END IF
Diagram of c++ selection structure

In the above algorithm, if the "condition" is true, then "Statement-1" will be execute otherwise "Statement-2" will run. We can also specify group of statements instead of "Statement-1" and statement-2.

In C++, there are three types of selection structures:


To get a better idea of how selection
structures work, look at the following example of C++ selection structure:

Selection Structure Example Program

The following program takes a number from the user and tests whether a given number is odd or even using if selection structure.
#include<iostream.h>
using namespace std;
main()
 {
      int num;
      cout<<"Enter an integer number:";
      cin>>num;
      if (num%2==0) cout<<"This is Even Value"<<endl; // this is the condition for even
      if (num%2==1) cout<<"This is Odd Value"; //this is the condition for odd
      return 0;
 } //end main function
 

Start typing and press Enter to search