Microsoft® Small Basic

Slides:



Advertisements
Similar presentations
Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Advertisements

Microsoft® Small Basic
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
CS0004: Introduction to Programming Repetition – Do Loops.
Microsoft® Small Basic
An Introduction to Textual Programming
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Engineering 1020 Introduction to Programming Peter King Winter 2010.
Bug Session Four. Session description Objectives Session activities summary Resources Prior knowledge of sequencing instructions using Bug Bug website.
Examining data using Microsoft Access Queries Using Criteria and Calculations SESSION 3.2 This section covers specifying an exact match condition in a.
CS 108 Computing Fundamentals Notes for Thursday, February 19, 2015.
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.
Slide 3-1 CHAPTER 3 Conditional Statements Objectives To learn to use conditional test statements to compare numerical and string data values To learn.
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,
CRE Programming Club - Class 4 Robert Eckstein and Robert Heard.
ANALYSIS AND ALGORITHM DESIGN - IV. Repeat statements/looping/counting/iterations It is often necessary to repeat certain parts of a program a number.
Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True.
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.
1 Module 3 Conditional Statements. Objectives  Conditional test statements to compare numerical and string data values  Looping statements to repeat.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Chapter 2 Pseudocode. Objectives To introduce common words, keywords and meaningful names when writing pseudocode To define the three basic control structures.
Pseudocode. Simple Program Design, Fourth Edition Chapter 2 2 Objectives In this chapter you will be able to: Introduce common words, keywords, and meaningful.
Pseudocode Simple Program Design Third Edition A Step-by-Step Approach 2.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Basic Control Structures
Algorithm Design.
Programming with Microsoft Visual Basic th Edition
CW-V1 SDD 0901 Principals of Software Design and Development Loops Starter: Water JugsWater Jugs.
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.
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 4 Robert Eckstein and Robert Heard.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
Chapter Five More on the Selection Structure Programming with Microsoft Visual Basic th Edition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
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.
Copyright © 2003 Pearson Education, Inc. Slide 3-1 The Web Wizard’s Guide to PHP by David A. Lash.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
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.
Computer Science Up Down Controls, Decisions and Random Numbers.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Repetition Structures Chapter 9
IF statements.
The Selection Structure
Starter Write a program that asks the user if it is raining today.
elppa legoog erawdrah erawtfos margorp Cisab llams Apple Google
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Iteration: Beyond the Basic PERFORM
Logical Operations In Matlab.
Chapter 5: Control Structure
Computer Science Core Concepts
Let’s all Repeat Together
ICT Programming Lesson 3:
ICT Gaming Lesson 3.
Chapter 3: Selection Structures: Making Decisions
Using Decision Structures
Chapter 3: Selection Structures: Making Decisions
GCSE Computing.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Control Structures.
Presentation transcript:

Microsoft® Small Basic Conditions and Loops Estimated time to complete this lesson: 2 hours

Conditions and Loops In this lesson, you will learn how to: Write programs that carry out different instructions based on whether one or more logical conditions are true. Write programs that repeat instructions until a specific event occurs.

Conditions in Small Basic Programs Would you like to specify conditions that control how your program runs (or whether it runs at all)? Let’s look at the following program: This program instructs the computer to display "Happy New Year" only if today is January 1st.  In Small Basic, you use the Clock object to determine the current date and time.   You use the If keyword to specify a condition that the computer evaluates to determine whether it should perform a particular operation. You use the Then keyword to specify what operation or operations the computer should perform if the condition is true. If the condition is false, the computer skips the operation or operations and proceeds to the next line of the program. You use the EndIf keyword to indicate that the computer should proceed to the next line of the program regardless of whether the condition was true. 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 operation and proceed to the EndIf line of the program. Code: If Clock.Day = 1 And Clock.Month = 1 Then TextWindow.WriteLine("Happy New Year") EndIf Notice that this program contains the If, Then, and EndIf keywords.

Conditions in Small Basic Programs Now, let’s write a program in which you specify an alternate action to perform if the condition is false. Depending on when you run the program, the computer displays one of the following results: Code: If Clock.Hour < 12 then TextWindow.WriteLine("Did you have your breakfast?") EndIf If Clock.Hour > 12 then TextWindow.WriteLine("Did you have your lunch?")

Conditions in Small Basic Programs In programming, you can do that same thing in more than one way. As a programmer, you can choose the best way. In this example, you might have noticed that the second condition in the program repeats a lot of information in the first condition. Let’s reduce the repetition by using the Else keyword. In this program, you specify a condition and an operation to perform if that condition is true. Then you specify a second condition and a second operation to perform if the second condition is true. However, the first condition is true only if the second condition is false, and the second condition is true only if the first condition is false. Therefore, you don’t need to specify the second condition because the computer can determine which operation to perform based only on the first condition. Instead of giving the computer two conditions to evaluate, you can specify that the computer should perform the first operation if the first condition is true and the computer should perform the second operation if the first condition is false.   The result of both approaches is the same. This example shows that you can do the same thing in different ways in programming. It’s up to you! Code: If Clock.Hour < 12 Then TextWindow.WriteLine("Did you have your breakfast?") Else TextWindow.WriteLine("Did you have your lunch?") EndIf Both programs give the same result, but you can use fewer If, Then, and EndIf keywords if you use the Else keyword.

Conditions in Small Basic Programs Let’s look at another example… You are writing a complex program, and you want to check whether the user typed an even number or an odd number. output 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 determine what number the user specified.   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. 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. Code: 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 Notice the use of If, Then, Else, and EndIf in the program.

Conditions in Small Basic Programs 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. Let’s look at this with an example. In this example, each condition contains a unique statement that the computer evaluates. When the computer evaluates a statement as true, the computer performs the operation for that condition and then proceeds to the end. Code: TextWindow.WriteLine("What is the current temperature in degrees Celsius? ") temp = TextWindow.Read() If temp <= 5 Then TextWindow.WriteLine("It’s very cold today.") ElseIf temp <= 15 Then TextWindow.WriteLine("It’s cool today.") ElseIf temp <= 25 Then TextWindow.WriteLine("It’s warm today.") Else TextWindow.WriteLine("It’s quite hot today.") EndIf

Fortune Telling You can use an If statement to write a fortune teller program You can use a Math function to get a random number. This number can then be used in an extended If statement to display a choice of answers such as If your feeling lucky The stars are confused ask later No way Not today Definitely Can you write the program?…Could you add the users name to the random answer to make it more personal?... PLAN or ALGORITHM Start Prog Get user name Ask them to think of their question Press enter when ready Get random number 1-6 If randNum is 6 display Ans 6 ElseIf randNum is 5 display Ans 5 ElseIf randNum is 4 display Ans 4 ElseIf randNum is 3 display Ans 3 ElseIf randNum is 2 display Ans 2 ElseIf randNum is 1 display Ans 1 Else display Ans 0 End If Display name and thank them for playing End Prog

Loops in Small Basic Programs 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 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 specific condition is true. So, let’s explore some loop statements…

Loops in Small Basic Programs Let’s start by writing a program that contains a For..EndFor loop. In general, you use a For..EndFor loop to run code a specific number of times. To manage this type of loop, you create a variable that tracks how many times the loop has run. Code: For a = 1 to 10 TextWindow.WriteLine(a) EndFor Click the button on the Toolbar. In this example, the variable contains a value that increases by 1 every time that the loop runs.

Loops in Small Basic Programs Let’s use this concept to print the multiplication table for the number 5. output In this program, you first use the WriteLine operation to display "Multiplication Table" on the screen. Then you create the variable “number” 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. You use the WriteLine operation to display the following elements in this order: --the value that is stored in the “a” variable --the multiplication sign --the value that is stored in the “number” variable --the equals sign --the products of the values of the “a” and “number” variables Code: TextWindow.WriteLine("Multiplication Table") number = 5 For a = 1 to 10 TextWindow.WriteLine(a + " x " + number + " = " + a * number) EndFor

Loops in Small Basic Programs 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 example, you can increase the value by 2 if you write the following code: 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. Code: TextWindow.WriteLine("Multiply odd numbers by 5:") number = 5 For a = 1 to 10 Step 2 TextWindow.WriteLine(a + " x " + number + " = " + a * number) EndFor

Operations in Small Basic Programs In the next example we use some mathematical symbols > means greater than < means less than They can be combined with = <= means less than or equal to >= means greater than or equal to We can also use the symbol <> this means unequal to Try to complete the problems below Does the condition return true of false? 21 >= 34 21 < = 34 21 > 15 21 >= 21

Loops in Small Basic Programs 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. But the computer evaluates the condition every time that the loop repeats. When the condition becomes false, the loop stops. Let’s write the following program to demonstrate the While loop: 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. 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. Code: a = 10 While (a <= 100) TextWindow.WriteLine(a) a = a +10 EndWhile

Let’s Summarize… Congratulations! Now you know how to: Write programs that evaluate logical conditions and perform operations based on those results. Write programs that repeat one or more instructions either a specific number of times or based a logical condition.

Show What You Know Create a program to convert one or more student scores from a percentage to a letter grade. First, ask the user to specify how many grades will be calculated. Then ask the user to specify the first percentage, and convert it to a letter grade based on the following criteria: If the percentage is more than 75, convert it to an A. If the percentage is less than 75 but more than or equal to 60, convert it to a B. If the percentage is less than 60 but more than or equal to 35, convert it to a C. If the percentage is less than 35, convert it to a D.