» Quick Introduction to C » 1. Basics » 1.4 Control Flow

Control Flow

Control flow in C is managed through a variety of control structures, such as conditional statements (if, else, switch) and loops (for, while, do-while).

If Else

#include <stdio.h>

int main() {
    int a = 58;
    if (a == 0) printf("a is 0\n");
    else if (a < 0) printf("a is negative\n");
    else {         
        printf("a is positive\n");
    }
    return 0;
}

Switch

swtich jumps to a matching value.

#include <stdio.h>

int main() {
    int dayOfWeek = 3;
    switch (dayOfWeek) {
        case 1:
            printf("Sunday\n");
            break;
        case 2:
            printf("Monday\n");
            break;
        case 3:
            printf("Tuesday\n");
            break;
        case 4:
            printf("Wednesday\n");
            break;
        case 5:
            printf("Thursday\n");
            break;
        case 6:
            printf("Friday\n");
            break;
        case 7:
            printf("Saturday\n");
            break;
        default:
            printf("Invalid number.\n");
    }
    return 0;
}

For Loops

In a for loop, the initialization, condition, and update expressions are specified within the parentheses. The loop body is executed repeatedly as long as the condition is true.

#include <stdio.h>

int main() {
    int sum = 0;
    for (int j = 1; j <= 10; ++j) {
        sum += j;
    }
    printf("Sum of numbers from 1 to 10: %d\n", sum);
    return 0;
}

While Loops

In a while loop, the condition is evaluated before entering the loop, and the loop continues executing as long as the condition is true.

#include <stdio.h>

int main() {
    int j = 1;
    int sum = 0;
    while (j <= 10) {
        sum += j;
        ++j;
    }
    printf("Sum of numbers from 1 to 10: %d\n", sum);
    return 0;
}

Do While Loops

In a do-while loop, the loop body is executed at least once, and the condition is checked after each iteration. If the condition is true, the loop continues; otherwise, it terminates.

do {
    sum += count;
    ++count;
} while (count <= 10);

Goto

The goto statement in C allows you to transfer control to another labeled part of the program. However, the use of goto is generally discouraged because it can lead to code that is harder to read and maintain.

#include <stdio.h>

int main() {
    int choice;

    menu: // label 'menu'
    printf("Menu:\n");
    printf("1. Option 1\n");
    printf("2. Option 2\n");
    printf("3. Exit\n");

    printf("Enter your choice (1-3): ");
    scanf("%d", &choice);

    switch (choice) {
        case 1:
            printf("You selected Option 1.\n");
            goto menu;
        case 2:
            printf("You selected Option 2.\n");
            goto menu;
        case 3:
            printf("Exiting the program.\n");
            break;
        default:
            printf("Invalid choice.\n");
            goto menu; 
    }

    return 0;
}

Tenary Operator

The ternary operator (? :) in C is a concise way to write a simple conditional expression. It takes three operands: a condition, a value to be returned if the condition is true, and a value to be returned if the condition is false.

#include <stdio.h>

int main() {
    int number = 58;

    // Use the ternary operator to assign a message based on the number's sign
    // You can embed one tenary operator into another one.
    const char *message = (number > 0) ? "Positive" : (number < 0) ? "Negative" : "Zero";

    printf("The number is %s.\n", message);
    return 0;
}

Code Challenge

Write a C program that uses the ternary operator to determine and print the corresponding letter grade.

Grading scale:

90-100: A
 80-89: B
 70-79: C
 60-69: D
  0-59: F
Loading...
> code result goes here
Prev
Next