Describe a strategy for avoiding nested conditionals. Give your own example of a nested conditional that can be modified to become a single conditional, and show the equivalent single conditional.

Answers

Answer 1
Answer:

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.


Related Questions

A LAN Uses category 6 cabling. An issue with the connection results in network link to get degeneration and Only one device can communicate at a time. What is the connection operating at?Half duplexSimplexPartialFull Duplex
A majority of the people whose information was compromised are homeless; therefore there is no easy way to contact them in order to alert them of the security breach. How should Data-Time Inc. manage this breach in security
The computer output for integer programs in the textbook does not include reduced costs, dual values, or sensitivity ranges because these variables are not meaningful for integer programs. TrueFalse
What are the advantages and disadvantages of using the serial console connection compared to the usb console connection to a cisco router or switch?
Edhisive 3.5 code practice

Does Kennesaw State University have any computing ethical policy for computer usage? If so - what is it?

Answers

Answer:

The answer to the questions: Does Kennesaw State University have any computing ethical policy for computer usage? If so - what is it? Would be as follows:

1. Yes, Kennesaw State University, a university in Georgia, does have a computing ethical policy that regulates the proper use of the facilities and computing services within the facilities of the university and the use of computing equipment that belongs to the university. This policy is known as the KSU Computer Usage Policy.

2. As said before this policy establishes that the use of computing services are not the right of a person, but rather a privilege afforded to the students, faculty and other people who are present in the university and who may need to use its computing services. The use of the computing services would be whitin this policy as long as it stays inside the delimitations established by federal, state and University policies.

Explanation:

In computer programming, what is syntax?

Answers

Answer: syntax is a set of rules for grammar and spelling. In other words, it means using character structures that a computer can interpret

Answer:

in computer science, the syntax of a computer language is the set of rules that defines the combinations of symbols that are considered to be correctly structured statements or expressions on that language.

What character makes an assignment statement an assignment statement?

Answers

Answer:

'='

Explanation:

The equal ('=') is the character that is used to assign the value in the programming.

In the programming, there is a lot of character which has different meaning and uses for a different purpose.

like '==' it is used for checking equality between the Boolean.

'+' is a character that is used for adding.

'-'  is a character that is used for subtraction.

similarly, '=' used for assigning.

for example:

a = a + b;

In the programming, the program evaluates the (a + b) first and then the result assigns to the variable.

Your friend sees an error message during Windows startup about a corrupted bootmgr file. He has another computer with a matching configuration and decides to copy the bootmgr file from the working computer to the computer with the problem. However, your friend is having problems finding the bootmgr file and asks for your help. What is your best response?

Answers

Answer:

He should remove any removable devices from the PC , change the boot device to USB or CD drive and insert the installation disk or a created ISO USB file to install

Explanation:

You are troubleshooting network connectivity issues on a workstation. Which command would you use to request new IP configuration information from a DHCP server?

Answers

Answer:

ipconfig / release command will be used to request new IP configuration information from a DHCP server

Explanation:

The ipconfig /release sends a DHCP release notification to the client so that the client immediately releases the lease henceforth updating the server's status information . This command also mark the old client's id as being available. Thus, the command ipconfig /renew then request a new IP address.

What type of object can offer the mosteffective way to display data and calculations in a presentation?

Answers

I think the answer would be a chart. It is important for researchers to use the most effective chart to display their data results.

A graph or a table.

You can use a graph or a table to make the point muchstronger than just describing the data. A graph can be used to represent one ofmore sets of data graphically. A table in this case as compared to a graph isless effective because it only shows the data whereas the graph shows an interpretationof data. However, you can use a table from an excel sheer to enter raw data and make some complex andsimple calculations.