From the course: C Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Breaking out of a loop

Breaking out of a loop - C Tutorial

From the course: C Essential Training

Start my 1-month free trial

Breaking out of a loop

- [Instructor] A loop repeats it statements until the terminating condition is met or until a break statement is encountered. In this code, input from the user is requested. The value input is used to determine how many times the loop spins. The if test at line 12 compares the value input with nine, once it reaches this value, no matter what was input, the loop is broken. Execution continues after the loop's block of statements. Build and run the code. And I'm going to type a huge value, 300. But the loop repeats only 10 times. The if statement at line 12 test to see whether the value of variable a is equal to nine. If so, its sole statement break is executed. And it counts 10 times because zero through nine is equal to 10. When used in a nested loop, break halts only the loop it's in. Here you see nested loops, an outer loop and an inner loop. At line 13, a test is made to determine whether the value of variable…

Contents