Big Tuto: Program with the C language
From theory to practice
Chapter 7: Instructions
Tutorial presented by: The Ludovyc and Jérémie F. Bellanger (Jay)
The Ludovyc
Date of publication: June 8, 2014
Last update: March 9, 2025
Here we are at chapter 7. And we have already come a long way: from the simplest program to the integration of variables as well as their display, their uses and the operations that can be done with them! ![]()
Now we're going to take another big step in the next 2 chapters, so hang on tight! Here we go! ![]()
Here is the list of integer types arranged in descending order according to their size:
long(8) -> int(4) -> short(2) -> char(1)
So we can do:
int i;
char c = 1;
i = c;
but not:
c = i;
and we can also do during operations:
int i;
char c;
char c1;
short s;
i = c + c1 + s;
Note: You will not be able to exceed the maximum values of each variable type during operations.
Well, after this little clarification, let's come to the correction of the last exercise
:
#include <stdio.h>
#include <stdlib.h>
int main(){
long result ; //To be able to add the two numbers
int a;
int b;
printf("Please enter numbers between 0 and 2,147,483,647\n");
printf("Enter the first number: ");
scanf("%d", &a);
printf("Enter the second number: ");
scanf("%d", &b);
result = a + b;
printf("The result is: %d\n", result);
return 0;
}
Now here is a simplification of operations on variables, which will be useful to us when writing programs:
// [type] => the type of the chosen variable
// [var] => the name of the chosen variable
// <=> mathematical symbol of the equivalence
// Operation with 1:
[ type ] [ var ] ;
[ var ] = [ var ] + 1 ; <=> [ var ] ++ ; //increment by 1
[ var ] = [ var ] - 1 ; <=> [ var ] -- ; //decrement by 1
// Operation with x, where x is a variable respecting the Russian Doll rule:
[ type ] [ var ] ;
[ var ] = [ var ] + x ; <=> [ var ] += x ; //addition
[ var ] = [ var ] - x ; <=> [ var ] -= x ; //subtraction
[ var ] = [ var ] * x ; <=> [ var ] *= x ; //multiplication
[ var ] = [ var ] / x ; <=> [ var ] /= x ; //division
Examples with char type
Operation with 1:
char c=0;
c=c+1; <=> c++; //Yes, the name of the programming language c++ comes from there! ![]()
// c(0)=c(0)+1; c will be equal to 1
c=c-1; <=> c--; // c(0)=c(0)-1; c will be equal to -1
Operations with 2:
char c=1;
c=c+2; <=> c+=2; //c(1)=c(1)+2; c will be equal to 3
c=c-2; <=> c-=2; //c(1)=c(1)-2; c will be equal to -1
c=c*2; <=> c*=2; //c(1)=c(1)*2; c will be equal to 2
c=c/2; <=> c/=2; //c(1)=c(1)/2; c will be equal to 0
//Because c is the name of a variable of type char which is an whole number
Now, let’s get to the big stuff: programming tools!
Definition: A pseudo-program is written in pseudo-code without a real standard, but it must be readable by any programmer and its purpose is to provide a draft for writing a program in a chosen programming language.
Here is a pseudo-program in French which aims to display an empty rectangle formed with "*" whose length and width have been defined by the user:
|
Initialization:
length equals 0 //vertical
width equals 0 //horizontal
line equals 0
column equals 0
Start of empty rectangle program:
length input by user
user input width
for line less than length we increment line by 1 //for
switch on column: //switch
if equal to 0 //switch case
//do nothing
default //switch default
column equals 0
if line equals 0 or has length-1 //if
as long as column is less than width
if column is less than width-1 //if
display "*"
otherwise //else
display "*\n"
increment column by 1
otherwise //else
to do //do...
if column equals 0 //if
display "*"
moreover if column equals width-1 //else if
display "*\n"
otherwise //else
display " "
increment column by 1
while column is less than width //...while
End of empty rectangle program.
|
I've put in blue what you currently know how to code, in black what you don't know yet, and in green the comments and other indications.
I will describe what initialization, condition and reset are after describing each tool separately. I will also add some examples with the different existing conditions.![]()
Note: The For, While and Do-While tools are loops. The brace " { " represents the loop entry, and " } " the loop exit. In the code contained in these braces, if an instruction is:
- break : the loop will be stopped, and the program will exit it, without reading the rest of the contained code if there is any.
- continue : the loop will be stopped, and the program will directly execute another loop without reading the rest of the contained code after the instruction if there is any.
It is customary to shift the contained code by one tab relative to the header for better readability.

The For statement
for ( initialization; condition; reset )
{
//code content
} //exit
Functioning :
For is a loop that performs the initialization once. Then, in each round, it performs the reset, checks if the condition is true, and then executes the code inside. If the condition is false, the program will go to the exit. During initialization, you can initialize multiple variables by separating them with commas (e.g. x=0, y=0, z=0,...). During condition checking, you can check multiple conditions by separating them with commas (e.g. condition1, condition2,...). This is just like a multiple "and" condition where commas replace && (multiple "and" condition is described later in this chapter
). During reset, you can also reset multiple variables by separating them with commas (e.g. x++, y++, z++,...).
The Switch instruction
switch([variable name])
{
case[possible value of the variable] :
//code contained
//often a break instruction;
//... other "cases"
default :
//code content
//often a break statement;
} //exit
Functioning :
The Switch takes a variable as a parameter. At each " case ", an equality comparison is made with the possible value. If the condition is true, then the code contained is executed. If there is no break instruction , the following " cases " are read by the program, this is often a source of error, moreover
. The " default " is a unique and optional " case " whose equality condition is always true.
The If statement (if, moreover, else)
if (condition 1) //If
{
//code content 1
}
else if (condition 2) //Moreover if
{
//code content 2
}
else //Otherwise
{
//code content 3
} //output
Functioning :
The if is unique and mandatory, the "else if " is optional, and the "else" is unique but optional.
If condition 1 is true, the code contained 1 will be executed and the program will then go to exit. If condition 1 is false and condition 2 is true, the code contained 2 will be executed and the program will then go to exit. Finally, if condition 1 is false and condition 2 is also false, the code contained 3 will be executed by default and the program will then go to exit.
You can add as many "else if (condition X)" as you want in the following order:
if (condition 1)
{
//code
}
else if (condition 2)
{
//code
}
else if (condition 3)
{
//code
}
//etc...
else //Optional
{
//code
}
The While statement
while (condition)
{
//code content
} //exit
Functioning :
While is a loop that checks if the condition is true and then executes the code inside each turn. If the condition is false, the program will go to exit. This is the opposite of the do-while tool which executes and then checks.
The Do-while statement
do
{
//code content
} while (condition); //sortie
Functioning :
Do-While is a loop that executes the code inside and then checks if the condition is true at each turn. If the condition is false the program will go to the exit (after the ";"). This is the opposite of the While tool which checks and then executes.
And that's it, this first chapter on instructions is finished! In the next chapter, we'll see how to initialize them and what conditions you can use.
@ Soon !
Jay et the Ludovyc.

English
Français