Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

What is Selection Structure in C++

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


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:

  • if selection structure
  • if-else selection structure
  • switch selection structure

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 if selection structure.

#include

#include

main()

 {

      int num;

      clrscr();

      cout
      cin>>num;

      if (num%2==0) cout
      if (num%2==1) cout
      getch();

 } //end main function




This post first appeared on Programming Explain, please read the originial post: here

Share the post

What is Selection Structure in C++

×

Subscribe to Programming Explain

Get updates delivered right to your inbox!

Thank you for your subscription

×