Logische Ausdr¨ucke:
Verkn¨upfungen von (int-) Variablen mittels lo- gischer Operatoren.
(&&, ||, ==, !=, ... - vgl. 2. ¨Ubung)
1 (6= 0) ... true 0 ... false
Beispiele:
x == y (x>=0.0) && (x<=1.0) !feof(datei) a < 10 (a>0)||(b>0)||(c>0) strcmp(s1,s2)
IF-Anweisung: Verzweigung im Programm
if-then if-then-else
if (AUSDRUCK) { if (AUSDRUCK) {
ANWEISUNGEN ANWEISUNGEN
} }
else {
ANWEISUNGEN }
AUSDRUCK logischer Ausdruck
ANWEISUNGEN Folge von Befehlen
Falls nur eine Anweisung auch:
if (Ausdruck) ANWEISUNG
{ }
Bsp. 1: Fehler vermeiden
double x, y, z;
if (y!=0.0) { z = x/y;
}
else {
printf("Fehler! Division durch 0.\n");
exit(-1);
}
Bsp 2: Geschachtelte Verzweigung
int a, b, c;
if (a>0 && b>0) { if (a>b) {
c = a-b;
}
else {
c = b-a;
} }
SWITCH-Anweisung: Alternativen
switch (AUSDRUCK) {
case KONST_1 : ANWEISUNGEN break;
case KONST_2 : ANWEISUNGEN break;
...
case KONST_n : ANWEISUNGEN break;
default : Anweisungen break;
}
AUSDRUCK int-Variable/-Ausdruck
KONST i int-Konstanten
. (keine Variablen oder Ausdr¨ucke)
Beispiel:
int note;
...
switch note {
case 1 : printf("Sehr gut\n");
break;
case 2 : printf("Gut\n");
break;
case 3 : printf("Befriedigend\n");
break;
case 4 : printf("Ausreichend\n");
break;
case 5 : printf("Ungen¨ugend\n");
break;
default : printf("Keine Note!\n");
break;
}