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.
Foundation of Computer Science A palindrome is a string that reads the same forward and backward....
Foundation of Computer Science A palindrome is a string that reads the same forward and backward. 1. Describe an algorithm that determines whether a string of n characters is a palindrome. 2. Write its corresponding program using your favorite programming language.
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?
Foundation of Computer Science Describe an algorithm that takes as input a list of n integers...
Foundation of Computer Science Describe an algorithm that takes as input a list of n integers and produces as output the largest difference obtained by subtracting an integer in the list from the one following it. Write its corresponding program using your favorite programming language
How do natural and artificial selection differ? Give an example of each.
How do natural and artificial selection differ? Give an example of each.
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.
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
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT