- One thing you can do with a 'for loop'
that's a lot easier than using a 'while loop'
is the concept of the 'nested loop'.
In this movie I'll show you how to create a 'nested loop',
and how to avoid endless loops in common looping mistakes.
This code sends 10 random numbers to output,
built and run to see what it does.
You see a neat row of ten random values.
The spacing is produced by the tab character,
shown on line 14 as backslash T is the tab character.
To make a grid of 100 random values,
you need to add nine more rows.
The way C programmers accomplish this task is
to create a nested loop or a second 'for loop'
that handles repeating the current output 10 times.
Or nine times actually plus the current one.
Open the exercise Nested For One.
This code is a modification to the previous excercise.
It includes, an extra loop, the old loop, the common loop,
is now nested inside the row loop.
The indentation keeps the code readable,
but also helps match up the statements in curly brackets.
Build and run this code, and you see 100 random numbers.
Open exercise file Nested For Two.
This nested loop might be a bit easier for you to read.
The outside alpha loop, goes from
the letters A through J, K is the limit.
The inside loop, which doesn't need curly
brackets because it holds only
one statement, loops from zero to nine.
The current loop variable values are that
output in line 11 build and run the code.
In the output you can better see how the nested loops run.
First comes loop A, the outer loop,
and the inner loop displays values zero through nine.
Then comes loop B zero through nine,
C zero through nine and so on.
Open exercise file For Three.
This is only half of the code required for this exercise.
Input is prompted and then the alphabet is display.
What I would like you to think about doing is
how would you like to have the loop stop,
when the letter input is equal to variable T in the loop?
You don't need to change the 'for statement'
to make that happen, instead you need to put
statements within the loop that will
bust out of the loop when the character is input.
The key is to use an 'if' test
and follow that with a break statement.
Here's the solution: above line 13
add 'if' T is equals, two equal signs
variable ch break, if you don't put
the two equal signs in there
what you get is an assignment and in C
assignments always evaluate true,
and don't forget the semi-colon after the break statement.
Save, build and run.
And the alphabet only displays to letter M.
I'm going to run it a few more times so you can see what it does.
Loop X, and then we'll do sometime early like D.
One more thing to check two items in a 'for statement',
the looping condition and the iteration.
If either is off, the looping will go on forever.
Compiler will check for flaws and logic, that's your job.
My tip is to read the 'for loop' statement.
Set T equal to character A, while T is less than
or equal to character Z increment T.
If you changed the condition just T equals Z for example,
if would evaluate true and the loop would run forever
so just watch your conditions and your iterations.