Command Prompt for Windows




LET Examples

Consider the following batch file :-

LET var="Hello, World"
ECHO %var%

Which will set the environment variable var to the string Hello, World, without the double quote characters and display the following :-

Hello, World

Consider the following line :-

LET var=Hello, World

This example will result in a Syntax error, as a comma is not a valid operator. In this case the SET command would be more appropriate.

Command LET is mainly designed to evaluate an expression and set the specified environment variable with the result. Consider the following batch file, which calculates the value of Pi and displays the results :-

@ECHO off

REM
REM Calculate the value of pi using the Gauss-Legendre algorithm
REM

SETLOCAL

LET a=1.0
LET b=1.0 / sqr(2.0)
LET t=1.0 / 4.0
LET p=1.0

FOR i IS 1 to 5 DO {  

	LET aa=(a + b) / 2  
	LET bb=sqr((a * b))  
	LET tt=t - (p * pow(a - aa, 2))  
	LET p=2 * p  
	LET pi=(pow(a + b, 2)) / (4 * t)  
	LET a=aa
	LET b=bb
	LET t=tt
    
	ECHO The calculated value of pi at iteration %i% is %pi%  
}  
ENDLOCAL
ECHO.

Runing this program displays the following :-

The calculated value of pi at iteration 1 is 2.914213562373094924
The calculated value of pi at iteration 2 is 3.140579250522168575
The calculated value of pi at iteration 3 is 3.141592646213542839
The calculated value of pi at iteration 4 is 3.141592653589794004
The calculated value of pi at iteration 5 is 3.141592653589794004

After only 4 iterations, the algorithm has calucated the value of Pi correct to 18 decimal places.