This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
notes:cprog:spring2024:projects:cnv0 [2024/03/05 00:49] – [while() loops] cgrant9 | notes:cprog:spring2024:projects:cnv0 [2024/03/06 20:54] (current) – [do-while loops] amelvil2 | ||
---|---|---|---|
Line 33: | Line 33: | ||
whatever you want to happen; | whatever you want to happen; | ||
} </ | } </ | ||
+ | |||
+ | An example of a for loop that shows what iteration the loop is on, i.e Iteration: 0, Iteration: 1, etc. until it reaches Iteration: 9 and ends the loop: | ||
+ | < | ||
+ | for (i = 0; i < 10; i++) { | ||
+ | printf(" | ||
+ | }</ | ||
====while() loops==== | ====while() loops==== | ||
A while loop is a loop that continues to iterate as long as the input evaluates to true. | A while loop is a loop that continues to iterate as long as the input evaluates to true. | ||
Line 54: | Line 60: | ||
====do-while loops==== | ====do-while loops==== | ||
+ | A do-while loop issues commands then checks the condition rather than checking the condition and then issuing the commands. | ||
+ | |||
+ | < | ||
+ | int i = 5; | ||
+ | do | ||
+ | { | ||
+ | i--; | ||
+ | } while ( i > 0 ) | ||
+ | </ | ||
+ | |||
+ | An example of a do-while loop being used. This loop will check if the user has inputted the correct password. The loop will continue until number equals 1234: | ||
+ | |||
+ | < | ||
+ | int number; | ||
+ | do { | ||
+ | printf(" | ||
+ | scanf(" | ||
+ | } while (number != 1234); | ||
+ | </ |