1 / 10

COMP 116: Introduction to Scientific Programming

COMP 116: Introduction to Scientific Programming . Lecture 15: While Loops. Recap. What we’ve learnt so far: MATLAB stuff Matrices, vectors, indexing, matrix manipulation Programming stuff Conditional statements (if-else-end). If-else-end.

gamada
Download Presentation

COMP 116: Introduction to Scientific Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP 116: Introduction to Scientific Programming Lecture 15: While Loops

  2. Recap What we’ve learnt so far: • MATLAB stuff • Matrices, vectors, indexing, matrix manipulation • Programming stuff • Conditional statements (if-else-end)

  3. If-else-end • Interpreted as: If the logical expression <test> evaluates to true execute commands1, otherwise execute commands2 if <test> commands1; % True else commands2; % False end

  4. Display absolute value % Implement our own absolute value % inVal = |inVal| % inVal should already be assigned % Is input value negative ? if (inVal < 0) % Parenthesis optional outVal = -inVal; % Invert negative else outVal = inVal; % Keep positive end disp(outVal);

  5. Loops • When you want to do something over and over again • Interpreted as: • Evaluate <test> • If true run commands1 • Go to step 1 while <test> % Called the loop condition commands1; % Called the body of the loop end

  6. What does this code do? % Assume A is a pre-assigned vector i=1; B=zeros(size(A)); while i<=length(A) B(i)=A(i)*2; i=i+1; end • Does this code terminate? • What is the value of i when the code terminates? • Upload answers using the link on the lecture page

  7. What does this code do? % Assume A is a pre-assigned vector i=1; var=0; while i<=length(A) var=var+A(i); i=i+1; end • Does this code terminate? • What is the value of i when the code terminates? • What is the value of var when the code terminates?

  8. Exercise I: Cumulative sum • Given a vector A, write a script that computes the cumulative sum of A i.e. A vector B, such that • Use cumsum(A) to get an idea B=zeros(size(A)); B(1)=A(1); i=2; while i<=length(A) B(i)=B(i-1)+A(i); i=i+1; end

  9. Exercise II: Guess the secret number % Define a secret number between 0 and 20, make % the user guess it secret = 7; guess = input(“Guess the number: ”); -- if the guess is less than secret-- disp(`low'); -- if the guess is greater than secret-- disp(`high.'); -- otherwise -- disp(num_guesses);

  10. Exercise II: Guess the secret number secret=ceil(20*rand); curr_guess=input(‘Guess: ‘); num_guesses=1; while (curr_guess ~= secret) if (curr_guess > secret) disp(‘high’); else disp(‘low’); end curr_guess=input(‘Guess: ‘); num_guesses=num_guesses+1; end disp(num_guesses); • Implement the guess-the-secret-number game using while loops and if-else-end. Count how many guesses the user needs • What should be the loop condition? • What should happen inside the loop body?

More Related