CRE Programming Club - Class 4 Robert Eckstein and Robert Heard.

Slides:



Advertisements
Similar presentations
Summer Computing Workshop. Introduction to Variables Variables are used in every aspect of programming. They are used to store data the programmer needs.
Advertisements

Microsoft® Small Basic
Introduction to Computing Science and Programming I
Programming Logic and Design, Third Edition Comprehensive
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Objectives In this chapter, you will learn about:
Repeating Actions While and For Loops
Computer Science 1620 Loops.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
Programming Epson Robots – Part 2 ME 4135 – Fall 2012 Dr. R. Lindeke.
COMP 1001: Introduction to Computers for Arts and Social Sciences Programming in Scratch Monday, May 16, 2011.
In.  This presentation will only make sense if you view it in presentation mode (hit F5). If you don’t do that there will be multiple slides that are.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
CRE Programming Club - Class 7 Robert Eckstein and Robert Heard.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Engineering 1020 Introduction to Programming Peter King Winter 2010.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
PHP Logic. Review: Variables Variables: a symbol or name that stands for a value – Data types ( Similar to C++ or Java): Int, Float, Boolean, String,
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
CRE Programming Club - Class 4 Robert Eckstein and Robert Heard.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Microsoft® Small Basic
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
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.
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
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.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Chapter 7 Problem Solving with Loops
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
CRE Programming Club Class 8 Robert Eckstein and Robert Heard.
CRE Programming Club - Class 5 Robert Eckstein and Robert Heard.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
Controlling Program Flow with Decision Structures.
Algorithms and Pseudocode
Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Microsoft® Small Basic Conditions and Loops Estimated time to complete this lesson: 2 hours.
Repetition Structures The long awaited …. Repetition Structures I repeat …
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
CRE Programming Club - Class 3 Robert Eckstein and Robert Heard.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Repetition Structures Chapter 9
IF statements.
Starter Write a program that asks the user if it is raining today.
MATLAB: Structures and File I/O
ALGORITHMS AND FLOWCHARTS
Control Structures: Part 1
elppa legoog erawdrah erawtfos margorp Cisab llams Apple Google
Repetition Structures
Chapter (3) - Looping Questions.
T. Jumana Abu Shmais – AOU - Riyadh
ALGORITHMS AND FLOWCHARTS
Class code for pythonroom.com cchsp2cs
How to allow the program to know when to stop a loop.
Presentation transcript:

CRE Programming Club - Class 4 Robert Eckstein and Robert Heard

Our Programs Need to Make Decisions We need our programs to be able to make decisions. Let’s look at the following code: If (Clock.Day = 1) And (Clock.Month = 1) Then TextWindow.WriteLine("Happy New Year") EndIf What do you think this program does?

Our Program Needs to Make Decisions Notice that this program contains the If, Then, and EndIf keywords. You use the If keyword to specify a condition that the computer evaluates. You use the Then keyword to specify what the computer should do if the condition is true.

Our Program Needs to Make Decisions If the condition is false, the computer skips what’s between Then and EndIf and proceeds to the next line of the program. You use the EndIf keyword to indicate end of the code that should be run if the condition was true.

What’s a “Condition”? Something that is “tested”, and the end result is either true or false. A condition is usually inside of parenthesis. (a = 20) They can be separated with “And” or “Or” and grouped with parenthesis. ((a = 20) And (b = 15)) Or (c = 25)

Decisions, Decisions... In this example, you use the If keyword specify the condition that today is the first day of the first month of the year (January). You use the Then keyword to specify that, if today is the first day of the first month, the computer should run the WriteLine operation. If today is not the first day of the first month, the computer should skip the block and proceed to the code after the EndIf.

What If You Need to Do Something If It’s False? If (Clock.Hour < 12) then TextWindow.WriteLine("Have your breakfast?") EndIf If (Clock.Hour > 12) then TextWindow.WriteLine("Have your lunch?") EndIf

If/Then/Else Let’s reduce the repetition by using the Else keyword. If (Clock.Hour < 12) Then TextWindow.WriteLine("Have your breakfast?") Else TextWindow.WriteLine("Have your lunch?") EndIf

If/Then/Else In this code, you specify that the computer should perform the first operation if the condition is true. But you’re also saying that the computer should perform the second operation if the condition is false.

Try Importing This Program: JVJ515 TextWindow.Write("Enter a number: ") number = TextWindow.ReadNumber() remainder = Math.Remainder(number, 2) If (remainder = 0) Then TextWindow.WriteLine("The number is even.") Else TextWindow.WriteLine("The number is odd.") EndIf

What Does It Do? In this program, you first ask the user for a number. Then you create a variable to store the number, and you use the ReadNumber() operation to read in the number. Next, you create a variable to store the remainder after you divide the user’s number by 2, and you use the Math.Remainder() operation to determine whether the remainder is 0.

What Does It Do? Finally, you specify that the number is even if the remainder is 0 and that the number is odd if the remainder is not 0. When you write a program, you can specify as many conditions as you want by using the ElseIf keyword. You can also specify one or more operations for the computer to perform, depending on which condition is true when your program is run.

If/Then/ElseIf/EndIf: Import KTH175 TextWindow.WriteLine("What’s the temp in degrees?") temp = TextWindow.Read() If (temp <= 5) Then TextWindow.WriteLine("It’s very cold today.") ElseIf (temp <= 45) Then TextWindow.WriteLine("It’s cool today.") ElseIf (temp <= 85) Then TextWindow.WriteLine("It’s warm today.") Else TextWindow.WriteLine("It’s quite hot today.") EndIf

Loops You can use a loop to instruct the computer to run one or more statements more than once. You can use a For loop if you know exactly how many times you want the computer to repeat the instructions. You can use a While loop if you want the program to repeat the instructions until a condition is true.

For Loops For a = 1 to 10 TextWindow.WriteLine(a) EndFor In this example, the variable a contains a value that goes up by 1 every time that the loop runs.

For Loops - Import FQZ998 Let’s use this concept to print the multiplication table for the number 5. Try running this: TextWindow.WriteLine("Multiplication Table") n = 5 For a = 1 to 10 TextWindow.WriteLine(a + " x " + n + " = " + (a * n)) EndFor

What Did This Program Do? In this program, you first use the WriteLine() operation to display "Multiplication Table" on the screen. Then you create the variable n to store the value of 5. Then you create a For loop with the variable a to ensure the WriteLine() operation will run 10 times.

String Concatenation You use the WriteLine() operation to display the following elements in this order: 1. The value that is stored in the a variable 2. The multiplication sign 3. The value that is stored in the n variable 4. The equals sign 5. The products of the values of the a and n variables

For Loops In the previous example, the value of the counter variable in a For loop increases by 1 every time the loop runs. However, you can increase the value by another number if you use the Step keyword.

For Loops with Steps For example, you can increase the value of a by 2 during during each pass if you write the following code: For a = 1 to 10 Step 2 TextWindow.WriteLine(a + " x " + n + " = " + (a * n)) EndFor

For Loops with Steps You can even decrease the value of the loop variable every time that the code runs if you use the Step keyword, but specify a negative number. For example, you can write a program that counts backward from 10 to 1 on the screen if you assign the value of -1 to the Step keyword.

While Loops If you don’t know the loop count before you write a program, you can create a While loop instead of a For loop. When you create a While loop, you specify a condition that is true when the loop starts. The computer evaluates the condition every time that the loop repeats. When the condition becomes false, the loop stops.

While Loops - HTJ964 Let’s write the following program to demonstrate the While loop: a = 10 While (a <= 100) TextWindow.WriteLine(a) a = a + 10 EndWhile

What Did This Program Do? In this example, you first create the a variable and set its value to 10. Next, you create a While loop with a condition that the value of the a variable is smaller than or equal to 100. Because you just set the value of that variable to 10, the condition is true when the loop starts.

While Loops You use the WriteLine() operation to display the value of the a variable every time that the loop runs. In the next statement, you increase the value of the a variable by 10 every time that the loop runs. The loop stops after it runs 10 times because the value of the a variable becomes larger than 100.

Goto - QHV742 You can instruct the computer to jump around if you use the Goto statement. j = 1 lineQ: TextWindow.WriteLine(j) j = j + 1 If (j < 30) Then Goto lineQ EndIf

Goto In this program, the lineQ: statement is called a label, which is similar to a bookmark. You can add as many labels as you want and name them whatever you want, as long as you don’t use the same name more than once. The Goto statement instructs the computer to run the statements after the lineQ: label again only if the condition in the If statement is true.

What Does the Program Do? In the first line of this program, you create a variable that is named j, and you set its value to 1. Then you create a label that is named lineQ: with a colon (:) at the end. In the next line, you tell the computer to display the value of the j variable on the screen. Then you increase the value of the j variable by 1.

What Does the Program Do? In the fourth line, you tell the computer to display the value of the j variable, increase its value by 1, and then determine whether that value is smaller than 10. If the value of the j variable is not smaller than 10, you tell the computer to continue to the next part of the program (or to stop running the program if no more code exists).

Try This - HWV179 start: TextWindow.WriteLine("How many members are in your family?") number = TextWindow.ReadNumber() remainder = Math.Remainder(number, 2) If (remainder = 0) Then TextWindow.WriteLine("Your family has an even number of members.") Else TextWindow.WriteLine("Your family has an odd number of members.") EndIf Goto start

Warning about Goto’s Warning: If you overuse Goto statements, your code will be difficult to understand and to maintain. Although these statements are useful sometimes, you should try to structure your programs so that you rarely use Goto statements. Too many Goto statements will make your program paths through the code look like spaghetti, so it is often called spaghetti code.

Next Time... We’re going to learn about subroutines and more about the graphics window.