Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 For loops inside nested if statements output error
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
jinjin




PostPosted: Sun Mar 16, 2008 4:18 pm   Post subject: For loops inside nested if statements output error

Hey everybody,

I'm back with another problem with loops and again its for Gr. 10 programming.

I was supposed to write a program that asks the user for a low integer, then a high integer, and then asks the user if they want to count from low to high (LH) or high to low (HL). Then, a loop was to be outputted based on their low/high integers and their loop setting.

Here's what I have:

code:

var high, low : int
var ans : string
put "Please enter a low integer."
get low
put "Please enter a high integer."
get high
put "Do you want to count from low to high (LH) or high to low (HL)?"
get ans
if ans = "LH" then
    for c : low .. high
        put c
        if ans = "HL" then
            for c2 : high .. low
                put c2
            end for
        end if
    end for
end if


The above program loops perfectly fine when someone inputs "LH" but if the user inputs "HL," it just ends abruptly. I have no idea why this occurs because both loops are written in the exact same way; the only difference is the variables 'c' and 'c2.' Any help is appreciated on fixing this. Thanks Smile

Regards,
jinjin
Sponsor
Sponsor
Sponsor
sponsor
A.J




PostPosted: Sun Mar 16, 2008 4:26 pm   Post subject: Re: For loops inside nested if statements output error

it seems okay except for one tiny part:
Turing:

if ans = "LH" then
    for c : low .. high
        put c
        if ans = "HL" then
            for c2 : high .. low
                put c2
            end for
        end if
    end for
end if

try closing the first for loop
after outputting the answer
Turing:

for c : low .. high
        put c
end for

and instead of using another if statement, use an elsif statement:
Turing:

if ans = "LH" then
    for c : low .. high
       % ouput here
    end for
elsif ans="HL" then
    for c : high..low
        % other output here
    end for
end if

but you can't have a for loop with high..low (since high>low)
but there's a word you have to add before it to tell the for loop that the numbers are decreasing Very Happy
see if this help
jinjin




PostPosted: Sun Mar 16, 2008 4:32 pm   Post subject: Re: For loops inside nested if statements output error

A.J @ Sun Mar 16, 2008 4:26 pm wrote:
it seems okay except for one tiny part:
Turing:

if ans = "LH" then
    for c : low .. high
        put c
        if ans = "HL" then
            for c2 : high .. low
                put c2
            end for
        end if
    end for
end if

try closing the first for loop
after outputting the answer
Turing:

for c : low .. high
        put c
end for

and instead of using another if statement, use an elsif statement:
Turing:

if ans = "LH" then
    for c : low .. high
       % ouput here
    end for
elsif ans="HL" then
    for c : high..low
        % other output here
    end for
end if

but you can't have a for loop with high..low (since high>low)
but there's a word you have to add before it to tell the for loop that the numbers are decreasing Very Happy
see if this help


Just what I needed - works perfect now Very Happy Very Happy Thanks A.J!
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: