Docsity

Programming - Unit 3b: Control flow, loops (slides), Diapositivas de Programación Informática

Vista previa parcial del texto

¡Descarga Programming - Unit 3b: Control flow, loops (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

L O O P S

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

for i= 2:1:

fprintf("%i\n", i);

end

  • Code to display all integer numbers from 2 to 20

i<=

True False

printf("%i\n", i);

i=

i= i+

FOR LOOP: EXAMPLE 1

for i= 1:3:

fprintf("%i\n", i);

end

  • Code to display all multiples of 3 1 to 20

i<=

True False

printf("%i\n", i);

i=

i= i+

FOR LOOP: EXAMPLE 2

FOR LOOP: SYNTAX

  • Vales can be specified as

initial_value: step :endl_value

  • Initialization=initial_value
  • Update : previous value + step
  • Control : iterator< =end_value

list of values (array)

  • initialzation = first value in the list
  • Update: next value in the list
  • control: last value of the list 7

for iterator = values

[statement]

end

Control expression

True

False

statements

initialization

update

% version 1: step explicit

for i= 2:1:

fprintf("%i\n", i);

end

% version 2: step omitted

for i= 2:20 % step? - > default value 1

fprintf("%i\n", i);

end

  • If step is 1 , the value can be omitted
  • Code to display all integer numbers from 2 to 20

FOR LOOP: EXAMPLE 1 UPDATED

FOR LOOP: EXAMPLE 4

  • Step can be negative
    • In this case the condition is

iterator >= end_value

for i=20:-2:

fprintf("%i\n", i);

end

fprintf('end\n');

FOR LOOP

  • The range needs to be determined before the loop is run for the first time
  • Which of the following can be coded with a for loop?
    • Prompt the user for an integer number n and print “Hello world!” n times.
    • Ask the user for two values and display all integers between them
    • Extend the example so that only multiples of a given number (also entered
by the user) are displayed
  • Print “Hello world” and ask the user if he/she want’s to see it again, keep
asking until answer is no

11

3. 2. W H I L E

PROGRAM THAT PROMPTS FOR A PASSWORD UNTIL CORRECT

% set password
password = 1234;
number = 0; % variable to store number entered
while (number ~= password)
number = input('Enter pin: ');
if (number~= password)
disp("incorrect, re-enter");
end
end
disp("Correct, welcome");

WHILE USE

  • Can be used when the number of times the action has to be repeated is

not know at the time of writing the code

  • i.e. depends on user input
  • Beware of infinite loops
  • check that the condition will be false under some situations

17

% example infinite loop

a=45;

while (a<100)

fprintf('aaaaaaaa\n');

a = a -1;

end

WHILE - EXAMPLE

  • What does this code do?
  • What is the value of n when the loop ends?

n = 32;

while n>

fprintf('%i ',n);

n = n / 2;

end

Solution: It prints the sequence 32 16 8

PRACTICE WITH LOOPS: WHILE OR FOR?

  • Add up all integers from 1 to 10
  • Read a number and add up all integers from 1 to that number
  • Add up all numbers entered by the user until user enters a 0

PASSWORD WITH LIMITED ATTEMPTS: POST CHECK

password = 1234; % set password

attempts=0; %initialize attempts to 0

number = 0; % initialize number

while ((number ~= password )&&( attempts < 3))

number = input('Enter pin: ');

attempts=attempts+1;

if (number~= password)

disp('incorrect, re-enter');

end

end

if (number == password)

disp('Welcome');

else

disp('Account locked!!');

end

Post-check

The while block ends when the password is

found or when the maximum number of

attempts limit is reached.

You need to check which one is true