Skip to content

C Decision Making

Decision making in C allows you to execute different code blocks based on conditions.

1. if Statement

c
if (condition) {
    // code to execute if condition is true
}

2. if-else Statement

c
if (condition) {
    // code if condition is true
} else {
    // code if condition is false
}

3. if-else if-else

c
if (condition1) {
    // code for condition1
} else if (condition2) {
    // code for condition2
} else {
    // code if none of the above conditions are true
}

4. switch Statement

c
switch (expression) {
    case constant1:
        // code for constant1
        break;
    case constant2:
        // code for constant2
        break;
    default:
        // code if no case matches
}

5. Conditional Operator

c
result = (condition) ? value_if_true : value_if_false;

Content is for learning and research only.