15
ITERATION LOOPING MECHANISM

Lesson 5 iteration and loops

Embed Size (px)

Citation preview

Page 1: Lesson 5   iteration and loops

ITERATIONLOOPING MECHANISM

Page 2: Lesson 5   iteration and loops

ITERATION

The Iteration is a looping mechanism. It repeats the process as prescribed by the loop. It uses counter and accumulator

Page 3: Lesson 5   iteration and loops

ADDING SMARTS TO YOUR PROGRAMGOTO COMMANDIF-THEN STATEMENT

Page 4: Lesson 5   iteration and loops

GOTO – Doing something more than once

Is a way we need for our program to go to the beginning and go it over .

Syntax: goto [branchlabel]

Page 5: Lesson 5   iteration and loops

Two ways to define a branch label1. Integer Number Branch Label

10 150 75 900 5400 etc…

2. Alphanumeric Branch Label

[start] [loopback] [getResponse] etc…

Unacceptable Branch Label [loop back] no spaces allowed Start must use brackets (point1) only use square brackets

Page 6: Lesson 5   iteration and loops

Sample Program[start]

amount=0

Tax=0

input “Type a dollar and cent amount”;amount

let tax=amount*0.05

print “Tax is: “;tax;”. Total is: “;tax+amount

goto [start]

Page 7: Lesson 5   iteration and loops

OUTPUT

Page 8: Lesson 5   iteration and loops

IF…THEN – Adding Smarts to Our Tax Program

When we consider two or more conditions that can affect our program, we would have to use a statement that would allow us some degree of decision making. This is where the IF…THEN statement will come in.

Page 9: Lesson 5   iteration and loops

Relational Operators used in JustBasic Symbol Meaning

= Equal to

< Less Than

> Greater Than

<= Less Than or Equal to

>= Greater Than or Equal To

<> Not Equal to

Examples:

answer$=“Y” Width < Length

Grade<=75 Grade>90

Page 10: Lesson 5   iteration and loops

To be able to use the IF…THEN Statement you must follow the syntax:

IF (condition) THEN (action)

When used in program, if the condition that was set is true the action following the “THEN” portion is executed, otherwise the IF…THEN line is bypass or ignored. For example

cls

name1$=“”

name$=“john”

input “Guess the name I entered: “;name1$

if name$=name1$ then print “You Got it right!!!”

print “You are Wrong the name is John.”

end

Page 11: Lesson 5   iteration and loops

If there are two actions that need to be done, you will use an extension of the IF…THEN statement, which is the IF…THEN…ELSE statement

syntax:if (condition) then(action 1)else(action 2)end if

The end if part of the last line is essential for you to avoid an error that occurs when you do not close your IF…THEN…ELSE statement.

Page 12: Lesson 5   iteration and loops

Sample Programcls

name1$=“”

name$=“john”

input “Guess the name I entered: “;name1$

if name$=name1$ then

print “You Got it right!!!”

else

print “You are Wrong the name is John.”

end if

end

Page 13: Lesson 5   iteration and loops

Sample Program[start]

print "Type a dollar and cent amount: "

input "(Press 'Enter' alone for Help)?";amount

if amount=0 then goto [help]

let tax=amount*0.05

print "Tax is: ";tax;". Total is: ";tax+amount

goto [start]

[help]

print "This tax program determines how much tax is"

print "due to an amount entered and also computes"

print "the total amount. The Tax rate is 5%"

goto [start]

Page 14: Lesson 5   iteration and loops

Problem:

Create a program that will input two numbers and print out the larger number. The program will ask the user if he/she want to try another one. If yes then it will go back at the start of the program if not, then the program ends.

Page 15: Lesson 5   iteration and loops

Output: