Question

In: Computer Science

• Describe one situation with real-world examples in which a programmer might want to create a...

• Describe one situation with real-world examples in which a programmer might want to create a loop that tests its condition in the beginning of the loop and one situation in which the condition is tested at the end of the loop.
• There are many situations where infinite loops may occur. Discuss those situations and provide best practices for each of the loop types that help avoid writing infinite loops.

Solutions

Expert Solution

//Loop
1.A program which writes into a text file till its size reaches say 2KB.in this case we cannot use do while or for loop because we don't know number of iterations.here we are checking condition at the end
Consider we want to take an integer input from user until user have entered a positive number. In this case we will use a do-while loop like this.Here we check the condition first.
2.
Infinite loops are loops that repeat forever without stopping.
Situations where a wrong variable is incremented,this infinite loops comes in:
Example:   
   int i, j=0;   
for( i = 0; i < 5; j++ )
printf( "i = %d\n", i );   
Other times infinite loops serve a useful purpose, such as this alternate means of checking user input:
while( true ) {
printf( "Please enter a month from 1 to 12 > " );
scanf( "%d", &month );
if( month > 0 && month < 13 )
break;
printf( "I'm sorry, but %d is not a valid month.\nPlease try again.\n", month );
}
      
Best practises using loops:
1.Avoid nesting of loops.Create/use methods functions in the loop
2.If your are aware of no of times loop runs,use for loop.if not sure of it go fr while loops.USe break and continue in the loops.


Related Solutions

In looking at Real World Fiscal Policy, one might want to consider the timing, or lag,...
In looking at Real World Fiscal Policy, one might want to consider the timing, or lag, or speed at which a policy can go into effect. How do the Tax cuts of 2001,2003, and the 2020 policies stack up when looking at timing issues?
Give an example of a real-world situation where we might need to know the mean AND...
Give an example of a real-world situation where we might need to know the mean AND standard deviation of a distribution. In other words, under what circumstances might we be concerned with... 1) the center of the distribution AND 2) the spread of the distribution (i.e., how far away data points are from the mean) HINT: When might looking exclusively at the mean be misleading or uninformative?
Describe a real-world business situation that could be addressed by collecting and analyzing a set of...
Describe a real-world business situation that could be addressed by collecting and analyzing a set of data using the t-test. What open source would you use to collect that data?
Describe a real world situation where the properties of a solution are different from those of...
Describe a real world situation where the properties of a solution are different from those of its solvent and where this is beneficial.
Give two examples of common applications of confidence intervals you might encounter in the “real world.”...
Give two examples of common applications of confidence intervals you might encounter in the “real world.” I need easy and clear examples
what are some real world examples when a person might use an independent samples t-test and...
what are some real world examples when a person might use an independent samples t-test and a dependent samples t-test?
What impediments in the real world might prevent the Law of One Price from holding for...
What impediments in the real world might prevent the Law of One Price from holding for a particular good? How is the Law of One Price related to the idea of Purchasing Power Parity (PPP)? What is the evidence for PPP actually existing?
Describe the difference it might make in one's life if one (a) sees the world as...
Describe the difference it might make in one's life if one (a) sees the world as basically hostile, cold, and unreceptive toward one's basic needs, or (b) sees the world as basically welcoming and interested in who one is as a person.
Evaluate real-world instances of externalities and public good inefficiencies and suggest solutions. Describe a situation where...
Evaluate real-world instances of externalities and public good inefficiencies and suggest solutions. Describe a situation where you experienced a positive or negative externality or free-riding by you or someone else. Why are these situations problematic?
Describe a situation in which it might be better to use the sample median rather than...
Describe a situation in which it might be better to use the sample median rather than the sample mean to predict the population mean of a random variable Y , µY .
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT