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 - C Tutorial
From the course: C Essential Training
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
Making a decision3m 37s
-
(Locked)
Exploring the possibilities2m 34s
-
(Locked)
Using the ternary operator3m 1s
-
Working with the switch-case structure4m 9s
-
(Locked)
Challenge: Select an item29s
-
(Locked)
Solution: Select an item57s
-
(Locked)
Creating a for loop3m 58s
-
(Locked)
Setting up a while loop3m 6s
-
(Locked)
Challenge: Repeat some text48s
-
(Locked)
Solution: Repeat some text1m 40s
-
(Locked)
Nesting loops1m 55s
-
(Locked)
Breaking out of a loop3m 28s
-
(Locked)
Avoiding the goto keyword1m 56s
-
(Locked)
Challenge: Work the grid2m 21s
-
(Locked)
Solution: Work the grid2m 8s
-
-
-