0% found this document useful (0 votes)
4 views

TP n°5

Uploaded by

foufita24
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)
4 views

TP n°5

Uploaded by

foufita24
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/ 31

People's Democratic Republic of Algeria

Ministry of Higher Education and Scientific Research


Mohamed Chérif Messaâdia University –Souk Ahras
Faculty of Science and Technology

TP N° 05: Control Statements


(For and While Loops, If and
Switch Statements)
[email protected]

College year: 2024-2025

1
Introduction
• The control structures in Matlab are very similar to those in other
programming languages such as C.
• They are useful for writing Matlab programs.

2
Conditional structure
Where:
• Condition : is a logical expression whose result can be true or false.
• Instructions : is a sequence of instructions.
• If the result of the evaluation of the condition is true, Instructions is
executed, then the instruction following the keyword end .
• If the result of the evaluation of the condition is false, we go directly to the
instruction following the end keyword.
3
Conditional structure
• A conditional structure allows you to execute a sequence of instructions only if the
condition is true.
• Different forms of conditional structures exist in Matlab:
• 1. Simple form:
• Syntax:
• if condition
Instructions
• end
4
Conditional structure
Example:
Nbr = input(‘Give a number ')
if (Nbr==9)
disp(‘Correct anserwer')
end.

5
Conditional structure
• 2. Alternative form:
• Syntax:
• if condition
Bloc of Instructions_1
else
Bloc of Instructions_2
• end
6
Conditional structure
Where:
• Condition: is a logical expression whose result can be true or false.
• Bloc Instructions_1 and Bloc Instructions_2 are two sequences of instructions.
• If the result of the evaluation of condition is true, Bloc Instructions_1 is
executed, then the instruction following the end keyword.
• If the result of the evaluation of condition is false, Bloc Instructions_2 is
executed, then the instruction following the end keyword.

7
Conditional structure
Example:
• if (x ~= 0)
y = 1/x ;
disp (y)
else
disp(‘Error division by zero');
end
8
Conditional structure
3. Nested form:
Syntax:
if condition_1
Instructions_1
elseif condition_2
Instructions_2
elseif condition_3
Instructions_3
...
else
Instructions_n
end
9
Conditional structure
Where:
• condition_i : is a logical expression whose result can be true or false.
Instructions _i : is a sequence of instructions.
• If the result of evaluating condition_i is true, Instructions _i is executed,
then the instruction following the keyword end.
• If none of the expressions condition_1, condition_2,..., is true, the
instruction Instructions_n (default sequence of instructions) is executed,
then the instruction following the keyword end.
10
Conditional structure
• Example:
• age=input('Give your age: ');
• if (age < 2)
• disp(‘You are a baby')
• elseif (age < 13)
• disp(‘You are a child')
• elseif (age < 18)
• disp (‘You are a teenagert')
• elseif (age < 60)
• disp (‘You are an adult' )
• else
• disp (‘You are an old man')
• end
11
Conditional structure
• Exercice 01:
• Write a matlab script that ask user to give two integers, than calculate their
multiplication, and display if the result is negative or positive
• Exercice 02:
• Write a script matlab that allows to find the root of the equation 2nd degree:
𝑎𝑥 2 + 𝑏𝑥 + 𝑐 = 0

12
Solution
• Exercice 01:
x= input('give the number x ');
y= input('give the number y ');
p=x*y;
if p<0
disp('the product is negative')
else
disp ('the product is pisitive')
end
13
Solution
• Exercice 02:
a= input('give the number a ');
b=input('give the number b');
c=input('give the number c');
d=b^2-4*a*c;
if d>0
x1= -b-sqrt(d)/2*a
x2= -b+sqrt(d)/2*a
elseif (d==0)
x= -b/2*a
else
disp('There is no solution')
end
14
Multiple choices – switch instruction
• Syntax:
switch var
case const_ 1,
instructions_1
case const_2,
instructions_2
...
otherwise
Instructions_n
15
end
Multiple choices – switch instruction
• Such that:
• var is a numeric variable or a string variable.
• const_i is a numeric constant or string constants of the same type as var.
• Instructions_i is a sequence of instructions.
• If the variable var is equal to the constant const_i , the corresponding
sequence of instructions is executed (i.e. Instructions_i , then the
instruction following the keyword end.
16
Multiple choices – switch instruction
• If var is not equal to any of the constants const_1, const_2,..., we execute
the instruction Instructions_n (default sequence of instructions), then the
instruction following the keyword end.
• If there is no default case, and var is not equal to any of the const_i
constants, then we continue at the first statement following the end keyword.

17
Multiple choices – switch instruction
Example:
x = input (‘Give a number : ') ;
switch x
case 0
disp('x = 0 ')
case 10
disp('x = 10 ')
case 100
disp('x = 100 ')
Otherwise
disp('x is not 0 or 10 or 100 ')
end
18
Multiple choices – switch instruction
Exercice:
Write a matlab script that allows to Grab the day number in a week (1-7) and
display the day name using switch case.

19
Solution

20
The loops
• Loops allow you to execute a sequence of instructions repeatedly.
1 The for loop: if you know the number of iteration from min value to max
value you can use the for loop
Syntax:
for indice = min: max
Instructions
end
21
The loops
• index: is a variable called the loop index.
• min (lower bound) and max (upper bound): are two integer constants.
• Instructions: is the sequence of instructions to repeat ( We are talking about the
body of the loop ).
• If min ≤ max , Instructions is executed ( supinf+1 ) times, for the values of the
index variable equal to inf, inf+1,...,sup , then it goes to the instruction immediately
following the end of loop instruction (end).
• If min > max , it goes directly to the instruction immediately following the end of
loop instruction (end).

22
The loops
• Example:
• Write a matlab script that calculate the sum from 1 to 9.
sum = 0;
for i = 1:9
sum = sum+i;
end;
disp(sum)
23
The loops
• Exercice:
• Write a matlab script that calculate the product of numbers from 1 to a
number given by the user.

24
Solution
• x = input ('give a number');
• p=1;
• for i=0:x
• p=p*i;
• end
• disp('the product is %d',p)

25
The loops
• 2. While loop:
• This loop repeat sequance of instructions until the condition is not verified.
• Syntax:
while condition
Instructions
end

26
The loops
• condition: is a logical expression (having two values true or false).
• Instructions: is a sequence of instructions that repeats as long as condition
has the value true.
• When the value of condition becomes false, we go to the instruction that
immediately follows the end of loop instruction (end).
• condition is generally the result of a test (for example i < 10) or the result
of a logical function (for example ~empty(x) ).

27
The loops
• Example:
• Write a matlab script that calculate the sum from 1 to 9 using while loop.

28
Solution
s=0;
i=1;
while i<=9
s=s+i;
i=i+1;
end

29
The loops
Exercice:
• Transform the following code using while loop:
n=input(‘Give a number :') ;
fact = 1;
for k = 1:n
fact = fact*k;
end
disp(fact)
30
Solution
n=input(‘Give a number :') ;
fact = 1;
k=1;
While k<=n
fact = fact*k;
k=k+1;
end
disp(fact)

31

You might also like