Structured Programming – SWE
CHAPTER 4: C VARIABLES
1-Definition
A variable is a named memory location used to store data that can be modified during
program execution. You can think of a variable as a named box where you keep a value
that can be used later.
In C, variables must have a specific type, which tells the program what kind of data the
variable can store.
int - stores whole numbers (integers), such as 123 or -123
float - stores numbers with decimals, such as 19.99 or -19.99
char - stores a single character, such as 'a' or 'B'. Characters are surrounded
by single quotes.
Data Types can be
Primitive types: int, float, double, char
Derived types: arrays, pointers
User-defined types: structures, unions
2- Declaring (Creating) Variables
To create a variable, you must specify the type and give the variable a name. You can
also assign a value at the same time:
The synthax to create a variable is type variableName = value;
Where :
type is one of the C data types (such as int),
variableName is the name you choose (for example x or myNum).
The equal sign = is used to assign a value to the variable.
So, to create a variable that should store a number, look at the following example:
int myNum = 15;
NB : You can also declare a variable first and assign a value later:
Mr TIOSSOCK NBONOU
Structured Programming – SWE
3- Declaring (Creating) Variables
All C variables must be identified with unique names. These unique names are
called identifiers. Identifiers can be short names (like x and y) or more descriptive names
(age, sum, totalVolume).
Note: It is recommended to use descriptive names in order to create understandable and
maintainable code.
The general rules for naming variables are:
Names can contain letters, digits and underscores
Names must begin with a letter or an underscore (_)
Names are case-sensitive (myVar and myvar are different variables)
Names cannot contain whitespaces or special characters like !, #, %, etc.
Reserved words (such as int) cannot be used as names
4- Scope of a variable
Scope refers to the region of a program where a variable, function, or identifier is
visible and can be accessed. So a variable can be
a- Local
The variable is accessible only inside the block or function where it is declared. Blocks
are defined by { }.
b- Global Scope (Global variable)
The variable is declared outside all functions. Accessible from any function in the
program.
Mr TIOSSOCK NBONOU
Structured Programming – SWE
c- A static variable
is a variable that:
Is initialized only once
Preserves its value between multiple function calls
Exists for the entire lifetime of the program
A static variable can be Local or Global:
NB: Lifetime is not the same as scope: scope defines where a variable can be accessed,
while lifetime defines how long the variable exists in memory.
Mr TIOSSOCK NBONOU
Structured Programming – SWE
CHAPTER 5: LOOPING
1-Definition
A loop is a control structure that repeatedly executes a block of code as long as a
condition remains true.
In structured programming, loops:
Replace unstructured repetition (goto)
goto is a control-flow statement that causes the program to jump directly to a labeled
statement in the same function.
Express iteration clearly and safely
Are essential for processing collections, streams, and files
Mr TIOSSOCK NBONOU
Structured Programming – SWE
2. Types of Loops
2.1 For Loop
For Lopp is also called Count-Controlled Loop.
Syntax
Execution Order of the for loop
1. Initialization (once)
2. Condition check
3. Loop body
4. Update
5. Repeat from step 2
Example
We use a for loop :
When the number of iterations is known
Index-based array traversal
Mr TIOSSOCK NBONOU
Structured Programming – SWE
Software Engineering Perspective
Most readable for counting
Encourages loop invariants
Easy to analyze for complexity
2.2- while Loop — Condition-Controlled Loop
The while loop repeats a block of code as long as a specified condition is true:
Synthax:
Example: Write a c program to count until 100 using the while loop.
Characteristics
Condition is checked before each iteration
May execute zero times
When to Use
When the number of iterations is unknown
Input validation
Event-driven logic
Mr TIOSSOCK NBONOU
Structured Programming – SWE
2.3 do–while Loop — Post-Test Loop
The do/while loop is a variant of the while loop. This loop will execute the code block once,
before checking if the condition is true, then it will repeat the loop as long as the condition is true.
Synthax
Example: Write a c program to count until 100 using the while loop.
Characteristics
Executes at least once
Condition checked after loop body
Use Case
Menu-driven programs
User input validation
Mr TIOSSOCK NBONOU