Session 2
Session 2
and Algorithms
LANGUAGE INTRO I
C PROGRAMMING LANGUAGE INTRO I
OBJECTIVES
Understand the structure of a C program
Examples:
a. Stdio. h – a file that contains functions that enable
inputting and outputting of data through the console of the
running application.
b. Stdlib.h – a file that contains functions for typical C
language functions used for memory allocation and
management.
Programs run from the first statement of the body till the last
NB: Variables can be declared after the main function
(discussed later)
Call functions
If … statement:
if ( condition) {
// BLOCK1 of statements
}
// BLOCK2 of statements
NB:
the word ‘else’ exists in C but the word ‘then’ does not exist
in the language like in pseudo code.
The curly brackets indicate the begin ‘{’ and end ‘}‘ of a
block of statements
If … else statement:
if ( condition) {
// BLOCK1 of statements
}
else {
// BLOCK2 of statements
}
// BLOCK3 of statements
// BLOCK5 of statements
Switch statement:
switch ( n) {
case 1 :
// BLOCK1 of statements
break;
case 2 :
// BLOCK2 of statements
break;
case 3 :
// BLOCK3 of statements
break;
case 4 :
// BLOCK4 of statements
break;
default :
// BLOCK5 of statements
}
// BLOCK6 of statements
do …while statement:
do {
// BLOCK1 of statements
}
while ( condition);
// BLOCK2 of statements
NB:
repeats Block1 until the condition is false
while statement:
while ( condition) {
// BLOCK1 of statements
}
// BLOCK2 of statements
For statement:
for( <initial value> ; <condition> ; <increment>) {
// BLOCK1 of statements
}
// BLOCK2 of statements
NB:
• Not always increasing, as regards to the increment part.
• Before Block1 is executed the, the condition must be verified.
Break statement:
Already covered in the switch statement.
Could be applied to the loops.
Enables us to escape a loop before it continues to the next
iteration. The takes us to the next block after the loop.
Example:
while ( condition) {
// BLOCK1 of statements
if (a > b) {
break;
}
}
// BLOCK2 of statements
Continue statement:
Enables us to go to the next iteration/ round of the
loop prematurely
Example:
while ( condition) {
// BLOCK1 of statements
if (a > b) {
continue;
}
// BLOCK2 of statements
}
// BLOCK3 of statements
Goto statement:
Enables us to go to a specified part of the program
Example:
while ( condition) {
// BLOCK1 of statements
if (a == b) {
goto MyDestination;
}
// BLOCK2 of statements
}
// BLOCK3 of statements
MyDestination :
// BLOCK4 of statements
Relational operators:
< : less than
<= : less than or equal to
> : greater than
>= : greater than or equal to
== : equal to
!= : different to
C PROGRAMMING LANGUAGE INTRO I
OPERATORS IN C
Logical operators:
(a > b) && ( c < d) : operator AND
Cast operator:
Used when the programmer wants to force the
conversion of a given expression to a type of
his/her choice.
Declaration of a struct.
struct myDate {
int day ;
int month;
int year ;
}