Upload
christopher-olaya
View
144
Download
1
Embed Size (px)
Citation preview
ITERATIONLOOPING MECHANISM
ITERATION
The Iteration is a looping mechanism. It repeats the process as prescribed by the loop. It uses counter and accumulator
ADDING SMARTS TO YOUR PROGRAMGOTO COMMANDIF-THEN STATEMENT
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]
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
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]
OUTPUT
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.
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
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
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.
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
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]
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.
Output: