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
Ksenya-84 [330]
3 years ago
11

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:
Alex787 [66]3 years ago
4 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 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<<"Eligible for promotion."; }

The second strategy is to use an interface. For example you can

abstract class Shape{

//declare a method common to all sub classes

  abstract public int area();

// same method that varies by formula of area for different shapes

}

class Triangle extends Shape{

  public int area() {

     // version of area code for Triangle

return (width * height / 2);

  }

}

class Rectangle extends Shape{

  public int area() {

     // version of area code for Rectangle

    return (width * height)

  }

}

// Now simply create Rectangle or Triangle objects and call area() for any such object and the relevant version will be executed.

You might be interested in
You are building a gaming computer and you want to install a dedicated graphics card that has a fast gpu and 1gb of memory onboa
irga5000 [103]
Usually a graphics card would be plugged in manually through a PCI-e express slot on the motherboard. But to give the card enough power (based on how powerful the card is) you would use a 6 pin or an 8 pin power connector.
<span />
5 0
3 years ago
Read 2 more answers
PlanthelogicforLungi’sapplicationusingpseudocode.Thelogicneedstosatisfythefollowingneeds:TheapplicationwillneedtoallowLungitoen
anastassius [24]
According to my
calculations is 3
6 0
1 year ago
Which is the process that a wireless router uses to translate a private ip address on internal traffic to a routable address for
luda_lava [24]
<span>The process that a wireless router uses to translate a private ip address on internal traffic to a routable address for the internet is </span>Network address translation<span> (NAT).  It is a method of relabeling one </span>IP address<span> space into another by altering network </span>address<span> information in IP header of packets while they are in transit across a </span>traffic routing<span> device. </span>
7 0
3 years ago
DIRECTIONS: Organize your desktop. Name the 5 folders based on the files given below. Organize your own desktop by sorting the g
dem82 [27]

Answer: Music, Documents, PowerPoints, Pictures/Videos, Audios

6 0
3 years ago
How does the dns help the world wide web scale so that billions of users can access billions of web pages?.
valentina_108 [34]

Answer:

domain name systems allow web users to choose where they want to go and to have many different sites

8 0
2 years ago
Other questions:
  • Which of the following is a malicious program that can replicate and spread from computer to computer?
    13·2 answers
  • How would you display all your photographic work in your résumé, if you have a large volume of work?
    10·1 answer
  • You're the network administrator for a company that has just expanded from one floor to two floors of a large building, and the
    7·1 answer
  • For an machine using 2-dimensional even parity for error detection/correction, and the following received bytes, where is the er
    9·1 answer
  • Suppose that a computer can read or write a memory word in 5 nsec. Also suppose that when an interrupt occurs, all 32 CPU regist
    10·2 answers
  • Create a program that asks the user to enter grade scores. Use a loop to request each score and add it to a total. Continue acce
    9·1 answer
  • A(n) ____ string contacts the data source and establishes a connection with the database using the Data Source Configuration Wiz
    5·1 answer
  • What icon is usually used to indicate an attachment feature?
    14·2 answers
  • Anyone want to play mine mincraft w/ me?
    9·1 answer
  • What would be the effect if the register contained the following values 10011000
    12·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!