Question: When using loops in a program it is important to have 1 ) A condition that can actually be reached, and 2 ) If in

When using loops in a program it is important to have 1) A condition that can actually be reached, and 2) If in a for loop, incrementation.
If you have a while loop that exits the loop when active = false, somewhere in the loop there must be a conditional statement that will make active false, otherwise you will be stuck in an infinite loop and crash your program. In a for loop if your loop does not iterate properly you will also create an infinite loop for (int i =0; i >=0; i++) will never exit since i is always going to be greater than or equal to 0, the proper condition to check for would be i >=0 and decrement to -1.
Two loops that you will see in many programs are the while loop and the for loop. A while loop can be used to loop through code while a condition is true. If you are writing a game, you would use a while loop to update every input or frame, depending on what kind of game you are making. A for loop increments through a quantity. So if you wanted to check for spaces in a string you could create a for loop that increments through every character in the string that looks for character a space character "" and run some code when that condition is met.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!