From the course: Complete Guide to C Programming Foundations
Creating a for loop - C Tutorial
From the course: Complete Guide to C Programming Foundations
Creating a for loop
- [Instructor] Another way to control program flow is one of the basic concepts of programming, the loop. A loop is one or more statements that repeat. The loop's condition determines how many times the statements repeat. Three C language keywords deal with loops. For, while, and do. Loops feature three parts, a starting condition or initialization, an expression that must remain true for the statements to repeat, and an exit condition to terminate the loop. In a for loop, all three conditions are expressed in the keywords parentheses shown in this exercise file. Two semicolons, separate the three expressions in a for statement. The starting condition or loop initialization appears first, variable a is initialized to zero. Next comes the condition. When true, the loop repeats. Here, the loop repeats as long as the value of variable a is less than 10. Finally, comes an expression that's executed each time the loop repeats. Variable a is incremented. This expression eventually triggers the loop's termination. This construction is complex with lots of symbols and whatnot still in a for loop. All of the loop's parts appear in one spot. Remember that the separators are semicolons, not colons or commas. Run to see the output. And the loop repeats 10 times. In this code, user inputs sets the number of times the loop repeats. The input value in variable b, sets the loop's termination shown in the middle of the for statement. Loop as long as a is less than b. Because only one statement is repeated, it need not be enclosed in braces. Run, and type a value. I'll do 15. And there's the countdown. Run again. And now I'm going to try a negative number just because it tells me not to. And the loop doesn't repeat. Why do you think the loop doesn't repeat? In the code, negative five is less than zero. The condition here is already true. The loop doesn't repeat and it never executes its statement. Not even once. You can specify multiple expressions in a for statement as shown in this exercise file. Two variables are initialized as the first part of the for statement. Each separated by a comma. The loop repeats as long as the value of variable a is less than 10 but as the loop repeats, the value of variable a is incremented, comma, and then the value of variable b is decremented. And you see the two values output here. The only required part of a for statement are the semicolons. Here, I've pulled out the other parts. What this construction does is to create an endless or infinite loop. And it goes on and on and on until you press control C to stop it. The endless loop can be done on purpose, and it happens often, but sometimes it happens accidentally because you messed up some logic or the termination condition never occurs. Be on the lookout for endless loops.
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
-
-
-
-
-
-
-
-