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
Which term describes the order of arrangement of files and folders on a computer?
jonny [76]
The term that best describes this would be called computer files and or organization file
6 0
3 years ago
Read 2 more answers
What is ambient sound, and what is its purpose in film? When can it be a drawback ?
Helen [10]

Answer:

Ambient sound is an environmental sound or surrounding sounds. They use this kind of sound to give a film's scene credence(belief in or acceptance of something as true). When the sound starts to become distracting to the audience the drawback is used.

Explanation:

6 0
3 years ago
Write a script called fact.sh that is located in your workspace directory to calculate the factorial of a number n; where n is a
ira [324]

Answer:

hope this helps

Explanation:

7 0
3 years ago
. Write a toString method for this class. The method should return a string containing the base, height and area of the triangle
marta [7]

Answer:

Check the explanation

Explanation:

Circle.java

public class Circle {

private double radius;

public Circle(double r) {

radius = r;

}

public double getArea() {

return Math.PI * radius * radius;

}

public double getRadius() {

return radius;

}

public String toString() {

String str;

str = "Radius: " + radius + "Area: " + getArea();

return str;

}

public boolean equals(Circle c) {

boolean status;

if (c.getRadius() == radius)

status = true;

else

status = false;

return status;

}

public boolean greaterThan(Circle c) {

boolean status;

if (c.getArea() > getArea())

status = true;

else

status = false;

return status;

}

}

I have created a class also to test this class below is the class: -

MainCircle.java

public class MainCircle {

public static void main(String args[]) {

Circle c = new Circle(2.5);

Circle c1 = new Circle(2.5);

Circle c2 = new Circle(4.5);

System.out.println(c);

System.out.println(c1);

System.out.println(c2);

System.out.println("Is c and c1 equal : "+c.equals(c1));

System.out.println("Is c2 greater than c1 : "+c1.greaterThan(c2));

}

}

Output: -

Radius: 2.5Area: 19.634954084936208

Radius: 2.5Area: 19.634954084936208

Radius: 4.5Area: 63.61725123519331

Is c and c1 equal : true

Is c2 greater than c1 : true

7 0
3 years ago
Difference between first and second generation of computers. <br>​
Harman [31]

\mathcal{\fcolorbox{black}{orange}{\pink{Question:}}}

Difference between first and second generation of computers.

\mathcal{\fcolorbox{black}{orange}{\red{Answer:}}}

Refer to attachment.!

<h3>Hope It's helped mate! :)</h3>

7 0
3 years ago
Other questions:
  • Hands-on Assignment To these assignments, refer to the tables in the JustLee Books database. Generate and test two SQL queries f
    10·1 answer
  • 6. When should you return to the right lane after passing another car on the left?
    14·2 answers
  • Which type of peripheral is a keyboard?
    11·2 answers
  • Why did the Wright brothers construct their own wind tunnel?
    11·1 answer
  • Is there anyone who is learning coding if so then can you tell me how to be perfect in coding​
    14·1 answer
  • Which of the following is the formula used to calculate the risk that remains after you apply controls? a. ALE=SLExARO b. Total
    9·1 answer
  • We cannot imagine a life without the Internet. Imagine that you had to live without being connected to the Internet. Discuss the
    9·1 answer
  • Made from fruit juice, water and sugar, The American version contains milk and cream and sometimes egg white​
    9·1 answer
  • The distance between two walls is called what?
    8·2 answers
  • What does RoHS stand for and why is RoHS compliance important?
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!