Question

In: Computer Science

Foundation of computer science Discuss the selection constructs. Give an illustrative example for each one of...

Foundation of computer science

Discuss the selection constructs. Give an illustrative example for each one of them.

Solutions

Expert Solution

It is also known as conditional construct. It is used to indicate decision in a program. There are different kinds of selection constructs. They are

  • IF …THEN ….
  • IF THEN … ELSE ..
  • IF … THEN … ELSEIF….
  • MULTIPLE SELECTION

1. IF ....THEN CONSTRUCT :

This structure is also called as the ‘one-way branch ‘ decision structure.

IF test-condition then

Statement 2

Endif

In some situations, certain statements have to be executed only if a certain condition is met. In such cases, a test condition is specified. The test-condition is a Boolean expression which when tested results in TRUE or FALSE. When the condition is TRUE, statement enclosed between THEN and ENDIF are executed. If the condition is false, the statements are ignored and control is transferred to the statement following the if..then structure.

Example showing If Then:

2. If ..Then ….Else Construct

This structure is also called as the ‘two-way branch” decision structure

If test-condition then

Statement 1

Else

Statement 2

Endif

If test-condition results in TRUE value, statement 1 is executed. If it is FALSE, statement 2 is executed. Statement 1 , Statement 2 can either be a single statement or a set of simple statements.

Example showing If ..Then ….Else:


3. IF … THEN … ELSEIF…. Construct

A nested if is an if statement that is the target of another if statement.

if (condition1):
   # Executes when condition1 is true
   if (condition2): 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here

Example showing If ..Then ….Else:

4. MULTIPLE SELECTION

switch (n)
{
    case 1: // code to be executed if n = 1;
        break;
    case 2: // code to be executed if n = 2;
        break;
    default: // code to be executed if n doesn't match any cases
}

Example showing MULTIPLE SELECTION


Related Solutions

Foundation of Computer Science Discuss the repetition constructs. Give an illustrative example for each one of...
Foundation of Computer Science Discuss the repetition constructs. Give an illustrative example for each one of them.
Briefly discuss the purpose of each of the following. Give one brief example of each.: a....
Briefly discuss the purpose of each of the following. Give one brief example of each.: a. Tests of controls b. Analytical procedures c. Tests of details of balances
Differentiate between a foundation species and a keystone species. Give an example of each. What is...
Differentiate between a foundation species and a keystone species. Give an example of each. What is an invasive species? Give an example. What is a pioneer species, and under what circumstances are these species important?
How do natural and artificial selection differ? Give an example of each.
How do natural and artificial selection differ? Give an example of each.
Present and discuss an illustrative example in a specific country of Neoclassical policy in Macroeconomics
Present and discuss an illustrative example in a specific country of Neoclassical policy in Macroeconomics
Discuss the applications of PDE's in Engineering, Science and Economics with a detailed example for each....
Discuss the applications of PDE's in Engineering, Science and Economics with a detailed example for each. In at least one example, discuss the solution to the PDE and how it has practical implications.
Give a concrete example of adverse selection and moral hazard each in financial markets.
Give a concrete example of adverse selection and moral hazard each in financial markets.
Please DISCUSS The types of demand deposits Please give an example from each one
Please DISCUSS The types of demand deposits Please give an example from each one
What are the primary sources of American Law and give one example of each?  Give an example...
What are the primary sources of American Law and give one example of each?  Give an example of one secondary source.  
whats the type of loops and give example of each one ?
whats the type of loops and give example of each one ?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT