answer.
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]
1 year 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]1 year 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
Write a print statement that displays a random integer between 5 and 5000. Assume the random library is imported.
Contact [7]

Answer:

Explanation:

The following code is written in Java. It is a very simple three line statement (assuming that the random library was imported) that chooses a random integer value between 5 and 5000 and prints it to the screen using the println statement.

Random rand = new Random();

int randomNum = rand.nextInt((5000 - 5) + 1) + 5;

System.out.println(randomNum);

The random number generator is initialized and given a value between 5 and 5000. Since random number Generator will generate a number between 0 and the given value then subtracting 5 from the initial generated number makes sure that it is not more than 5000 and then adding 1 and 5 after wards makes sure that it is more than 5 always.

4 0
1 year ago
How efficient would a 6-bit code be in asynchronous transmission if it had 1 parity bit, 1 start bit, and 2 stop bits?
Lana71 [14]
How efficient well if we are transmitting 6bits and we need 10 to transmit the 6 bits that would be 6/10 = .6 = 60% efficient.
7 0
1 year ago
1.2.2: Output variable value. Jump to level 1 Write a statement that outputs variable userAge. End with a newline. 1 2 3 4 5 6 7
serg [7]

Answer:

cout<<"User Age is ="<<userAge<<endl;

Explanation:

#include<iostream>

using namespace std;

int main()

{

int userAge;

cin >> userAge; // Program will be tested with values: 15, 40.

cout<<"User Age is ="<<userAge<<endl;

return 0;

}

5 0
2 years ago
Lin is booting up his computer, and during the boot process, the computer powers down. After several unsuccessful attempts to bo
inysia [295]

Answer:

Option (c) is the correct answer of this question.

Explanation:

RAM (Random Access Memory) It is a type of computer memory which can also be retrieved and adjusted in either sequence, usually still had to preserve operating memory and bytecode.

It is a type storage device, and maintains the data used when the device is operating.RAM makes file access more frequently compared with certain digital storage types.

 <u>For Example</u>:- PCs, tablets, smartphones ,printers etc.

Other options  are not related to the given scenario.

8 0
2 years ago
A good way to avoid skids is to _____ when coming into a turn. A. accelerate B. decelerate C. counter steer D. cover brake
lord [1]

Answer:

B decelerate

Explanation:

Deceleration will help in avoiding you from loosing control of the vehicle

3 0
1 year ago
Read 2 more answers
Other questions:
  • A U.S. social security number consists of a string of 9 digits, such as "444422333". Assume that input consists of a sequence of
    6·2 answers
  • Convert the following binary number to octal (111000)2​
    12·1 answer
  • An organization has a datacenter manned 24 hours a day that processes highly sensitive information. The datacenter includes emai
    5·1 answer
  • Write a client program that writes a struct with a privateFIFO name (call it FIFO_XXXX, where XXXX is the pid that you got from
    11·1 answer
  • Consider the relation Courses(C, T, H, R, S, G), whose attributes can be thought of informally as course (C), teacher (T), hour
    10·1 answer
  • Which of the following should get a Page Quality (PQ) rating of Low or Lowest? Select all that apply. True False A page with a m
    8·1 answer
  • SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA. Assume that the classes listed in the Java Quick R
    9·1 answer
  • Complete this truth Table. Write a program that you can enter from the keyboard, a 1 or 0 into three Boolean variables, A,B,C. W
    14·1 answer
  • Which statement is true about the purpose of a work in process constraint?
    15·1 answer
  • For a data structure, such as a stack, who is responsible for throwing an exception if the stack is empty and a pop() is called:
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!