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
Why would someone need to use tools like IECacheView or MyLastSearch on a system? Do companies have the right to search a workst
natali 33 [55]

IECacheView helps a user quickly and easily access and read the cache folder of Internet Explorer. Information such as filename, content type, number of hits, expiration time, and the full path of cache filename can be accessed. With all this information, investigators can look at what users are accessing on the web. Anything accessed on IE by an employee at his or her workstation can be searched by the company. This brings us to the question; do companies have the right to search a workstation they issued to their employees using IECacheView?

In my own opinion, I honestly think that it is not right to search an employee’s workstation using IECacheView or any other tool whatsoever. Under the law, employees have reasonable rights to privacy. These rights prevent employers from searching their employees. Unless there is evidence of computer crimes or misconduct, employees have a greater expectation of privacy

6 0
4 years ago
Which of the following is NOT a good way to reduce pollutants in paint shops?
Citrus2011 [14]

Automobile repair shops produce many types of waste -- some hazardous, some not necessarily hazardous but still potentially damaging to the environment if not handled properly, and all requiring proper treatment and/or disposal at significant cost to the business. A list of the types of waste that the shop owner or manager must contend with would include:

solvents (paints and paint thinners)

antifreeze

scrap metal

batteries and other auto parts

oils and oil filters

fuels of various types

acids and alkalis (contaminated rags and towels)

Whatever the nature and characteristics of the waste may be, it all has one thing in common: All waste represents loss of resources and loss of money.

4 0
3 years ago
How was WiFi discovered?
postnew [5]

Answer:

An Australian scientist by the name of John O'Sullivan was inspired by Stephen Hawking's theory of evaporating black holes and their subsequent radio waves. He set out to find them and prove the theory correct. (Wi-Fi was discovered on accident)

7 0
3 years ago
Extend the class linkedListType by adding the following operations: Write a function that returns the info of the kth element of
WINSTONCH [101]

Answer:

Explanation:

The following code is written in Java. Both functions traverse the linkedlist, until it reaches the desired index and either returns that value or deletes it. If no value is found the function terminates.

public int GetNth(int index)

       {

       Node current = head;

       int count = 0;

       while (current != null)

       {

       if (count == index)

       return current.data;

       count++;

       current = current.next;

       }

       assert (false);

       return 0;

       }

public int removeNth(int index)

       {

       Node current = head;

       int count = 0;

       while (current != null)

       {

       if (count == index)

       return current.remove;

       count++;

       current = current.next;

       }

       assert (false);

       return 0;

       }

6 0
3 years ago
Over the years, workplace discrimination has become________.
natta225 [31]

Over the years, workplace discrimination has become more subtle.

5 0
3 years ago
Other questions:
  • What activities are the most likely to infect your computer with a virus? Check all that apply
    11·2 answers
  • You have been asked to implement enterprise software for a manufacturer of kitchen appliances. What is the first step you should
    11·1 answer
  • Question 19 :Rachel, a database administrator, has created a database for her website. It contains pictures of vacations that pe
    13·1 answer
  • Bao bì chủ động active packaging và bao bì thông minh intelligent packaging khác biệt như thế nào
    15·1 answer
  • Which of the following BEST describes Computer Science (CS)?
    8·1 answer
  • Discuss how the Internet has made our communication hypertextual, omnidirectional, and more interactive
    6·1 answer
  • 29. Write a program that asks to input any ten numbers and displays sum of them​
    11·1 answer
  • 4.8 code practice question 1 edhesive
    8·2 answers
  • Which of the following is NOT a media file? *<br> .wav<br> .mp4<br> .exe
    10·1 answer
  • question 1 scenario 1, question 1-5 you’ve just started a new job as a data analyst. you’re working for a midsized pharmacy chai
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!