Add module3_controlflow.c
This commit is contained in:
206
src/c_examples/module3_controlflow.c
Normal file
206
src/c_examples/module3_controlflow.c
Normal file
@@ -0,0 +1,206 @@
|
|||||||
|
// ============================================
|
||||||
|
// Modul 3: Flusskontrolle
|
||||||
|
// Beispiele in C
|
||||||
|
// ============================================
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
// Beispiel 3.1: If-Else Strukturen
|
||||||
|
void demo_if_else() {
|
||||||
|
printf("=== If-Else Strukturen ===\n\n");
|
||||||
|
|
||||||
|
int score = 75;
|
||||||
|
|
||||||
|
// Einfache Bedingung
|
||||||
|
if (score >= 60) {
|
||||||
|
printf("Bestanden!\n");
|
||||||
|
} else {
|
||||||
|
printf("Durchgefallen!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verschachtelt
|
||||||
|
if (score >= 90) {
|
||||||
|
printf("Note: A\n");
|
||||||
|
} else if (score >= 80) {
|
||||||
|
printf("Note: B\n");
|
||||||
|
} else if (score >= 70) {
|
||||||
|
printf("Note: C\n");
|
||||||
|
} else {
|
||||||
|
printf("Note: D oder schlechter\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ternary Operator (kurze If-Else)
|
||||||
|
char* result = (score >= 60) ? "PASS" : "FAIL";
|
||||||
|
printf("Ergebnis: %s\n\n", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Beispiel 3.2: Switch-Case
|
||||||
|
void demo_switch() {
|
||||||
|
printf("=== Switch-Case ===\n\n");
|
||||||
|
|
||||||
|
int day = 3;
|
||||||
|
|
||||||
|
switch (day) {
|
||||||
|
case 1:
|
||||||
|
printf("Montag\n");
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
printf("Dienstag\n");
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
printf("Mittwoch\n");
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
case 5:
|
||||||
|
printf("Donnerstag oder Freitag\n");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
printf("Wochenende\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vorsicht: Ohne break = Fall-through!
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Beispiel 3.3: For-Loop
|
||||||
|
void demo_for_loop() {
|
||||||
|
printf("=== For-Loop ===\n\n");
|
||||||
|
|
||||||
|
// Standard
|
||||||
|
printf("Standard: ");
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
printf("%d ", i);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Rückwärts
|
||||||
|
printf("Rückwärts: ");
|
||||||
|
for (int i = 5; i > 0; i--) {
|
||||||
|
printf("%d ", i);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Mit Schrittgröße
|
||||||
|
printf("Schritt 2: ");
|
||||||
|
for (int i = 0; i < 10; i += 2) {
|
||||||
|
printf("%d ", i);
|
||||||
|
}
|
||||||
|
printf("\n\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Beispiel 3.4: While-Loops
|
||||||
|
void demo_while_loops() {
|
||||||
|
printf("=== While-Loops ===\n\n");
|
||||||
|
|
||||||
|
// While (Bedingung zuerst)
|
||||||
|
int count = 0;
|
||||||
|
printf("While: ");
|
||||||
|
while (count < 5) {
|
||||||
|
printf("%d ", count);
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Do-While (Bedingung am Ende, mindestens 1 Durchlauf)
|
||||||
|
int num = 10;
|
||||||
|
printf("Do-While (num=10, Bedingung <5): ");
|
||||||
|
do {
|
||||||
|
printf("%d ", num); // Wird trotzdem ausgeführt!
|
||||||
|
num++;
|
||||||
|
} while (num < 5);
|
||||||
|
printf("\n\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Beispiel 3.5: Break und Continue
|
||||||
|
void demo_control_flow() {
|
||||||
|
printf("=== Break und Continue ===\n\n");
|
||||||
|
|
||||||
|
// Continue: Überspringt Rest der Iteration
|
||||||
|
printf("Continue (ungerade überspringen): ");
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
if (i % 2 == 1) continue; // Ungerade überspringen
|
||||||
|
printf("%d ", i);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Break: Beendet Schleife sofort
|
||||||
|
printf("Break (beendet bei 5): ");
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
if (i == 5) break;
|
||||||
|
printf("%d ", i);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Break in verschachtelten Schleifen
|
||||||
|
printf("\nBreak aus verschachtelter Schleife:\n");
|
||||||
|
bool found = false;
|
||||||
|
for (int i = 0; i < 3 && !found; i++) {
|
||||||
|
for (int j = 0; j < 3; j++) {
|
||||||
|
if (i == 1 && j == 1) {
|
||||||
|
printf("Gefunden bei (%d, %d)!\n", i, j);
|
||||||
|
found = true;
|
||||||
|
break; // Nur innere Schleife!
|
||||||
|
}
|
||||||
|
printf("(%d, %d) ", i, j);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Beispiel 3.6: Endlosschleife mit Abbruch
|
||||||
|
void demo_infinite_loop() {
|
||||||
|
printf("=== Endlosschleife (mit Abbruch) ===\n\n");
|
||||||
|
|
||||||
|
int counter = 0;
|
||||||
|
|
||||||
|
while (1) { // Endlos
|
||||||
|
printf("Iteration %d\n", counter);
|
||||||
|
counter++;
|
||||||
|
|
||||||
|
if (counter >= 3) {
|
||||||
|
printf("Abbruchbedingung erreicht!\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Beispiel 3.7: Nested Loops ( verschachtelt)
|
||||||
|
void demo_nested_loops() {
|
||||||
|
printf("=== Verschachtelte Schleifen ===\n\n");
|
||||||
|
|
||||||
|
// Multiplikationstabelle
|
||||||
|
printf("Multiplikationstabelle 1-5:\n");
|
||||||
|
for (int i = 1; i <= 5; i++) {
|
||||||
|
for (int j = 1; j <= 5; j++) {
|
||||||
|
printf("%3d ", i * j);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================
|
||||||
|
// Hauptprogramm
|
||||||
|
// ============================================
|
||||||
|
int main() {
|
||||||
|
demo_if_else();
|
||||||
|
demo_switch();
|
||||||
|
demo_for_loop();
|
||||||
|
demo_while_loops();
|
||||||
|
demo_control_flow();
|
||||||
|
demo_infinite_loop();
|
||||||
|
demo_nested_loops();
|
||||||
|
|
||||||
|
printf("=== Wichtige Konzepte ===\n");
|
||||||
|
printf("1. If-Else: Bedingte Ausführung\n");
|
||||||
|
printf("2. Switch: Mehrfachauswahl (nur für diskrete Werte)\n");
|
||||||
|
printf("3. For: Bekannte Iterationszahl\n");
|
||||||
|
printf("4. While: Unbekannte Iterationszahl\n");
|
||||||
|
printf("5. Continue: Nächste Iteration\n");
|
||||||
|
printf("6. Break: Schleife beenden\n");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user