













Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity
Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium
Esta página no es visible en la vista previa
¡No te pierdas las partes importantes!














P R O G R A M M I N G Y E A R 2 0 2 0 - 2 0 2 1
CONTENTS
Sequential
Instruction block
Instruction block
Conditional if-else, switch
Iterative (loops ) for, while
Any (any!) algorithm can be implemented using
only these three control flow structures
Instruction block True False
Logical expression
Logical Instruction block Instruction block expression
True
False
STRUCTURED PROGRAMMING
CONDITIONAL STRUCTURE if - else
if Logical expression instruction_block_1;
else instruction_block_2; end
if a > b printf("A larger than B"); else printf("A smaller than or equal to B"); end
Logical expression
Instruction block 1
True False
Instruction block 2
CONDITIONAL STRUCTURE IF
True
False
instruction_block; end
if age < 18 printf("KID"); ticket_discount = 20; end
Flow diagram
NESTED IF STRUCTURES
True Instruction Block 1 False
Logical expression
Logical expression
Instruction Block 2.
Instruction Block 2.
True
False
if (logical_expression_1) instruction_block_1; else if(logical_expression_2) instruction_block_2.1; else instruction_block_2.2; end end
EXAMPLE
and displays the corresponding grade:
10
ELSEIF
mark=input('Enter your mark:');
if ( mark < 5 )
fprintf("Fail: Insuficiente\n");
elseif ( mark <= 7 )
fprintf("Pass: Bien \n");
elseif ( mark <= 9 )
fprintf("Pass: Notable\n");
else
fprintf("Pass with honours: Sobresaliente\n");
end
SELECTION STATEMENTS
Nested if else
15
if
elseif
elseif
else
end
17
numSides = input ('Insert number of sides: ');
switch (numSides) % numSides is the selector for the switch case {0,1,2} fprintf('not a polygon \n'); case 3 fprintf('triangle\n'); case 4 fprintf('rectangle\n'); case 5 fprintf('pentagon\n'); case 6 fprintf(‘hexagon\n'); otherwise fprintf('More than 6 sides\n'); end
polygon name according to the number of sides
SWITCH
SWITCH - SYNTAX
SWITCH
22
c = input(‘Enter a character: ','s');
switch c case {'a','e','i','o','u'} fprintf('It’s a vowel’\n'); otherwise fprintf('It’s not a lowercase vowel \n'); end
The switch_expression can group several values, {}
In MatLab the switch_expresion can’t be a range of float values – use if instead