Hi, I just wanted to know if i can do a Do While with two conditions and how

+4 votes
asked Oct 6 by Magale (210 points)
What I want to do is that the program asks you for a number from 1 to 10 and tells you the table, but if someone puts a number greater than 10, the program write that they made a mistake and do not do the loop, I don't know if it is understood

I am using VB console and this is what I wrote but it throws an error, clearly I am missing something but I can't see what it is

Module VBModule
    Sub Main()
        dim numbertable as integer
        dim counter as integer
       
       console.write ("Enter the table number (from 1 to 10): ")
       numbertable = console.readline ()
       
       counter = 1
       
        if numbertable > 10 then
       console.writeline ("The number is wrong")
       end if
       
   do while  ((contador < 11)) & ((numbertable < 10))
       console.writeline (numbertable & "X" & counter & "=" & numbertable * contador)
       counter = counter + 1
       loop   
 
    End Sub
    
End Module

I would really appreciate the help

3 Answers

0 votes
answered Oct 6 by FGolazeski (140 points)
"contador" is not declared.
"And" is the logical operation you're trying to use, not "&".
Also, you don't need to check numbertable every time through the loop because it won't change in the loop. You check it in the If statement so your loop can be part of the If's Else clause.
+2 votes
answered Oct 7 by Peter Minarik (12,220 points)

Problems With The Code

If you look at the build output, you can see the compilation errors.
contador is not a valid identifier. Did you mean counter?
After this fix, you can run the code, but there will be runtime errors.
The problem here seems to be that console.readline does not return an integer, but a String. You need to parse the integral number from the string: Int32.TryParse().
Another problem was that you were trying to use the & (string concatenation) operator instead of the And (logical and) operator. I've fixed that in the code below.

Fixed Code

Module VBModule
    Sub Main()
        console.write ("Enter the table number (from 1 to 10): ")
        dim line as string = console.readline ()
        dim numbertable as integer
        if Int32.TryParse(line, numbertable) then
            if numbertable <= 10 then
                dim counter as integer = 1
                do while (counter < 11 And numbertable < 10)
                    console.writeline (numbertable & "X" & counter & "=" & numbertable * counter)
                    counter = counter + 1
                loop
            else
                console.writeline ("The number is wrong")
            end if
        else
            console.writeline("numbertable is invalid")
        end if
    End Sub
End Module

Disclaimer

I've never written a VB code before. Apologies for all the mistakes I may have made.

0 votes
answered Nov 13 by Ali Mansoor (140 points)
console.write("Enter the number: ")
number = console.readline()
Do While number > 10 or number < 1
    console.write("Enter between 1-10:")
    number = console.readline()
Loop

for counter = 1 to 10
   console.writeline(number & "x" & counter & "=" & number*counter)
Next
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...