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
Excerpt from "How Prepared Are Students for College Level Reading? Applying a Lexile-Based Approach."
Verizon [17]

Answer: Students graduating from high school are not prepared for college-level work.

Explanation:

From the excerpt, it was written that there is a high rates of enrollment in remedial college courses which shows that there are many students are graduating from high school who are unprepared for college-level work.

Therefore, based on this, the best summary of the paragraph is that the students graduating from high school are not prepared for college-level work.

Therefore, the correct option is C.

7 0
3 years ago
Which Tab contains the paragraph attributes?
alexgriva [62]

Answer:

b insert

Explanation:

7 0
3 years ago
A chain of dry-cleaning outlets wants to improve its operations by using data from
kotykmax [81]
A










explanation cause it is
5 0
3 years ago
Which language do you use to add functionality to a web page
Dovator [93]
The answer is Javascript. In partner with HTML/HTML5 (Hypertext markup language : which provides the structure and CSS (Client Side Scripting : Provides the design to the structure). JS or Javascript programming language provides the function at the backend of a webpage. It responds to the input and also calls on the PHP / SQL Scripts to tap the database.
6 0
3 years ago
Help please!, explain what is missing and what needs to be changed if anything.
german

Using the computer language in JAVA to write a function code that output numbers in reverse

<h3>Writting the code in JAVA:</h3>

<em>import java.util.Scanner;</em>

<em>public class LabProgram {</em>

<em>    public static void main(String[] args) {</em>

<em>        Scanner scnr = new Scanner(System.in);</em>

<em>        int[] userList = new int[20];</em>

<em>        int numElements;</em>

<em>        numElements = scnr.nextInt();</em>

<em>        for (int i = 0; i < numElements; ++i) {</em>

<em>            userList[i] = scnr.nextInt();</em>

<em>        }</em>

<em>        for (int i = numElements - 1; i >= 0; --i) {</em>

<em>            System.out.print(userList[i] + " ");</em>

<em>        }</em>

<em>        System.out.println();</em>

<em>    }</em>

<em>}</em>

See more about JAVA at brainly.com/question/12975450

#SPJ1

8 0
2 years ago
Other questions:
  • Which of the following is not a standard method called as partof the JSP life cycle?
    5·1 answer
  • Graded Assignments may be found at the end of each chapter of the required textbook under the title "Real-World Exercises". Each
    15·1 answer
  • U2- an example of __________ is an attempt by an unauthorized user to gain access to a system by posing as an authorized user.
    6·1 answer
  • Marissa, a 21-year-old young woman, is working as an intern at a software company. She has recently graduated from college. She
    6·1 answer
  • In C++ Please :
    6·1 answer
  • Jenny is working on a laptop computer and notices that the computer is not running very fast. She looks and realizes that the la
    14·1 answer
  • Create an application named Percentages whose main() method holds two double variables. Assign values to the variables. Pass bot
    14·1 answer
  • List three types of Software:
    15·1 answer
  • You compared each letter in the correct word to the letter guessed.
    5·1 answer
  • List and describe the five moral dimensions that are involved in political, social, and ethical issues. Which do you think will
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!