Computer Science Canada

"for" statements

Author:  air_force91 [ Thu Nov 20, 2003 2:57 pm ]
Post subject:  "for" statements

here's the output i need to get:

5 4 3 2 1
These are small numbers


How do i get this? I know how to use the for and end for statement...im only supposed to use the statement to get those numbers but i get more than one sentences for "These are small numbers"...so how do i get the output as it is above? i need it ASAP...pls...thanx

Author:  Tony [ Thu Nov 20, 2003 3:03 pm ]
Post subject: 

code:

for decreasing i: 5 .. 1
put i
end for

Author:  air_force91 [ Thu Nov 20, 2003 3:43 pm ]
Post subject: 

that's the thing...that's the way i created my code but the numbers appear in columns...i want them to appear across the screen... Sad

Author:  Mazer [ Thu Nov 20, 2003 4:29 pm ]
Post subject: 

code:

for decreasing i : 5 .. 1
    put i, " "..
end for

Author:  Tony [ Thu Nov 20, 2003 7:03 pm ]
Post subject: 

unlike C++, turing always sticks newline character after each put statment. You have to place .. at the end of the put statment to get rid of new line as in Mazer's example

Author:  air_force91 [ Thu Nov 20, 2003 7:20 pm ]
Post subject: 

thanx a lot...it really helped...another ques if u have time to answer it...ive written a program...it actullay ansks the user the name and calculates the average of three courses they're taking...

var name,answer:string
var winID:int
var mark,mark1,mark2,staverage:real

winID:=Window.Open("position:top,center,graphics:640;640")

loop
put "Please enter your name: "..
get name
cls

put "Hello ", name, " Please enter marks for your first course: "..
get mark
cls

put "Please enter marks for your second course: "..
get mark1
cls

put "Please enter marks for your third course: "..
get mark2
cls

staverage:=(mark + mark1 + mark2)/3

put "Your average is: ", staverage
delay(10000)
cls

put "Would you like to continue(yes/no): "..
get answer
loop
if answer="no" then
Window.Close(winID)
end if
end loop
end loop


what i wanna know is that before the "end if"...i could include "elsif" if the user's answer is "yes"...my teacher said to use "randint" but whenever i type it in...i get this error saying there are no parameters or something...so what should i include if i want the loop part of my code to repeat itself if the answer is "yes"? and could u explaint the randint command to me?? thanx.... Laughing

Author:  Tony [ Thu Nov 20, 2003 7:34 pm ]
Post subject: 

Confused your teacher wants you to have a random chance to exit the program?

anyways...
code:

loop
if answer="no" then
Window.Close(winID)
end if
end loop

thats a problem... this should not be in the loop. Just take the loop part out and everything should work fine.

Author:  air_force91 [ Thu Nov 20, 2003 7:44 pm ]
Post subject: 

isn't ur bit of code same as mine? i don't get it...what is randint used for? ok u know how i have the "loop" inserted for the gathering of marks...well...if the user answers "yes" then is there a way i could just have the loop part (the marks part) repeat itself...i think this is where randint is used...? pls help me out....it's only been a week since ive learnt to program.... Crying or Very sad

Author:  Tony [ Fri Nov 21, 2003 12:35 am ]
Post subject: 

that bit of code is yours... that's where the problem is.

randint has nothing to do with loops. It is used to create a (pseudo) random number.

yours should be like
code:

loop
...
your marks here
...
put "exit? y/n"
get answer

if answer = "y" then
exit
end if
end loop


so if user's answer is "y", if statment executes exit command, which jumps out of the loop. Otherwise if statment is skipped and program loops back to the beginning

Author:  Andy [ Fri Nov 21, 2003 6:56 pm ]
Post subject: 

u noe, having to put \n or endl after every line is kinda annoying...


: