
-----------------------------------
jinjin
Sun Mar 16, 2008 4:18 pm

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:


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 :) 

Regards,
jinjin

-----------------------------------
A.J
Sun Mar 16, 2008 4:26 pm

Re: For loops inside nested if statements output error
-----------------------------------
it seems okay except for one tiny part:

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 

for c : low .. high
        put c
end for

and instead of using another if statement, use an elsif statement:

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 :D
see if this help

-----------------------------------
jinjin
Sun Mar 16, 2008 4:32 pm

Re: For loops inside nested if statements output error
-----------------------------------
it seems okay except for one tiny part:

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 

for c : low .. high
        put c
end for

and instead of using another if statement, use an elsif statement:

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 :D
see if this help

Just what I needed - works perfect now  :D  :D Thanks A.J!
