Faculty Supervisor: Al Day
Some material derived from lessons authored by Prof. Martha Selby
HTML Documentatiion by Larry Genalo Jr.
Date Last Updated: 8/7/95
![]()
Nested loops can be extremely useful, but also confusing to deal with.
Remember, for each iteration of the outer loop, the inner loop is executed and repeated until done.
Two for loops must use different control variables!
Results
for (m=1; m<=4; m++) { (m) (n) for (n="1;" n<="3;" n++) { 1 1 printf("%d %d\n", m, n); 1 2 } 1 3 } 2 1 2 2 2 3 3 1 Note: Inside loop is 3 2 executed 3 times for 3 3 each of the 4 times the 4 1 outside loop is executed! 4 2 4 3
Many times a flag can help to simplify the condition.
A flag is type int variable used to represent whether or not a certain event has occured. It is often initialized to 0 where it remains until the event occurs. When the event occurs, the flag is changed to 1.