From the course: Practice It: C Decisions and Loops
Unlock the full course today
Join today to access over 24,000 courses taught by industry experts.
Check it: Reviewing high scores - C Tutorial
From the course: Practice It: C Decisions and Loops
Check it: Reviewing high scores
- [Instructor] Welcome back. I hope you managed to beat your own high score and without cheating. Converting a while loop into a for loop and vice versa is rather easy, providing that the code runs and that you can identify all the parts. So here are the parts. Line 11, initialization, line 12, condition, line 15, iteration action. These lines are edited down into a single for loop statement. for x = 0. The semicolon is already in place. I can join the next line. And there's the looping condition, semicolon, and then add line 14, x++, the trigger, remove it from here. There's the loop. The braces are unnecessary, but I'll keep them anyway. And there's the output. Within the code, I follow a more traditional C format when I declare all the variables up top in a function. You can, however, declare a variable anytime before it's used. Often in a for loop, you see other programmers do this. And of course I can…
Contents
-
-
-
-
Repeating a chunk of code1m 56s
-
(Locked)
Try it: Calculating a harmonic series2m 49s
-
(Locked)
Check it: Calculating a harmonic series4m 24s
-
(Locked)
Using a for loop1m 40s
-
(Locked)
Try it: Reviewing high scores1m 30s
-
(Locked)
Check it: Reviewing high scores3m 43s
-
(Locked)
Try it: Counting the Mondays1m 58s
-
(Locked)
Check it: Counting the Mondays2m 30s
-
(Locked)
Try it: Generating a table1m 48s
-
(Locked)
Check it: Generating a table4m 15s
-
(Locked)
Check it: Crafting a bubble sort1m 39s
-
(Locked)
Try it: Counting in two directions1m 48s
-
(Locked)
Check it: Counting in two directions1m 58s
-
(Locked)
Discovering the do-while loop1m 19s
-
(Locked)
Try it: Processing text input2m 29s
-
(Locked)
Check it: Processing text input2m 31s
-
-