This shows you the differences between two versions of the page.
cs:c_language:while_do-while_for_loops_1 [2019/02/26 14:35] |
cs:c_language:while_do-while_for_loops_1 [2020/11/26 23:18] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== While, do-while and for loops (Example 1) ====== | ||
+ | **Concepts: | ||
+ | A simple example on the use of the loop constructs: '' | ||
+ | **Text:**\\ | ||
+ | Realize a program that: | ||
+ | * prints as output the numbers between '' | ||
+ | * only '' | ||
+ | |||
+ | **Solution (while): | ||
+ | <file C while_1.c> | ||
+ | /* | ||
+ | Realize a program that prints as output numbers between 0 and n (with n>0). | ||
+ | */ | ||
+ | |||
+ | #include < | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | unsigned int i, n; | ||
+ | |||
+ | printf(" | ||
+ | scanf(" | ||
+ | | ||
+ | i=0; | ||
+ | while(i< | ||
+ | printf(" | ||
+ | i++; | ||
+ | } | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | **Solution (do-while): | ||
+ | <file C do-while_1.c> | ||
+ | /* | ||
+ | Realize a program that prints as output numbers between 0 and n (with n>0). | ||
+ | */ | ||
+ | |||
+ | #include < | ||
+ | int main() | ||
+ | { | ||
+ | unsigned int i, n; | ||
+ | |||
+ | printf(" | ||
+ | scanf(" | ||
+ | |||
+ | i=0; | ||
+ | do{ | ||
+ | printf(" | ||
+ | i++; | ||
+ | }while(i< | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | **Soluzione (for):**\\ | ||
+ | <file C for_1.c> | ||
+ | /* | ||
+ | Realize a program that prints as output numbers between 0 and n (with n>0). | ||
+ | */ | ||
+ | |||
+ | #include < | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | unsigned int i=0, n; | ||
+ | |||
+ | printf(" | ||
+ | scanf(" | ||
+ | |||
+ | for(i=0; | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ |