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
There's a rising issue with bots sending links starting with 'bit'. These links are dangerous and harmful for any computer or de
-Dominant- [34]
Hi there I just posted a question and this pop up

5 0
3 years ago
Read 2 more answers
What do economists call a decline in the real GDP combined with a temporary rise in price level?
ad-work [718]
A <span>stagflation is my answer.</span>
3 0
3 years ago
Read 2 more answers
Two electronics technicians are measuring electrical quantities in circuits. Technician A says that copper, glass, porcelain, an
OleMash [197]

The correct answer is A. Only technician B is correct because I don't believe carbon is form of resistors.


8 0
3 years ago
Read 2 more answers
Scenario: 1. Victim opens the attacker's web site. 2. Attacker sets up a web site which contains interesting and attractive cont
Ann [662]

Answer :

These sort of attacks are known are Injected IFrame Attacks. The Injected IFrame Attacks are a common Cross Site Scripting or XCC attack that contains multiple or single iFrame tags implemented within a page which is not visible to user. User unintentionally enters his information which is posted to hacker’s server and information’s gets hacked.

5 0
3 years ago
This organization created web standards to be followed in the creation and contentof a web page​
kumpel [21]

Answer:

The World Wide Web Consortium (W3C) is an international community where member organizations, a full-time staff, invited experts and the public work together to develop Web Standards. Led by Web inventor and Director Tim Berners-Lee and CEO Jeffrey Jaffe, W3C's mission is to lead the Web to its full potential.

<em />

<em>Hope you got it</em>

4 0
3 years ago
Other questions:
  • What is the primary purpose for a screen saver in windows?
    8·1 answer
  • Which of these is a social consequence of effective communication
    12·1 answer
  • Domain of (x+8)/(x(x+10))
    12·1 answer
  • How do you change your age on here? I accidentally put that i was 15 but i am only 13. How do I change this?
    12·1 answer
  • In this activity, you'll decide how a computer (in this case, a smartphone)
    12·1 answer
  • A word object in an excel worksheet to activate the word features
    9·1 answer
  • write a program that asks the user to enter a positive integer, then prints a list of all positive integers that divide that num
    6·1 answer
  • When an external device becomes ready to be serviced by the processor the device sends a(n)_________ signal to the processor.
    8·1 answer
  • As marketing manager, you need to have ( blank) skills and (blank) skills.
    11·1 answer
  • Chris needs to modify the default bullets that are used in a nonnumbered list in Word.
    12·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!