0

I have a problem trying to output the result of a multiplication table made in CMD using a conditional loop. The code is the following:

@ECHO OFF & CLS

SET AUX=0

IF "%1" == "" (
    SET /P num=Introduzca el numero a mostrar su tabla de multiplicar: 
) ELSE (
    SET num=%1
)

:sumarCont
SET /A AUX+=1

IF /I %AUX% LEQ 10 (

    SET /A mul=%num%*%AUX%
    ECHO %AUX% * %num% = %mul%
    GOTO :sumarCont
)

@PAUSE > nul

The thing is that in the first loop interaction the variable called AUX won't update ending up in an output like this:

Introduzca el numero a mostrar su tabla de multiplicar: 5
1 * 5 =   
2 * 5 = 5 
3 * 5 = 10
4 * 5 = 15
5 * 5 = 20
6 * 5 = 25
7 * 5 = 30
8 * 5 = 35
9 * 5 = 40 
10 * 5 = 45

I have tried using Delayed Expansion but the result remains the same. What am I missing?

Thanks in advance.

  • The correct way of performing arithmetic with defined variables is `Set /A mul=num*AUX` not `SET /A mul=%num%*%AUX%`. – Compo Mar 27 '20 at 14:02

1 Answers1

0

While I was playing with the code I got it to work properly. Tha only thing I change was this line of code.

:sumarCont
SET /A AUX+=1
SET /A mul=num*AUX

IF /I %AUX% LEQ 10 (
    ECHO %AUX% * %num% = %mul%
    GOTO :sumarCont
)

I took the AUX variable out of the IF and now is working as it should. sorry for my bad english.

  • and better `setlocal enabledelayedexpansion for /L %%i in (1,1,10) do (set /a aux=num*%%i & echo %num% * %%i = !aux!) endlocal` – elzooilogico Mar 27 '20 at 15:23