Day 2 review What is script file What

  • Slides: 27
Download presentation
Day 2 review • What is script file. • What is a “for” loop.

Day 2 review • What is script file. • What is a “for” loop. • Let's fire up Matlab.

Day 3 plan • Logic • Let's fire up Matlab.

Day 3 plan • Logic • Let's fire up Matlab.

LOGIC AND: OR: Complement: Exclusive OR: & | ~ xor p. 8

LOGIC AND: OR: Complement: Exclusive OR: & | ~ xor p. 8

LOGIC p. 8 What is TRUE in Matlab? NON ZERO ELEMENTS. TRY: a=[0 1

LOGIC p. 8 What is TRUE in Matlab? NON ZERO ELEMENTS. TRY: a=[0 1 1 0 1]; b=[1 1 0 0 234009]; c = a&b d = a|b e = ~a f = xor(a, b)

LOGIC p. 8 Question (without typing): Teenage. Crushlove = xor(Mylove, Yourlove); Whose love is

LOGIC p. 8 Question (without typing): Teenage. Crushlove = xor(Mylove, Yourlove); Whose love is true?

LOGIC p. 8 BRUSH UP ON YOUR LOGIC!! --> YOU WILL MAKE LOCIGAL MISTAKES

LOGIC p. 8 BRUSH UP ON YOUR LOGIC!! --> YOU WILL MAKE LOCIGAL MISTAKES and you'll think your computer has gone crazy. … humbling experience -->TEST AND RETEST LOGICAL STATEMENTS. -->DO NOT MAKE CONVOLUTATED logical statements in one swoop: such as “if (this is true) or (that is wrong while x is also true) but not y”.

IF IF something is true do {list of commands} Else { list of commands

IF IF something is true do {list of commands} Else { list of commands 2} end

IF syntax if (conditional statement) … elseif (conditional statement) … AL N O I

IF syntax if (conditional statement) … elseif (conditional statement) … AL N O I T ELA R e v a ts h n e m e t else l sta a n o i t i ual q e t Cond rs p. 8: o to, n l o t a a u r q e … Op an, e h t s s n, le a h t r e arg L end to…

IF syntax So… if (Mylove) Mylove=0; else Yourlove=0; end; Whose love is true?

IF syntax So… if (Mylove) Mylove=0; else Yourlove=0; end; Whose love is true?

IF syntax So… if (Mylove == true) Mylove=0; else yourlove=0; end; %help true %

IF syntax So… if (Mylove == true) Mylove=0; else yourlove=0; end; %help true % == is not =

IF exercise Ask user for password. If password is less than 6 characters long,

IF exercise Ask user for password. If password is less than 6 characters long, return "invalid password"

IF exercise Algorithm steps: 1. ask for user input (password) 2. check size of

IF exercise Algorithm steps: 1. ask for user input (password) 2. check size of password 3. if size is too small, reject password Use "length(vector)" which returns the length of a vector

IF exercise > pwd = input('Please enter password: '); pwdsize = length(pwd); %check of

IF exercise > pwd = input('Please enter password: '); pwdsize = length(pwd); %check of pwdsize if (pwdsize < 6) 'password invalid' else 'valid password' end size

SWITCH: a more convenient if If you know in advance SEVERAL OUTCOMES ARE POSSIBLE,

SWITCH: a more convenient if If you know in advance SEVERAL OUTCOMES ARE POSSIBLE, use SWITCH. For example: ask user to choose a number from 1 to 10, each number prints an associated joke.

SWITCH: syntax Something like: choice = input('number from 1 to 10? ') switch choice

SWITCH: syntax Something like: choice = input('number from 1 to 10? ') switch choice case 1 … case 2 … … otherwise … end. Choice is the name of the variable that can take the values listed after the “case” word.

SWITCH: syntax More generally, if testing n switch n case n 1 {n 1,

SWITCH: syntax More generally, if testing n switch n case n 1 {n 1, n 2, …} is the list of … possible values that n case n 2 can take} … otherwise … end

SWITCH example : edit MHarmony. m myage='26'; status='single'; avail='immediate'; info=input('what information do you want

SWITCH example : edit MHarmony. m myage='26'; status='single'; avail='immediate'; info=input('what information do you want to know? . . . Enter 1 for age, 2 for status, 3 for availability or 4 to quit. ') switch info case 1 txt=['The age is ' num 2 str(myage)]; %vectors within brackets are % concatenated too. case 2 txt=['The status is ' status]; case 3 txt=['The availability is ' avail]; otherwise txt=['You Quitter!]; end txt

Switch exercise "Guess the mood of the user (Happy, Surprised, Sad, Angry, Depressed, Overworked)"

Switch exercise "Guess the mood of the user (Happy, Surprised, Sad, Angry, Depressed, Overworked)" Ok, only first four options.

Switch exercise Mood. m %here are your comments for help name = input('What''s your

Switch exercise Mood. m %here are your comments for help name = input('What''s your name? '); %Note double single quotes… %if you don't specify 's', you can still input %a string with the ' command randmood = floor(rand * 4) +1; %rand is between 0 and 1 switch randmood case 1 message = case 2 message = case 3 message case 4 message = end; [name ' is happy!'] [name ' is surprised!'] = [name ' is sad. '] [name ' is angry!']

WHILE A loop for an a priori unknown number of iterations. WHILE (condition is

WHILE A loop for an a priori unknown number of iterations. WHILE (condition is satisfied) keep doing this. End While (not. Found. Gold) Keep. Digging; End

WHILE: syntax while (condition) … if (condition) break end … end %if condition is

WHILE: syntax while (condition) … if (condition) break end … end %if condition is false, loop is not executed, “jumps to end. ” %exits while

Exercise Write a program that asks user for PIN number, until user gets it

Exercise Write a program that asks user for PIN number, until user gets it right.

Exercise • pin=0; • While (pin~=1234) pin=input('What''s your pin number? '); • end;

Exercise • pin=0; • While (pin~=1234) pin=input('What''s your pin number? '); • end;

Exercise • Modify script to end if user has three bad entries. • pin=0;

Exercise • Modify script to end if user has three bad entries. • pin=0; • counter=0; • while (pin~=1234) pin=input('What''s your pin number? '); counter=counter+1; if (counter==3) break end • end;

WHILE: syntax while (condition) %if condition is false, loop is not executed, “jumps to

WHILE: syntax while (condition) %if condition is false, loop is not executed, “jumps to end. ” bla 1 if (condition) continue %goes back to while end Bla 2 end

Homework/in class exercises 1. Write a script encrypt. m that encrypts words. --> don't

Homework/in class exercises 1. Write a script encrypt. m that encrypts words. --> don't forget to add headers and comments to your code. 2. Write Mess. Word. m This program takes a word as input and shuffles only the inner letters of the word, leaving the first and last letter undisturbed. Alejandro --> Arjelando House --> Husoe

Mess. Word What are the steps to undertake? 1. Figure out if the word

Mess. Word What are the steps to undertake? 1. Figure out if the word is valid (no characters or numbers) 2. Figure out if the length is valid (if length < 3, what should happen? ) 3. Use your old homework to do the rest.