1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
sashaice [31]
3 years ago
15

Describe a strategy for avoiding nested conditionals. Give your own example of a nested conditional that can be modified to beco

me a single conditional, and show the equivalent single conditional.
Computers and Technology
1 answer:
harina [27]3 years ago
6 0

Answer:

One of the strategies to avoid nested conditional is to use logical expressions such as the use of AND & operator.

One strategy is to use an  interface class with a method. That method can be created to be used for a common functionality or purpose. This is also called strategy design pattern. You can move the chunk of conditional statement to that method. Then each class can implement that interface class and use that shared method according to their own required task by creating objects of sub classes and call that common method for any such object. This is called polymorphism.

Explanation:

Nested conditionals refers to the use of if or else if statement inside another if or else if statement or you can simply say a condition inside another condition. For example:

if( condition1) {  

//executes when condition1 evaluates to true

 if(condition2) {

//executes when condition1  and condition2 evaluate to true

 }  else if(condition3) {

 //when condition1 is true and condition3 is true

} else {

 //condition1  is true but neither condition2 nor conditions3 are true

}  }

The deeply nested conditionals make the program difficult to understand or read if the nested conditionals are not indented properly. Also the debugging gets difficult when the program has a lot of nested conditionals.

So in order to avoid nested conditionals some strategies are used such as using a switch statement.

Here i will give an example of one of the strategies i have mentioned in the answer.

Using Logical Expressions:

A strategy to avoid nested conditionals is to use logical expressions with logical operators such as AND operator. The above described example of nested conditionals can be written as:

if(condition1 && condition2){  //this executes only when both condition1 and condition2 are true

} else if(condition1 && condition3) {

this executes only when both condition1 and condition3 are true

} else if(condition1 ){

//condition1  is true but neither condtion2 nor condtion3 are true  }

This can further be modified to one conditional as:

if(!condition3){

// when  condition1 and condition2 are true

}

else

// condition3 is true

Now lets take a simple example of deciding to go to school or not based on some conditions.

if (temperature< 40){

  if (busArrived=="yes")   {

      if (!sick)       {

          if (homework=="done")           {

              printf("Go to school.");

          }

      }     

  }

}

This uses nested conditionals. This can be changed to a single conditional using AND logical operator.

if ((temperature <40) && (busArrived=="yes") &&

(!sick) && (homework=="done"))

{    cout<<"Go to school."; }

You might be interested in
Mr. Simmons has assigned a research project. Every student in the class will create a single page report about the recycling hab
Cloud [144]

Answer: template

Explanation:

In order for each student to have a page layout that follows a consistent style, then the students should have a template.

A template refers to a predesigned document that can be used can to create documents without having to think about formatting. When a template is used, the margin size, spacing, font style, etc are predetermined.

4 0
3 years ago
What will be the range of the random numbers generated by the following code snippet? rand() % 50 + 5;
Licemer1 [7]

Answer:

Between 5 to 54

4 0
3 years ago
The 3 parts of the CPU are
katrin2010 [14]

Central Processing Unit

Arithmitic Logic Unit

Control Unit


3 0
4 years ago
Read 2 more answers
When you make a cell phone call, which type of electromagnetic wave is most likely transmitting the signal?
Olegator [25]
They use Radio waves 
6 0
3 years ago
How does the speaker feel about traditional forms of poetry
Salsk061 [2.6K]
A speaker sometimes is not able to capture the intended details since it is affected by homophones.
7 0
4 years ago
Read 2 more answers
Other questions:
  • What would be some long- term goals.
    11·1 answer
  • You can use the results from a search on a database in all of these ways except to ____.
    11·2 answers
  • Which technology forms the foundation for cloud computing? forms the foundation for cloud computing.
    12·2 answers
  • Which two statements are true about the Data Sync functionality? (Choose two.)
    15·1 answer
  • This matches the domain name with the correct IP address:
    12·1 answer
  • Write an assembly subroutine that check if a number is in the interval of [0, 10] and return 1 if the number is in this interval
    6·1 answer
  • A recursive method that computes the number of groups of k out of n things has the precondition that ______. n is a positive num
    12·1 answer
  • What are the functions of super computer?<br> ​
    7·2 answers
  • Instructions: Write a program that calculates the amount of ingredients needed for various flavors of cheesecake.
    13·1 answer
  • An entity is said to be _____-dependent if it can exist in the database only when it is associated with another related entity o
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!