Course content
Chapter 1: Getting started with the C Language
Presentation of the C language and its role in embedded systems. Understand the importance of programming in the field of embedded systems. Overview of basic programming concepts.
0/6
Chapter 2: Exploring Variables and Operators
Discovery of variables: types, declaration, assignment Play with arithmetic operators through exercises
0/3
Chapter 3: Making Decisions with Conditional Statements
Introduction to Conditional Statements with Real-World Examples
0/1
Foundations of the C Language: First Steps in Programming (course 1)
About the lesson

I Introduction

Imagine yourself making coffee in the morning. Maybe you have a routine: If the coffee is too hot, you wait until it cools or add a little cold milk. If it's too cold, you warm it up a little. This ability to make decisions based on different situations is exactly what we want to build into our programs. In programming, we don't run the coffee shop, but we often have to process information and react accordingly.

In the world of programming, making decisions is essential. This allows our software to be dynamic, responsive and adaptive. If you're creating a game, for example, you might want certain events to happen if the player reaches a certain score, or other actions to trigger if the player loses all their lives. Decisions are everywhere!

A brief overview of the execution flow of a program

Normally, a program executes its instructions from top to bottom, like reading a book from the first page to the last. However, we don't always want everything to be so linear. Sometimes, depending on certain conditions (like in our coffee shop example), we want to change the order in which things happen or maybe skip certain parts altogether.

Conditional statements are like forks in the road. Depending on the situation, they guide you on a different path. Instead of just following a straight line, your program can now explore different routes depending on the conditions encountered. It's like having an intelligent GPS in your code that helps you make the best decisions in real time.

II. Instruction if and its complement else : Duality in decision-making

1.Instruction if : What is that ?

Instruction if is one of the most fundamental control structures in programming. It allows your program to make decisions based on certain conditions. If the specified condition is true, the associated code block will be executed. Otherwise, this code block will be ignored.

Think of it like an automatic light in a room. If someone enters (condition: presence of a person), the light turns on. If no one enters, the light stays off. Instruction if works the same way: it “turns on” and executes a block of code only if a specific condition is met.

It is useful for many reasons:

  • Adaptability : Your code can react to different scenarios.
  • Selectivity : You can choose to run certain parts of your code only when necessary.
  • Intelligence : Your program can make decisions based on the information it processes.

2.Instruction else : What is that ?

Just like if allows you to execute a block of code when the condition is true, else allows us to execute a block of code when this condition is false. It is the natural complement of if, offering a solution for cases where the condition if is not satisfied.

Let's take the example of automatic light again: with only one if, we could say “if someone comes in, turn on the light”. With the addition of else, we can add “otherwise, turn it off”. This is what makes the combination if-else so powerful: it covers all possibilities.

3. Practical example on VS Code: checking if a variable is positive

Open VS Code and create a new file named positif.c. Copy the following code there:

#include <stdio.h>

int main() {
    int number;

    printf("Please enter a number: ");
    scanf("%d", &number);

    if (number > 0) {
        printf("The number %d is positive\n", number);
    } else {
        printf("The number %d is not positive\n", number);
    }

    return 0;
}

This program asks the user to enter a number. It then uses the instructions if et else to check if this number is positive. If so, it displays that the number is positive. Otherwise, it displays the opposite.

Let's decipher together what you just typed:

  • After retrieving a number using scanf, we arrive at an if statement. This is where the magic of decisions happens.
  • if (number > 0) { … }: This line checks if the number entered is greater than zero.
    • The braces { … } delimit what should be executed if the condition is true.
  • Braces Tip: Think of braces as doors. If the condition is true, the door opens and the code inside is executed.
  • else { … }: What if the condition is not true? This is where else comes in. If if is the door that doesn't open, else is the emergency door that opens instead.
  • printf(“The number %d is positive.n”, number); and printf(“The number %d is not positive.n”, number); : These two lines are your personalized messages depending on whether the number is positive or not.

To run this example:

  1. Compile the file with the command: gcc positif.c -o positif
  2. Run the program with the command: ./positif
  3. Enter a number and see the result!

This example perfectly illustrates how we can use the instructions if and else to make simple decisions based on information provided by the user.

III. Instruction else if : Navigate through multiple scenarios

1.Instruction else if : What is that ?

After discovering the potential of the duo if-else, you might ask yourself: “What if I have multiple conditions to check, not just two?” This is where comes in else if.

When we have several scenarios or conditions to check, the instruction else if acts as an intermediary between if et else, allowing successive evaluation of the conditions until one of them is true.

2. Practical example on VS Code: Classifying a number

Imagine we want to classify a user-entered number as either negative, zero, or positive. It's a perfect case to combine if, else if et else:

#include <stdio.h>

int main() {
    int number;

    printf("Enter a number: ");
    scanf("%d", &number);

    if (number > 0) {
        printf("The number %d is positive\n", number);
    } else if (number < 0) {
        printf("The number %d is negative\n", number);
    } else {
        printf("The number is zero\n");
    }

    return 0;
}

Think of it as a light

IV. Combination with comparison operators

1. Introduction to comparison operators

Comparison operators are used to compare two values. Here are the most common:

  • == : equal to
  • != : different from
  • > : better than
  • < : less than
  • >= : greater than or equal to
  • <= : less than or equal to

In conditional statements, these operators are crucial. They allow you to test relationships between values ​​and execute different blocks of code based on the results of these tests.
Think about a dating app. The app can show different profiles depending on the age range you are looking for. If you are looking for people over 30 years old, the application uses the operator > to filter the results.

2. Practical example on VS Code: determining if a user is an adult based on their age

Imagine that we want to know if the user of our program is an adult or not. This is a classic scenario for using comparison operators with if. Here is how we could write such a program:

#include <stdio.h>

int main() {
    int age;
    printf("Enter your age: ");
    scanf("%d", &age);

    if (age >= 18) {
        printf("You are an adult in most countries.\n");
    } else {
        printf("You are a minor.\n");
    }

    if (age == 18) {
        printf("Congratulations, you just became an adult!\n");
    } else if (age  18) {
        printf("You are over 18 years old.\n");
    }

    return 18;
}

Open VS Code and create a new file named age_verifier.c.

  • Copy and paste the code, then save the file.
  • Compile the file with the command: gcc age_verifier.c -o age_verifier.
  • Run the program with the command: ./age_verifier.
  • Enter your age and watch the program respond differently depending on the age you enter.

V.Practical exercises: Some simple problems to put into practice what has been learned

Here are some exercises to reinforce the understanding of conditional statements and comparison operators in C. For each exercise, I will include a description of what you will need to do as well as the code skeleton you will need to complete.

Exercise 1: Even or odd?

Write a program that asks the user to enter an integer and determines whether that number is even or odd.

#include <stdio.h>

int main() {
    int number;

    // Ask the user to enter an integer

    // Use the modulo operator to check if the number is even or odd

    // Display the result

    return 0;
}

Exercise 2: Notes and grades

Write a program that asks the user to enter a grade (out of 100) and displays the corresponding grade according to the following grading system: A for 90-100, B for 80-89, C for 70-79, D for 60-69, and F for less than 60.

#include <stdio.h>

int main() {
    int note;

    // Ask the user to enter a note

    // Use if, else if and else to determine the grade

    // Show rank

    return 0;
}

Exercise 3: Age and categories

Write a program that asks for the user's age and informs them which age category they are in: child (0-12), adolescent (13-17), adult (18-64), or senior ( 65+).

#include <stdio.h>

int main() {
    int age;

    // Ask the user to enter their age

    // Use multiple if/else if statements to determine age category

    // Show age category

    return 0;
}

Exercise 4: Comparing numbers

Write a program that asks the user for two numbers and displays the larger of the two.

#include <stdio.h>

int main() {
    int num1, num2;

    // Ask the user to enter two numbers

    // Use if/else to compare numbers and determine which is larger

    // Display the largest number

    return 0;
}

Conclusion

Course 1: Introduction to C Language Programming for Embedded Systems has equipped you with the essential skills to begin navigating the vast world of C programming. We explored the origins of the language, learned how to establish a development environment , and carried out simple but fundamental programs.

The chapters have been designed to build your understanding block by block – from the basic structure of a C program, to manipulating variables and operators, and finally, to applying conditional statements that are crucial for flow control execution in embedded programs.

Anticipation of Course 2: The Fundamentals of Loops and Functions Now, let's turn our attention to Course 2. We will delve into the fundamentals of loops and functions. You will learn how to automate repetitive tasks without rewriting code unnecessarily, how to simplify complex programs and make them more efficient using for, while, and do-while loops.

Functions will also be a key topic, where you will discover how to modularize your code for better reusability and readability. These are valuable skills for programming embedded systems, where efficiency and optimization are paramount.

By mastering these tools, you will be well prepared to tackle more advanced programming problems specific to embedded systems. Get ready to dive deeper into C programming with enthusiasm and curiosity. Course 2 awaits you with captivating concepts and additional practical skills to enrich your programming journey.

Participate in the discussion
Add to favourites