Question

In: Computer Science

how to ensure the while loop will end correctly

how to ensure the while loop will end correctly

Solutions

Expert Solution

To answer this we will look into how while loop works.

-While-Loop is also called as Condition-Controlled (i.e they conitnue to loop until some condition is met).
- It is used normally when we want to repeat a secition of code for unspecified number of times until the condition is met.
- One interesting thing is that while loop checks for the condition first (i.e stopping condition) and if the condition is initially false it may not even exceute the body at all.

Syntax:
while(condition)
{
   body;
}

Now how to ensure the while loop will end correclty.

-So to ensure that it ends correctly we have to make sure that it meets the stopping condition. If it doesn't met the stopping condition that it will result into infinite loop which we don't want.

- So to meet the stopping condition we have to do either addition , subtraction , mulitplication or divison for the variable used in the condition until the stopping condition is met. (i.e condition becomes false)

For example let's take an example and understand it further.

int j = 0;
// Here checking the while-loop condition before entering the loop
while( j < 5 )
//if it's true we will enter
{
   printf( "j = %d\n",j++);
   // Now how to meet the stopping condition for that we have increment the value of j
  
}
printf( "After loop, j = %d\n", j );

#Note If need any help then ping me in the comments I will be glad to help you out ^_^ and If you liked the answer and explanation then please give a thumbs up I really need a +ve feedback at the moment.


Related Solutions

Open Average Test Scores while loop, comment out the while loop and add a for loop...
Open Average Test Scores while loop, comment out the while loop and add a for loop that averages 4 test scores. Code C# While loop code using System; class Program { static void Main() { int count = 0, total = 0, number;    while (count < 3) { Console.Write("Enter a number: "); number = Convert.ToInt32(Console.ReadLine()); total += number; count++; }    double average = total / 3.0; Console.Write("Average = " + average.ToString("####0.00")); } }
why is it important to ensure that a patient knows how to perform exercises correctly
why is it important to ensure that a patient knows how to perform exercises correctly
Modify the previous program to use the Do-While Loop instead of the While Loop. This version...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version of the program will ask the user if they wish to enter another name and accept a Y or N answer. Remove the "exit" requirement from before. Output: Enter the full name of a person that can serve as a reference: [user types: Bob Smith] Bob Smith is reference #1 Would you like to enter another name (Y or N)? [user types: y] Enter...
How many times will the following while loop iterate? int i = 1; while (i <...
How many times will the following while loop iterate? int i = 1; while (i < 5) {     i = i + 1;     System.out.println(“Hello!”); } Group of answer choices 4 0 5 It will iterate infinitely
Study the following code with a while-loop and convert it to a for-loop (fill in the...
Study the following code with a while-loop and convert it to a for-loop (fill in the blanks). int i=4, result=1; while(i>1) { result *= i; i--; } The following for-loop performs the same functionality: int result=1; for (__________ i=4; i _________1;____________) { result *= i; }
What is the difference between a for loop and a while loop? When is it ideal...
What is the difference between a for loop and a while loop? When is it ideal to use a for loop, and when is it ideal to use a while loop? Can they be used interchangeably? Does it depend on the circumstance? Furthermore, what is your professional opinion on the advisability of the absence of the do while loop in the MATLAB programming language? If you use outside resources or ideas that are not your own to help make your...
C language and it has to be a while loop or a for loop. Use simple...
C language and it has to be a while loop or a for loop. Use simple short comments to walk through your code. Use indentations to make your code visibly clear and easy to follow. Make the output display of your program visually appealing. There is 10 points deduction for not following proper submission structure. An integer n is divisible by 9 if the sum of its digits is divisible by 9. Develop a program that: Would read an input...
Sentinel While Loop Lab Convert Lab 11 from a counter controlled WHILE loop to a sentinel...
Sentinel While Loop Lab Convert Lab 11 from a counter controlled WHILE loop to a sentinel WHILE loop. Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User is done entering grades ELSE Count each grade as it is entered. Compute a running total of the grades entered. END IF After the user enters the sentinel of -1, calculate the average of...
C programming. Explain by taking a programming example how do while loop is different from while...
C programming. Explain by taking a programming example how do while loop is different from while loop?
Re-write following while loop into Java statements that use a Do-while loop. Your final code should...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same output as the original code below. int total = 0; while(total<100) { System.out.println("you can still buy for"+(100-total)+"Dollars"); total=total+5; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT