Loops For loop for n = [2 5 8 7 3] code end While loop while a ~= 3 code end.

Slides:



Advertisements
Similar presentations
MATLAB Examples. CS 1112 MATLAB Examples Find the number of positive numbers in a vector x = input( 'Enter a vector: ' ); count = 0; for ii = 1:length(x),
Advertisements

COMPUTER PROGRAMMING I Understand Problem Solving Tools to Design Programming Solutions.
Flow Charts, Loop Structures
3-2 What are relational operators and logical values? How to use the input and disp functions. Learn to use if, if-else and else-if conditional statements.
1 CS 161 Introduction to Programming and Problem Solving Chapter 9 C++ Program Components Herbert G. Mayer, PSU Status 10/20/2014.
Representing a Game Board In a game, we represent the action taking place using an array – In a very simple game, we use individual variables to represent.
James Tam Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.
J. Michael Moore Other Control Structures CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
EGR 106 – Project Intro / While Loop Project description Project schedule Background information / terminology A simple example Examples of similar programs.
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
MOM! Phineas and Ferb are … Aims:
11 Finding Winners Using Arrays Session 8.2. Session Overview  Find out how the C# language makes it easy to create an array that contains multiple values.
Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes.
MATLAB FUNDAMENTALS: CONTROL STRUCTURES – LOOPS HP 100 – MATLAB Wednesday, 10/1/2014
Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
Flowcharts.
Coding Design Tools Rachel Gauci. What are Coding Design Tools? IPO charts (Input Process Output) Input- Make a list of what data is required (this generally.
© 2006 Pearson Education 1 More Operators  To round out our knowledge of Java operators, let's examine a few more  In particular, we will examine the.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Chapter 1 Pseudocode & Flowcharts
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Python Arrays. An array is a variable that stores a collection of things, like a list. For example a list of peoples names. We can access the different.
ENG College of Engineering Engineering Education Innovation Center 1 More Script Files in MATLAB Script File I/O : Chapter 4 1.Global Variables.
Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
18/12/ Arrays 2D Arrays Defining, Declaring & Processing.
Week 9 - Programming III Today: – Another loop option – A programming example: tic-tac-toe Textbook chapter 7, pages , (sections 7.4.2,
EGR 115 Introduction to Computing for Engineers Designing The Battleship Game in MATLAB Monday 22 Sept 2014 EGR 115 Introduction to Computing for Engineers.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Loops and Files. 5.1 The Increment and Decrement Operators.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 1 Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
Beginning Programming for Engineers Matlab Conditional Computation.
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code. Repetition in Pascal:
ECS10 10/10
Week 8 - Programming II Today – more features: Loop control
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Pasa números Divide students in to teams and have each team sit in a row. Give each student a dry erase marker and something to erase with. Each team gets.
Duo By: Fernando & Vivian.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Flowcharts and Pseudo Code
Tic Tac Toe application
User Input Keyboard input.
Pseudo-Code Conditional Branches
Tic-Tac-Toe Game Engine
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Loops For loop for n = [ ] code end While loop while a ~= 3 code end

The if else command Note – many elseifs are allowed, but only 1 “else”

Variable values by example index1index2 All possible combinations of the indices are generated.

Prior example – compounded interest until the amount doubles: value = 1000; for year = 1:1000 value = value * 1.08; disp([num2str(year),' years: $ ',num2str(value) ]) if value > 2000 break end for loop terminated with break

Expected output:

while version format bank value = 1000; while value < 2000 value = value * 1.08; disp(value) end

Example – Collecting and storing data until a zero is entered: x = [ ]; new = 1; while new ~= 0 new = input('enter value '); x = [ x, new ]; end x = x(1:end–1) empty array to drop the zero initialize

Example – Getting valid keyboard input: E.g. forcing the user’s input to be between 0 and 10: x = –3; while ( x 10 ) x = input( 'type a value ' ); end

or: x = input('enter value '); while (x 10) disp('invalid choice'); x = input('enter value '); end disp('finally!');

Example – computing pi:

Example – “infinite” Hi-Lo: numb = round (10*rand(1)); done = 0; while ~done guess = input('guess'); if guess = = numb disp( 'You got it !!!' ); done = 1; elseif guess > numb disp('too high') else disp('too low') end initialization single guess loop control

3-by-3 array for the board: – Empty cell = 0 – X = +1 – O = – 1 Game end: – Winner if row, col, or diag sum = +3 or –3 – Draw is no zeros left Loop for game: – Initial move (X) plus 4 pairs of moves – Break if winner

Flowchart:

Program Outline:

Program Details: Initialization

Board Graphic

Get and Show First Move (X)

Start Loop with the Second Player

Check for Victory by O

Finish Loop with the First Player

Check for a Draw % check for draw if all(result ~= 0) & (sum(sum(abs(board))) == 9) disp('Nobody wins') end

Typical Output