User Tools

Site Tools


it:informatica:linguaggio_c:costrutti_while_do-while_for_1
Return to Home page

Differences

This shows you the differences between two versions of the page.


it:informatica:linguaggio_c:costrutti_while_do-while_for_1 [2024/04/08 22:35] (current) – created - external edit 127.0.0.1
Line 1: Line 1:
 +====== Costrutti while, do-while e for (Esempio 1) ======
 +**Concetti:**\\
 +Semplice esempio di utilizzo dei costrutti iterativi //while//, //do-while// e //for//
  
 +**Testo:**\\
 +Si realizzi una programma che:
 +  * stampi a schermo i numeri da 0 a n
 +  * si considerino solo numeri //n>0//
 +
 +**Soluzione (while):**\\
 +<file C while_1.c>
 +/*
 +Realizzare un programma che stampi a schermo i numeri da 0 a n (con n>0).
 +*/
 +
 +#include <stdio.h>
 +
 +int main()
 +{
 +  unsigned int i, n;
 +
 +  printf("Numero di elementi: ");
 +  scanf("%d", &n);
 +  
 +  i=0;
 +  while(i<=n){
 +    printf("%3d\n", i);
 +    i++;
 +  }
 +
 +  return 0;
 +}
 +</file>
 +
 +**Soluzione (do-while):**\\
 +<file C do-while_1.c>
 +/*
 +Realizzare un programma che stampi a schermo i numeri da 0 a n (con n>0).
 +*/
 +
 +#include <stdio.h>
 +int main()
 +{
 +  unsigned int i, n;
 +
 +  printf("Numero di elementi: ");
 +  scanf("%d", &n);
 +
 +  i=0;
 +  do{
 +    printf("%3d\n", i);
 +    i++;
 +  }while(i<=n);
 +
 +  return 0;
 +}
 +</file>
 +
 +**Soluzione (for):**\\
 +<file C for_1.c>
 +/*
 +Realizzare un programma che stampi a schermo i numeri da 0 a n (con n>0).
 +*/
 +
 +#include <stdio.h>
 +
 +int main()
 +{
 +  unsigned int i=0, n;
 +
 +  printf("Numero di elementi: ");
 +  scanf("%d", &n);
 +
 +  for(i=0;i<=n;i++){ /* E' lo stesso di for(;i<=n;i++) ma solo perche' i e' stata inizializzata prima */
 +    printf("%3d\n", i);
 +  }
 +
 +  return 0;
 +}
 +</file>

If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it

You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/it/informatica/linguaggio_c/costrutti_while_do-while_for_1?rev=1551188125&do=diff

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki
Privacy Policy