0% found this document useful (0 votes)
5 views82 pages

Cours 4

Uploaded by

kaouthar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views82 pages

Cours 4

Uploaded by

kaouthar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 82

AMAR TELIDJI UNIVERSITY LAGHOUAT

FACULTY of TECHNOLOGY
Technical Sciences Engineers Department

Computer Science 1
Lesson 04: Control Statements

Academic Year 2024/2025


Introduction
Algorithm add ;
Variables :
A,B,C : integer ;
Start
Input(A,B) ;
C ←A+B;
Output(C) ;
End.
These statements are executed one after the other in
order. This type of algorithms is called sequential

Computer Science 1 2023/2024 2 / 66


Introduction
In a sequential algorithm, all statements are executed
in order (step by step)
It is not the case of all problems
Problems often have multiple situations to consider,
and the algorithm should take all the possibilities
Examples :
calculate the maximum of two integers
resolve a second degree equation
calculate the sum 1 + 2 + 3 + ... + 100
calculate the term of a sequence
etc.
We need different statements to express these
situations : Control statements
Different types : conditional, loop, goto, ...
Computer Science 1 2023/2024 3 / 66
Outlines
1 Conditional Statements
Conditional Flowchart
Conditional Pseudo-code
Conditions in C
2 Loop Statements
Pseudo-code of Loops
Loops Flowchart
Loops in C
3 Exercises

Computer Science 1 2023/2024 4 / 66


Conditional
Statements
Conditional Statements

Analysis of the problem of calculating the maximum of


two integers
Input data : two integers A and B
Output data : the biggest number between A and B
Processing : compare A and B
if A is the biggest number, the maximum is A,
otherwise, it is B
After the comparison, we have to choose between two
situations : we use the conditional statement

Computer Science 1 2023/2024 6 / 66


Conditional Statements

A condition is a boolean expression : takes only two


possible values true or false
If the condition is true, the algorithm executes the
statements, then follows up with the other
instructions of the algorithm
Otherwise, it executes another set of statements
then continue with the algorithm

Computer Science 1 2023/2024 7 / 66


Conditional
Flowchart
Condition Flowchart
Flowcharts of conditions use the diamant shape with two
outputs : one labelled with Yes, if the condition is true
and another output labelled with No for the other case

Computer Science 1 2023/2024 9 / 66


Example
Write a flowchart that calculates the maximum between
two integers A and B

Computer Science 1 2023/2024 10 / 66


Conditional
Pseudo-code
Conditional Pseudo-code

Four types of conditions


Simple
Complete
Nested
Multiple choices

Computer Science 1 2023/2024 12 / 66


Simple Conditions
Syntax of the simple condition :
If ( Condition )
Instruction 1 ;
Instruction 2 ;
..
.
EndIf ;

Computer Science 1 2023/2024 13 / 66


Simple Conditions
Syntax of the simple condition :
If ( Condition )
Instruction 1 ;
Instruction 2 ;
..
.
EndIf ;

Example 1
Write a pseudo-code that decides whether an integer is
positive

Computer Science 1 2023/2024 13 / 66


Simple Conditions

Algorithm positive ;
Variables :
A : integer ;
Start
Input (A) ;
If ( A > 0 )
Output(A, ’is positive’) ;
EndIf ;
End.

Computer Science 1 2023/2024 14 / 66


Simple Conditions

Example 2
Write a flowchart and the pseudo-code to read two
integers A and B then puts the biggest number in B and
the smallest one in A

Computer Science 1 2023/2024 15 / 66


Simple Conditions

Algorithm max ;
Variables :
A, B, C : integer ;
Start
Input (A,B) ;
If ( A > B )
C ← A;
A ← B;
B ← C;
EndIf ;
Output(A,B) ;
End.

Computer Science 1 2023/2024 16 / 66


Complete Conditions
Syntax of complete conditions
If ( Condition )
Instruction 1 ;
Instruction 2 ;
..
.
Else
Instruction 3 ;
Instruction 4 ;
..
.
EndIf ;

Computer Science 1 2023/2024 17 / 66


Complete Conditions

Example
Write a flowchart then a pseudo-code that calculate the
average of two semesters and then print out if the student
has succeeded or not

Computer Science 1 2023/2024 18 / 66


Algorithm admission ;
Variables :
N1, N2, Moy : real ;
Start
Input(N1,N2) ;
Moy ← (N1 + N2)/2 ;
If ( Moy >= 10 )
Output(’Success’) ;
Else
Output(’Failure’) ;
EndIf ;
End.

Computer Science 1 2023/2024 19 / 66


Examples of Algorithms With Conditions
Algorithm calcul ;
Variables Instruction A B C
A, B, C : integer ;
Start
A ← 3;
B ← 2;
C ← 1;
If ( A = 3 )
A ← 7;
B ← (A+C) * 3 ;
Else
A ← 2;
EndIf ;
End.

Computer Science 1 2023/2024 20 / 66


Examples of Algorithms With Conditions
Algorithm calcul ;
Variables Instruction A B C
A, B, C : integer ; 1 3
Start
1→ A ← 3 ;
B ← 2;
C ← 1;
If ( A = 3 )
A ← 7;
B ← (A+C) * 3 ;
Else
A ← 2;
EndIf ;
End.

Computer Science 1 2023/2024 20 / 66


Examples of Algorithms With Conditions
Algorithm calcul ;
Variables Instruction A B C
A, B, C : integer ; 1 3
Start 2 3 2
A ← 3;
2→ B ← 2 ;
C ← 1;
If ( A = 3 )
A ← 7;
B ← (A+C) * 3 ;
Else
A ← 2;
EndIf ;
End.

Computer Science 1 2023/2024 20 / 66


Examples of Algorithms With Conditions
Algorithm calcul ;
Variables Instruction A B C
A, B, C : integer ; 1 3
Start 2 3 2
A ← 3; 3 3 2 1
B ← 2;
3→ C ← 1 ;
If ( A = 3 )
A ← 7;
B ← (A+C) * 3 ;
Else
A ← 2;
EndIf ;
End.

Computer Science 1 2023/2024 20 / 66


Examples of Algorithms With Conditions
Algorithm calcul ;
Variables Instruction A B C
A, B, C : integer ; 1 3
Start 2 3 2
A ← 3; 3 3 2 1
B ← 2; 4 7 2 1
C ← 1;
If ( A = 3 )
4→ A ← 7 ;
B ← (A+C) * 3 ;
Else
A ← 2;
EndIf ;
End.

Computer Science 1 2023/2024 20 / 66


Examples of Algorithms With Conditions
Algorithm calcul ;
Variables Instruction A B C
A, B, C : integer ; 1 3
Start 2 3 2
A ← 3; 3 3 2 1
B ← 2; 4 7 2 1
C ← 1; 5 7 24 1
If ( A = 3 )
A ← 7;
5→ B ← (A+C) * 3 ;
Else
A ← 2;
EndIf ;
End.

Computer Science 1 2023/2024 20 / 66


Examples of Algorithms With Conditions
Algorithm calcul ;
Variables Instruction A B C
A, B, C : integer ; 1 3
Start 2 3 2
A ← 3; 3 3 2 1
B ← 2; 4 7 2 1
C ← 1; 5 7 24 1
If ( A = 3 ) 6 7 24 1
A ← 7;
B ← (A+C) * 3 ;
Else
6→ A ← 2 ;
EndIf ;
End.

Computer Science 1 2023/2024 20 / 66


Examples of Algorithms With Conditions
Algorithm calcul ;
Variables Instruction A B C
A, B, C : integer ; 1 3
Start 2 3 2
A ← 3; 3 3 2 1
B ← 2; 4 7 2 1
C ← 1; 5 7 24 1
If ( A = 3 ) 6 7 24 1
A ← 7; 7 7 24 1
B ← (A+C) * 3 ;
Else
A ← 2;
EndIf ;
7→End.

Computer Science 1 2023/2024 20 / 66


Examples of Algorithms With Conditions
Algorithm calcul ;
Variables
A, B, C : integer ;
Start
A ← 0;
B ← 4;
C ← 5;
If ( A = 3 )
A ← 7;
B ← (A+C) * 3 ;
Else
A ← 2;
EndIf ;
End.

Computer Science 1 2023/2024 21 / 66


Examples of Algorithms With Conditions
Algorithm calcul ;
Variables Instruction A B C
A, B, C : integer ;
Start
A ← 0;
B ← 4;
C ← 5;
If ( A = 3 )
A ← 7;
B ← (A+C) * 3 ;
Else
A ← 2;
EndIf ;
End.

Computer Science 1 2023/2024 21 / 66


Examples of Algorithms With Conditions
Algorithm calcul ;
Variables Instruction A B C
A, B, C : integer ; 1 0
Start
1→ A ← 0 ;
B ← 4;
C ← 5;
If ( A = 3 )
A ← 7;
B ← (A+C) * 3 ;
Else
A ← 2;
EndIf ;
End.

Computer Science 1 2023/2024 21 / 66


Examples of Algorithms With Conditions
Algorithm calcul ;
Variables Instruction A B C
A, B, C : integer ; 1 0
Start 2 0 4
A ← 0;
2→ B ← 4 ;
C ← 5;
If ( A = 3 )
A ← 7;
B ← (A+C) * 3 ;
Else
A ← 2;
EndIf ;
End.

Computer Science 1 2023/2024 21 / 66


Examples of Algorithms With Conditions
Algorithm calcul ;
Variables Instruction A B C
A, B, C : integer ; 1 0
Start 2 0 4
A ← 0; 3 0 4 5
B ← 4;
3→ C ← 5 ;
If ( A = 3 )
A ← 7;
B ← (A+C) * 3 ;
Else
A ← 2;
EndIf ;
End.

Computer Science 1 2023/2024 21 / 66


Examples of Algorithms With Conditions
Algorithm calcul ;
Variables Instruction A B C
A, B, C : integer ; 1 0
Start 2 0 4
A ← 0; 3 0 4 5
B ← 4; 4 0 4 5
C ← 5;
If ( A = 3 )
4→ A ← 7 ;
B ← (A+C) * 3 ;
Else
A ← 2;
EndIf ;
End.

Computer Science 1 2023/2024 21 / 66


Examples of Algorithms With Conditions
Algorithm calcul ;
Variables Instruction A B C
A, B, C : integer ; 1 0
Start 2 0 4
A ← 0; 3 0 4 5
B ← 4; 4 0 4 5
C ← 5; 5 0 4 5
If ( A = 3 )
A ← 7;
5→ B ← (A+C) * 3 ;
Else
A ← 2;
EndIf ;
End.

Computer Science 1 2023/2024 21 / 66


Examples of Algorithms With Conditions
Algorithm calcul ;
Variables Instruction A B C
A, B, C : integer ; 1 0
Start 2 0 4
A ← 0; 3 0 4 5
B ← 4; 4 0 4 5
C ← 5; 5 0 4 5
If ( A = 3 ) 6 2 4 5
A ← 7;
B ← (A+C) * 3 ;
Else
6→ A ← 2 ;
EndIf ;
End.

Computer Science 1 2023/2024 21 / 66


Examples of Algorithms With Conditions
Algorithm calcul ;
Variables Instruction A B C
A, B, C : integer ; 1 0
Start 2 0 4
A ← 0; 3 0 4 5
B ← 4; 4 0 4 5
C ← 5; 5 0 4 5
If ( A = 3 ) 6 2 4 5
A ← 7; 7 2 4 5
B ← (A+C) * 3 ;
Else
A ← 2;
EndIf ;
7→End.

Computer Science 1 2023/2024 21 / 66


What does the following flowchart do ? Deduce the
corresponding pseudo-code.

Computer Science 1 2023/2024 22 / 66


Algorithm multiples ;
Variables :
N, R : integer ;
Start
Input(N) ;
R ← N mod 3 ;
If ( R = 0 )
Output(N, ’is a multiple of 3’) ;
Else
Output(N, ’is not a multiple of 3’) ;
EndIf ;
End.

Computer Science 1 2023/2024 23 / 66


Nested Conditions
Conditions inside other conditions. The inner statements
should verify all the conditions down the path from the
first one

Computer Science 1 2023/2024 24 / 66


Nested Conditions
If ( condition 1 )
< Bloc 1 >
Else
If ( condition 2 )
< Bloc 2 >
Else
If ( condition 3 )
< Bloc 3 >
Else
< Bloc 4 >
EndIf ;
EndIf ;
EndIf ;

Computer Science 1 2023/2024 25 / 66


Exercises
Write the algorithms (flowcharts and pseudo-codes) of the
following problems :
1 Calculate the magnitude of an integer X
2 Determine whether an integer is odd or even
3 Resolve a second degree equation AX 2 + BX + C = 0

Computer Science 1 2023/2024 26 / 66


Conditions in C
Simple Conditions

if (condition)
Block of statements ;
If the block of statements contains more than one
instruction, we use the braces ({ and })to limit the block
Example
Write a pseudo-code then a C program to calculate the
magnitude of an integer

Computer Science 1 2023/2024 28 / 66


Simple Conditions
Algorithm C Language

Algorithm v_absolue ;
Variables : #include<stdio.h>
A : integer ; int main(){
Start int A;
Input (A) ; scanf("%d", &A);
If ( A < 0 ) if (A<0)
A ← -A ; A=-A;
EndIf ; printf("%d", A);
Output(A) ; return 0;
End. }

Computer Science 1 2023/2024 29 / 66


Simple Conditions

Example 2
Write a C program that reads two integers A and B then
puts the biggest number in B and the smallest one in A

Computer Science 1 2023/2024 30 / 66


Algorithm C Language

Algorithm MaxA ;
Variables : #include<stdio.h>
A, B, C : integer ; int main(){
Start int a, b, c;
Input (A,B) ; scanf("%d%d", &a, &b);
If ( A > B ) if (a<b)
C ← A; {c=a;
A ← B; a=b;
B ← C; b=c;}
EndIf ; printf("%d %d", a,b);
Output(A,B) ; return 0;
End. }

Computer Science 1 2023/2024 31 / 66


Complete Conditions

If ( condition )
Instructions
Else
Instructions
EndIf ;

if ( condition )
Instruction1 ;
else
Instruction2 ;

Computer Science 1 2023/2024 32 / 66


Complete Conditions

Again, if the block contains more than one instruction,


these instructions should be limited by the braces

Example
Write a C program to calculate the average of two
semesters and then print out if the student has succeeded
or not

Computer Science 1 2023/2024 33 / 66


Algorithm C Language

Algorithm moyenne ;
Variables : #include<stdio.h>
N1, N2, Moy : real ; int main(){
Start float N1, N2, Moy;
Input(N1,N2) ; scanf("%f%f", &N1, &N2);
Moy ← (N1 + N2)/2 ; Moy = (N1+N2)/2;
If ( Moy >= 10 ) if (Moy>=10)
Output(’Success’) ; printf("Success");
Else else
Output(’Failure’) ; printf("Failure");
EndIf ; return 0;
End. }

Computer Science 1 2023/2024 34 / 66


Nested Conditions
if(condition 1){
...
}
else
if(condition 2){
...
}
else
if(condition 3){
...
}
else {
...
}

Computer Science 1 2023/2024 35 / 66


Exercises

Write the algorithms (flowchart and pseudo-code) then a C program


to :
1 resolve a second degree equation,
2 determine whether a triangle is isosceles, equilateral or general
3 calculate tomorrow’s date from today’s

Computer Science 1 2023/2024 36 / 66


Loop Statements
Introduction

Write an algorithm to print out numbers from 1 to N


Let’s consider N = 4
Algorithm print ;
Start
Output (’1’) ;
Output (’2’) ;
Output (’3’) ;
Output (’4’) ;
End.

Computer Science 1 2023/2024 38 / 66


Introduction

What if N = 100 ?
Problem
Redundancy and too long algorithms

Solution
Use loops, called also repetitive or iterative statements

Computer Science 1 2023/2024 39 / 66


Introduction

Loops are statements used to repeat a set of statements a


predefined number of times

One passage in a loop is called iteration


In algorithms, we distinguish between three kind of
loops
1 for
2 while
3 repeat

Computer Science 1 2023/2024 40 / 66


for Loop
for Loop

Repeat a block of statements a number of times,


fixed and known before the processing
Main characteristic of this loop is that we know the
number of times we execute the block of statements

for (count ← initial to final)


statements ;
..
.
Endfor ;

Computer Science 1 2023/2024 42 / 66


for Loop

The variable count is called the loop counter


initial is the first value assigned to the counter during
the first iteration
final is the last value assigned to the counter in the
last iteration
A each instant, we know the number of already
executed iterations and the number of the remaining
iterations

Computer Science 1 2023/2024 43 / 66


Example 01
Write a pseudo-code to print out the numbers 1, 2, . . . , N
using the for loop
Algorithm print ;
Variables :
i,N : integer ;
Start
Input(N) ;
for ( i ← 1 to N )
Output (i) ;
Endfor ;
End.

Computer Science 1 2023/2024 44 / 66


Example 02
Write a pseudo-code to calculate the sum 1 + 2 + . . . + N
using the for loop
Algorithm sum ;
Variables :
i,N,S : integer ;
Start
Input(N) ;
S ← 0;
for ( i ← 1 to N )
S ← S + i;
Endfor ;
Output (S) ;
End.

Computer Science 1 2023/2024 45 / 66


Example 03
Write a pseudo-code to calculate the power X n using the
for loop
Algorithm power ;
Variables :
i,N : integer ;
P,X : real ;
Start
Input(X,N) ;
P ← 1;
for ( i ← 1 to N )
P ← P * X;
Endfor ;
Output (P) ;
End.
Computer Science 1 2023/2024 46 / 66
Example 04
Write a pseudo-code to calculate the factorial of an
integer using the for loop
Algorithm factorial ;
Variables :
i,N,f : integer ;
Start
Input(N) ;
f ← 1;
for ( i ← 1 to N )
f ← f * i;
Endfor ;
Output (f) ;
End.

Computer Science 1 2023/2024 47 / 66


Loops Flowchart
Loops Flowchart

No form is dedicated to loops in flowcharts


Use the diamant shape (conditions) to write loops
The expression and the position of the condition
make the difference between the different loops

Computer Science 1 2023/2024 49 / 66


for Flowchart

Add a variable counter to calculate the number of


iterations
Initialise the counter before the condition
The expression of the condition is while the
counter is less or equal to the maximum number
of iterations
Increment the counter inside the loop (after the
statements)
Add an arrow from the statements to the condition

Computer Science 1 2023/2024 50 / 66


Computer Science 1 2023/2024 51 / 66
The following flowchart prints out the numbers 1, 2, ..., N

Computer Science 1 2023/2024 52 / 66


The following flowchart prints out the sum
S = 1 + 2 + ... + N

Computer Science 1 2023/2024 53 / 66


Modification to calculate the product P = 1 ∗ 2 ∗ ... ∗ N ?

Computer Science 1 2023/2024 54 / 66


Loops in C
Loops in C

Three types of loops :


1 for
2 while
3 do... while

Computer Science 1 2023/2024 56 / 66


The for Loop

for (expr1 ; expr2 ; expr3){


<Instructions>
}

expr1 is evaluated once at the beginning of the loop,


it initialises the variables of the loop
expr2 is evaluated after each iteration of the loop, it
is the condition to continue the repetition
expr3 is evaluated after each iteration too, to
increment or change the value of the counter

Computer Science 1 2023/2024 57 / 66


The for Loop

Example
Write a C program to calculate the power x n (x a none
null real and n > 0 a none null integer)

Computer Science 1 2023/2024 58 / 66


Algorithm power ;
Variables : #include<stdio.h>
N,i : integer ; int main(){
X,P : real ; float x, P; int i, n;
Start scanf("%f%d", &x, &n);
Input(X,N) ; P=1;
P ← 1; for (i=1;i<=n;i++)
for (i ← 1 to N) P=P*x;
P ← P * X; printf("%f", P);
Endfor ; return 0;
Output (P) ; }
End.

Computer Science 1 2023/2024 59 / 66


Nested Loops

Loops’ statements could be themselves other loops :


nested loops
The first iteration of the external loop starts the
internal loop, which is completely executed before
going to the next iteration of the external loop
The same thing is done with the second iteration
The process goes on until the external loop runs out
of iterations

Computer Science 1 2023/2024 60 / 66


Nested Loops

#include <stdio.h>
int main () {
int i, j;
for (i=0; i<4; i++) {
printf("\n Loop i, i is %d\n",i);
for (j=3; j>0; j--)
printf(" Loop j, j is %d\n",j);
}
return 0;
}

Computer Science 1 2023/2024 61 / 66


Nested Loops

#include <stdio.h>
int main () {
int i, j;
for (i=0; i<=5; i++){
for (j=0; j<=i; j++)
{printf("O");}
printf("K\n");
}
return 0;
}

Computer Science 1 2023/2024 62 / 66


break and continue

break is used inside a loop to stop it and go to the


statement after the loop
In the case on nested loops, break stops the most
internal loop (the one containing the break
statement)
continue is used to stop the current iteration and go
to the next one

Computer Science 1 2023/2024 63 / 66


Exercise 1
1 Write a C program to print out the divisors of an
integer
2 Modify the code to calculate the sum of these divisors
3 Modify the program to verify whether a number is
afin perfect or not
A number (integer) is said to be perfect if it equals the sum of its divisors
without the number itself. For example, 6 is a perfect number because 6 =
1+2+3

Computer Science 1 2023/2024 64 / 66


Exercise 2
1 Modify the code of the first exercise to calculate the
number of divisors of an integer
2 Deduce the code that verifies whether an integer is a
prime number or not

Computer Science 1 2023/2024 65 / 66


Computer Science 1 2023/2024 66 / 66

You might also like