- Codingstandard
- HW1: Häufige Fehler
- Variablen & Operatoren
- Abfragen & Schleifen
[...]
if(a>=18) {
if(wd != 0){if( 100<=m) {
e = 1;printf("Herzlich willkommen im Casino!\n");
} else { printf("Zugang verweigert!\n");}
}else { puts("Zugang verweigert!");}
} else { printf("Zugang verweigert!\n");}
[...]
Einheitliche Formatierung des Programms um die Zusammenarbeit zu vereinfachen.
if (age >= 18 && well_dressed && money >= 100)
{
entry = TRUE;
printf("Herzlich willkommen im Casino!\n");
}
else
{
printf("Zugang verweigert!\n");
}
hw.c und hw abgeben!
Eine Variable ist eine Speicherstellen im Arbeitsspeicher, die einen Wert eines bestimmten Typs speichern kann.
int mit int age = 20;
age = 21;
age = age + 3;
printf("%d\n", age);
|
1int main()
{
2 int apples = 0;
3 apples = 5;
4 printf("I have %d apples.\n", apples);
5 return 0;
}
|
|
|
1int main()
{
2 int apples = 15;
3 apples = apples + 5;
4 apples = apples - 5;
5 int persons = 5;
6 int apple_per_person = apples/persons;
7 int more_persons = persons * 2;
8 return 0;
}
|
|
|
1int main()
{
2 int apples = 2;
3 int persons = 4;
4 int apple_per_person = apples/persons;
5 return 0;
}
|
|
|
1int main()
{
2 double apples = 2;
3 double persons = 4;
4 double apple_per_person = apples/persons;
5 return 0;
}
|
|
| Ganzzahlen |
|---|
| char |
| short |
| int |
| long |
| Dezimalzahlen |
|---|
| float |
| double |
| a + b; | Summe |
| a - b; | Differenz |
| a * b; | Produkt |
| a / b; | Division |
| a % b; | Modulo |
++i; i++;
--i; i--;
i += 3; i -= 2; i *= 4; i /= 5;
Code wird nur dann ausgeführt wenn eine bestimmte Bedingung erfüllt ist.
if(bedingung)
{
// wird ausgeführt wenn die Bedingung erüllt ist
}
else
{
// wird ausgeführt wenn die Bedingung nicht erfüllt ist
}
Als Bedingung wird meistens ein Vergleich verwendet (z.B. age > 18)
|
1int main()
{
2 int age = 17;
3 if(age >= 18)
{
printf("Willkommen im Casino!");
}
4 else
{
5 printf("Zutritt verweigert!");
}
6 return 0;
}
|
|
for, while, do (if ist keine Schleife!)
#include <stdio.h>
int main()
{
int beer = 99;
while(beer > 0)
{
printf("%d bottles of beer on the wall, %d bottles of beer.\n", beer, beer);
beer = beer - 1;
printf("Take one down and pass it around, %d bottles of beer on the wall.\n", beer);
}
printf("No more bottles of beer on the wall, no more bottles of beer.\n");
return 0;
}
/* While-Loop example */
#include <stdio.h>
int main()
{
int number_of_students = 20;
int student_id = 0; //Init
while(student_id < number_of_students) //Condition
{
printf("Student-ID: %d\n", student_id);
student_id++; //Step
}
return 0;
}
/* While-Loop example 2 */
#include <stdio.h>
int main()
{
int max_value = 100;
int current_value = 1;
int add_value = 2;
int iterations = 0; //Init
while(current_value < max_value) //Condition
{
current_value += add_value;
iterations++; //Step
}
printf("Iterations: %d\n", iterations);
return 0;
}
/* For-Loop example */
#include <stdio.h>
int main()
{
int number_of_students = 20;
int student_id = 0;
//Init, Condition
for (student_id = 0; student_id < number_of_students; student_id++)
{
printf("Student-ID: %d\n", student_id);
//Step
}
return 0;
}
Assignments!