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
RAPTOR Syntax and Semantics By Lt Col Schorsch
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Introduction to Computing Science and Programming I
Chapter 2 - Algorithms and Design
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
ITEC113 Algorithms and Programming Techniques
Objectives In this chapter, you will learn about:
Repeating Actions While and For Loops
Computer Science 1620 Loops.
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
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.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
ALGORITHMS AND FLOWCHARTS
COMP 1001: Introduction to Computers for Arts and Social Sciences Programming in Scratch Monday, May 16, 2011.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
For Loops. Challenge: Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● Make X vary, so it says 1, then 2, then 3 ● Use only one output.
CRE Programming Club - Class 7 Robert Eckstein and Robert Heard.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Chapter 2 - Algorithms and Design
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.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 5 Scott Marino.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Python Programming Chapter 6: Iteration Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
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,
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
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.
Loops & Graphics IP 10 Mr. Mellesmoen Recall Earlier we wrote a program listing numbers from 1 – 24 i=1 start: TextWindow.WriteLine(i) i=i+1 If.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
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.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
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.
Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure.
CRE Programming Club - Class 4 Robert Eckstein and Robert Heard.
Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
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.
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.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Introduction To Repetition The for loop
Repetition Structures Chapter 9
Starter Write a program that asks the user if it is raining today.
ALGORITHMS AND FLOWCHARTS
elppa legoog erawdrah erawtfos margorp Cisab llams Apple Google
Selection CIS 40 – Introduction to Programming in Python
Repetition Structures
Chapter (3) - Looping Questions.
T. Jumana Abu Shmais – AOU - Riyadh
ALGORITHMS AND FLOWCHARTS
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 the 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.

Homework Do one (or both) of the following: -Use a for loop to draw 100 shapes in random positions on the GraphicsWindow. Vary the colors and sizes of each shape before it is drawn. Use Math.GetRandomNumber(…). -Use the Clock object to make a clock on the GraphicsWindow that updates every second. Note that GraphicsWindow.DrawText(…) does not erase what was there previously. Use if’s to handle AM/PM and numbers less than 10. Use Program.Delay(1000) in the loop as well.

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