C - Decision Making: A Beginner's Guide

Hello there, future programmers! Welcome to the exciting world of C programming. I'm thrilled to be your guide on this journey through one of the most fundamental concepts in coding: decision making. As someone who's been teaching C for over a decade, I can assure you that mastering these concepts will be a game-changer in your programming adventure. So, let's dive in!

C - Decision Making

If Statement in C Programming

Imagine you're at an ice cream shop, and you need to decide whether to buy a cone or not. In programming, we use the 'if' statement for such decisions. It's like asking a question and doing something based on the answer.

Here's how it looks in C:

if (condition) {
    // code to be executed if the condition is true
}

Let's see a real example:

#include <stdio.h>

int main() {
    int scoops = 2;

    if (scoops > 1) {
        printf("Wow, that's a lot of ice cream!\n");
    }

    return 0;
}

In this example, if the number of scoops is greater than 1, it will print the message. Try changing the value of 'scoops' and see what happens!

If...else Statement in C Programming

Now, what if we want to do something when the condition is false? That's where 'if...else' comes in handy. It's like having a Plan B.

if (condition) {
    // code to be executed if the condition is true
} else {
    // code to be executed if the condition is false
}

Let's expand our ice cream example:

#include <stdio.h>

int main() {
    int money = 5;
    int ice_cream_cost = 6;

    if (money >= ice_cream_cost) {
        printf("Yay! You can buy an ice cream!\n");
    } else {
        printf("Sorry, not enough money for ice cream today.\n");
    }

    return 0;
}

Here, we're checking if we have enough money for ice cream. If we do, great! If not, well, there's always next time.

Nested If Statements in C Programming

Sometimes, life (and programming) isn't just about one decision. We often need to make decisions within decisions. That's where nested 'if' statements come in.

if (outer_condition) {
    if (inner_condition) {
        // code to be executed if both conditions are true
    }
}

Let's see a more complex ice cream scenario:

#include <stdio.h>

int main() {
    int temperature = 30;
    int has_money = 1; // 1 means true, 0 means false

    if (temperature > 25) {
        if (has_money) {
            printf("It's hot and you have money. Time for ice cream!\n");
        } else {
            printf("It's hot, but no money for ice cream. Maybe a cold shower?\n");
        }
    } else {
        printf("It's not that hot. Save your money for a rainy day.\n");
    }

    return 0;
}

This program considers both the temperature and whether you have money before suggesting ice cream.

Switch Statement in C Programming

The 'switch' statement is like a multi-flavor ice cream cone of decision making. It's perfect when you have multiple options to choose from.

switch(expression) {
    case constant1:
        // code to be executed if expression equals constant1
        break;
    case constant2:
        // code to be executed if expression equals constant2
        break;
    ...
    default:
        // code to be executed if expression doesn't match any constants
}

Let's use it to choose ice cream flavors:

#include <stdio.h>

int main() {
    int flavor = 2;

    switch(flavor) {
        case 1:
            printf("You chose vanilla. Classic!\n");
            break;
        case 2:
            printf("Chocolate it is. Yum!\n");
            break;
        case 3:
            printf("Strawberry, a fruity delight!\n");
            break;
        default:
            printf("We don't have that flavor. How about vanilla?\n");
    }

    return 0;
}

This program selects a message based on the chosen flavor number.

The ?: Operator in C Programming

The ternary operator (?:) is like a shorthand 'if...else' statement. It's perfect for quick, simple decisions.

condition ? expression_if_true : expression_if_false;

Here's a concise way to decide on dessert:

#include <stdio.h>

int main() {
    int is_hungry = 1; // 1 for true, 0 for false

    printf(is_hungry ? "Let's get ice cream!\n" : "Maybe later.\n");

    return 0;
}

This one-liner checks if you're hungry and suggests ice cream if you are.

The Break Statement in C Programming

The 'break' statement is like saying "I'm done here" in the middle of a conversation. It's used to exit loops or switch statements early.

#include <stdio.h>

int main() {
    int i;
    for(i = 1; i <= 10; i++) {
        if(i == 5) {
            printf("Found 5! Let's stop here.\n");
            break;
        }
        printf("%d ", i);
    }
    return 0;
}

This program counts to 10 but stops when it reaches 5.

The Continue Statement in C Programming

'Continue' is like saying "Skip this and move on" in a conversation. It skips the rest of the current iteration in a loop and moves to the next one.

#include <stdio.h>

int main() {
    int i;
    for(i = 1; i <= 5; i++) {
        if(i == 3) {
            printf("Oops, let's skip 3.\n");
            continue;
        }
        printf("Ice cream scoop number %d\n", i);
    }
    return 0;
}

This program counts ice cream scoops but skips number 3.

The goto Statement in C Programming

The 'goto' statement is like a teleportation device in your code. It jumps to a labeled statement. However, use it sparingly as it can make code hard to follow.

#include <stdio.h>

int main() {
    int scoops = 0;

    scoop_more:
    scoops++;
    printf("Added a scoop. Total: %d\n", scoops);

    if(scoops < 3) {
        goto scoop_more;
    }

    printf("Ice cream sundae complete!\n");
    return 0;
}

This program adds scoops to your sundae until you have three.

Here's a table summarizing all the decision-making methods we've covered:

Method Purpose Syntax
if Simple condition check if (condition) { ... }
if...else Two-way decision if (condition) { ... } else { ... }
Nested if Multiple conditions if (condition1) { if (condition2) { ... } }
switch Multiple options switch(expression) { case constant: ... }
?: Quick, simple decision condition ? true_expression : false_expression
break Exit loop or switch break;
continue Skip to next iteration continue;
goto Jump to labeled statement goto label;

Remember, practice makes perfect! Try writing your own programs using these concepts. Before you know it, you'll be making decisions in C like a pro. Happy coding!

Credits: Image by storyset