1 / 17

Laboratorio di Linguaggi lezione di ripasso costrutti

Università dell’Insubria Facoltà di Scienze Matematiche, Fisiche e Naturali di Varese Corso di Laurea in Informatica Anno Accademico 2007/08. Laboratorio di Linguaggi lezione di ripasso costrutti. Marco Tarini. Ripasso costrutti base :. Istruzioni di Controllo del Flusso nozioni di

Télécharger la présentation

Laboratorio di Linguaggi lezione di ripasso costrutti

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Università dell’Insubria Facoltà di Scienze Matematiche, Fisiche e Naturali di Varese Corso di Laurea in Informatica Anno Accademico 2007/08 Laboratorio di Linguaggilezione di ripasso costrutti Marco Tarini

  2. Ripasso costrutti base: Istruzioni di Controllo del Flusso nozioni di sintassi e semantica (intuitivamente, non formalmente) M a r c o T a r i n i - L a b o r a t o r i o d i L i n g u a g g i - 2 0 0 7 / 0 8 - U n i v e r s i t à d e l l ’ I n s u b r i a

  3. if then else • costrutto condizionale if (<expression>) <statement1>else<statement2> Come gia’ detto, esegue statement1 (il ramo then) sse l’espressione risulta diversa da zero M a r c o T a r i n i - L a b o r a t o r i o d i L i n g u a g g i - 2 0 0 7 / 0 8 - U n i v e r s i t à d e l l ’ I n s u b r i a

  4. <statement> nota: punto e virgola... niente punto-e-virgola! • Esempi di <statement>s: y = x + 10; { y = x + 10; z = 4; }; M a r c o T a r i n i - L a b o r a t o r i o d i L i n g u a g g i - 2 0 0 7 / 0 8 - U n i v e r s i t à d e l l ’ I n s u b r i a

  5. if then else • costrutto condizionale if (<expression>) <statement1>else<statement2> if (x) y = x + 10; else y = 20; if (x==2) {y = x + 10; ... } else y = 20; M a r c o T a r i n i - L a b o r a t o r i o d i L i n g u a g g i - 2 0 0 7 / 0 8 - U n i v e r s i t à d e l l ’ I n s u b r i a

  6. Lo sapevate che... • In C, quasi tutti gli statement sono anche espressioni? • Ad esempio, l’assegnamento e’ anche un’espressione, che vale il valore assegnato (e ha anche il suo tipo) • Cio’ consente di scrivere, per esempio:(non solo e’ coinciso, ma e’ anche efficiente,come da filosofia C) x = y int x,y,z; x = y = z = 10 ; M a r c o T a r i n i - L a b o r a t o r i o d i L i n g u a g g i - 2 0 0 7 / 0 8 - U n i v e r s i t à d e l l ’ I n s u b r i a

  7. if then else: trappole • errore di sintassi (non compila, innocuo) • errore nella guardia (compila: errore “cattivo”. Cosa fa?) • punto-e-virgola di troppo(compila: errore “cattivo”. Cosa fa?) if (x==2) {y = x + 10; ... } else y = 20; if (x=2) { y = x + 10; ... } if (x==2); { y = x + 10; ... } M a r c o T a r i n i - L a b o r a t o r i o d i L i n g u a g g i - 2 0 0 7 / 0 8 - U n i v e r s i t à d e l l ’ I n s u b r i a

  8. for • costrutto iterativo for (<expr0>;<expr1>;<expr2>) <stat> for (i=0,j=10;(j>20) &&(i<5); i++, j--) { vect[i]+=10; printf(“%d”, vect[i]); } M a r c o T a r i n i - L a b o r a t o r i o d i L i n g u a g g i - 2 0 0 7 / 0 8 - U n i v e r s i t à d e l l ’ I n s u b r i a

  9. while • ciclo while while (<expr0>) <stat> while (i<N && a[i]< 1000) i = i + 1; M a r c o T a r i n i - L a b o r a t o r i o d i L i n g u a g g i - 2 0 0 7 / 0 8 - U n i v e r s i t à d e l l ’ I n s u b r i a

  10. do while • ciclo do-while do <stat> while (<expr0>) do { printf("Immettere un valore intero pari\n"); scanf("%d",&a); /* leggi il numero a da tastiera */ } while(a%2); M a r c o T a r i n i - L a b o r a t o r i o d i L i n g u a g g i - 2 0 0 7 / 0 8 - U n i v e r s i t à d e l l ’ I n s u b r i a

  11. switch • costrutto condizionale a più vie switch (<expr0>) { case <const1>: <stat1>break; ... case <const2>: <stat2>break; } switch (ch){ case 'a': cont_a++; break; case 'e': cont_e++; break; case 'i': cont_i++; break; case 'o': cont_o++; break; case 'u': cont_u++; break; default : cont_car++; } M a r c o T a r i n i - L a b o r a t o r i o d i L i n g u a g g i - 2 0 0 7 / 0 8 - U n i v e r s i t à d e l l ’ I n s u b r i a

  12. switch • senza break: fall trought switch (oggi){ case LUN: ... /* gestisci il caso LUN */ break; case MAR: ... /* gestisci il caso MAR */ break; case SAB: ... /* operazioni solo per il SAB */ /* FALL TROUGHT */ case DOM: ... /* gestisci per il SAB e la DOM */ break; default : ... /* gestisci gli altri casi */ } M a r c o T a r i n i - L a b o r a t o r i o d i L i n g u a g g i - 2 0 0 7 / 0 8 - U n i v e r s i t à d e l l ’ I n s u b r i a

  13. labels e goto • salti non strutturati • cattiva pratica di programmazione • (porta a programmi spaghetti) • perlomeno, non eleganti <label>: goto <label>; M a r c o T a r i n i - L a b o r a t o r i o d i L i n g u a g g i - 2 0 0 7 / 0 8 - U n i v e r s i t à d e l l ’ I n s u b r i a

  14. labels e goto • ad esempio: • equivalente a: i=0; while(i<=NUM) printf("%d \n",++i); ...             i=0;    INIZIO : if(i>NUM)goto FINE;             printf("%d \n",++i); goto INIZIO;    FINE :   ... M a r c o T a r i n i - L a b o r a t o r i o d i L i n g u a g g i - 2 0 0 7 / 0 8 - U n i v e r s i t à d e l l ’ I n s u b r i a

  15. break e continue • utilizzabili in tutti i cicli • for, while, do-while • break = esci dal ciclo • (vai alla prima iterazione dopo il ciclo) • continue = interrompi l’iterazone corrente • vai all’inizio della prossima iterazione • (dove, per prima cosa, la guardia di uscita dal ciclo viene testata) M a r c o T a r i n i - L a b o r a t o r i o d i L i n g u a g g i - 2 0 0 7 / 0 8 - U n i v e r s i t à d e l l ’ I n s u b r i a

  16. Fine ripassino • Fine ripassino M a r c o T a r i n i - L a b o r a t o r i o d i L i n g u a g g i - 2 0 0 7 / 0 8 - U n i v e r s i t à d e l l ’ I n s u b r i a

  17. :) dahttp://xkcd.com M a r c o T a r i n i - L a b o r a t o r i o d i L i n g u a g g i - 2 0 0 7 / 0 8 - U n i v e r s i t à d e l l ’ I n s u b r i a

More Related