Beruflich Dokumente
Kultur Dokumente
Limbajul de programare C
expression;
expression-statement
if-else, else-if, switch
selection-statement
for, while, do-while
iteration-statement
goto, continue, break, return
jump-statement
06.02.2018 Lucian Cucu - The C Programming Language 2
West University of Timisoara
Faculty of Mathematics and Informatics Programming I
Permit selectia unei alternative din doua sau mai multe sub controlul unei
expresii.
Instructiune: if-else Selection from multiple variants
Sintaxa:
if(expr) if(expr1) /*if TRUE */
statement;
statement-1;
statement-3;
… /*if none is TRUE*/
else
statement;
Instructiune: switch
Presupunand ca
Sintaxa:
expr == const_expr1 is TRUE
switch(expr) {
case const_expr1:
Presupunand ca
statement_1;
expr == const_expr2 este TRUE
case const_expr2:
statement_2; executie
Presupunanad ca
case const_expr3: expr == const_expr3 este TRUE
statement_3;
…
default: Presupunand ca
statement; expr NU este egala cu nici una din
} const_expri
Instructiune: switch
Sintaxa:
switch(expr) { if(expr==const_expr1)
case const_expr1:
statement-1;
statement-1;
break;
else if(expr==const_expr2)
case const_expr2:
statement-2; statetment-2;
break;
Secvente echivalente
06.02.2018 Lucian Cucu - The C Programming Language 5
West University of Timisoara
Faculty of Mathematics and Informatics Programming I
•for
•while } - testarea conditiei de terminare la inceput expr
TRUE
statement
FALSE
expr TRUE
FALSE
Instructiune: for
Sintaxa:
for(expr1; expr2; expr3)
statement;
Executie:
evaluate expr1
evaluate expr2; if TRUE (that is: if non 0) if FALSE (that is: if 0)
execute statement
evaluate expr3
Terminologie:
expr1 pasul de initializare (poate lipsi)
expr2 conditie (poate lipsi)
statement corpul ciclului (orice instructiune, inclusiv for sau o instructiune multipla)
expr3 pasul de modificare (poate lipsi)
06.02.2018 Lucian Cucu - The C Programming Language 7
West University of Timisoara
Faculty of Mathematics and Informatics Programming I
for( ; ; )
{... /* ciclu infinit*/
if(expr)
break; /*alternative: return sau goto label */
...
}
for(e1; e2; e3)
{... /*cicluri imbricate */
for(e4; e5; e6)
{...
for(e7; e8; e9)
{...
}
}
}
06.02.2018 Lucian Cucu - The C Programming Language 8
West University of Timisoara
Faculty of Mathematics and Informatics Programming I
Instructiune: while
Sintaxa: while(expression)
statement;
Executie:
evaluate expression;if TRUE, that is: if non 0 if FALSE, that is: if 0
execute statement
E.g.
while((c=getchar()) != EOF) /* ciclu tipic pt citirea unui caracter de la stdin */
…;
Instructiune: do-while
Sintaxa:
do
statement;
while(expression);
Executie:
execute statement
evaluate expression; if TRUE, that is: if non 0 if FALSE, that is: 0
Instructiuni:
continue
break
Sintaxa:
continue;
break;
Utilizare:for(i=0; i<N; i++)
{
... Afisarea elementelor pozitive din
if(t[i]<0) tabloul t care preced primul
continue; element nul.
...
if(t[i]==0)
break;
printf(“%10d” ,t[i]);
}
Instructiune: return
Sintaxa:
return;
return expression;
Executie:
Type Function();
void Function();
Caller()
Caller() {
{ Type variable;
... ...
Function(); variable=Function();
... ...
} }
Unitati functionale :
- functii (in programele C : functii care returneaza o valoare)
- proceduri (in programele C : functii declarate de tip void )
06.02.2018 Lucian Cucu - The C Programming Language 15