Docsity

Programming - Unit 3a: Control flow, if and switch (slides), Diapositivas de Programación Informática

Vista previa parcial del texto

¡Descarga Programming - Unit 3a: Control flow, if and switch (slides) y más Diapositivas en PDF de Programación Informática solo en Docsity!

U N I T 3.

C O N T R O L F L O W

P A R T I

I F + S W I T C H

P R O G R A M M I N G Y E A R 2 0 2 0 - 2 0 2 1

CONTENTS

1. Introduction

2. Conditional control flow structures

1. if else

2. switch

3. Iterative control flow structures (loops)

1. for

2. while

4. Control structure nesting

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

[Böhm-Jacopini theorem]

STRUCTURED PROGRAMMING

  • Programming paradigm
    • Best practice for developing good programs
      • Good = easy to develop and to maintain (solve errors, extend functionality)
  • Basic principles of structured programming
    • Single entry and exit end point (start /end)
    • Only sequential, conditional and iterative control flow structures allowed
  • Never "jump" from one point to the program to another (“go to”)

CONDITIONAL STRUCTURE if - else

  • Logic expression is evaluated
    • If the expression is true block of code 1 is run
    • If the expression is false block of code 2 is run
    • After either branch has been executed, control returns to the point after the if

if Logical expression instruction_block_1;

else instruction_block_2; end

Syntax

Example

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

Flow diagram

CONDITIONAL STRUCTURE IF

  • Simplest version
  • If the expression is true the block is run
  • If it is not, nothing happens

True

False

Instruction

block

if Logical expression Logical expression

instruction_block; end

if age < 18 printf("KID"); ticket_discount = 20; end

Syntax

Example

Flow diagram

NESTED IF STRUCTURES

  • Nest = one if inside another if
  • To define different alternative and mutually exclusive paths
  • If all logical expressions are false the last block is run

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

  • Develop a program that queries the user for his or her mark in an exam

and displays the corresponding grade:

  • Sobresaliente: 9 to 10
  • Notable: 7 to 9
  • Bien: 5 to 7
  • Insuficiente: less than 5
  • Extend the program to print "Error" when the mark is not between 0 and

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

The elseif clause

  • Multiple alternative choices.
  • Mutually exclusive.
  • If all the conditions are false, the final

else block is executed

Nested if else

15

if

elseif

elseif

else

end

2. C O N D I T I O N A L

I N S T R U C T I O N S

2. 2. S W I T C H

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

  • selector:
    • Must be a variable or expression of any of the following datatypes: integer, float, double, char, string, logical - If logical use if!
  • case clause
    • Each “case” clause is followed by a constant expression (or more than one)
      • Examples 12, max, max+
  • otherwise (default)
    • If the selector value doesn’t match the value of any of the case clauses, the default block (if existing) will be run

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