From the course: Complete Guide to C Programming Foundations
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Breaking out of a loop - C Tutorial
From the course: Complete Guide to C Programming Foundations
Breaking out of a loop
- [Instructor] A loop repeats its statements until the terminating condition is met or until a break statement is encountered. In this code, input from the user is requested. The loop repeats based on the value input or until the value of variable A reaches nine, run. I'm going to try a huge value like 300, but you see the loop only repeats 10 times. The IF statement at line 12 tests to see when the value of variable A is equal to nine. If so, its sole statement break is executed, the loop is broken, and execution continues outside the loops block. When used in a nested loop break halts only the loop it sits in here, you see nested for loops. At line 13 an if test is made, if variable column is equal to the letter E, the inner loop is broken run, and the table stops output at all rows after letter E, only the inner loop is affected. You can use break to avoid entangling your code in an endless loop. Here, the while loops condition is one, true. The loop has no exit condition except…
Contents
-
-
-
-
-
-
Making a decision3m 25s
-
(Locked)
Exploring the possibilities2m 53s
-
(Locked)
Using the ternary operator3m
-
Working with the switch-case structure4m
-
(Locked)
Challenge: Select an item28s
-
(Locked)
Solution: Select an item1m 45s
-
Creating a for loop3m 36s
-
(Locked)
Setting up a while loop2m 58s
-
(Locked)
Challenge: Repeat some text51s
-
(Locked)
Solution: Repeat some text1m 51s
-
(Locked)
Nesting loops1m 59s
-
(Locked)
Breaking out of a loop3m 16s
-
(Locked)
Avoiding the goto keyword1m 33s
-
(Locked)
Chapter challenge: Interpreting commands2m 26s
-
(Locked)
Chapter solution: Interpreting commands3m 55s
-
-
-
-
-
-
-
-