Date/Time:
A if statement allows you to choose in any program, and the logic is very similar to how we use if in the English language. If I want this then I will get that. As you can see on the flow chart, if I choose this then it goes throw, or else it goes somewhere else. We can also make a Boolean(True or False) selections with a if and else statement. If the statement is “True”, it will go to true, else it will go to false.
In batch programming, we would write a script like this in this way:
@echo off
:begin
echo 1 – [Go to Next]
echo.
set /p entry=
if %entry% == 1 (
goto new
) else (
goto begin
)
:new
echo I went to next.
(In this simple program, the user can pick 1 to go to next, or if they press anything else they will go back to begin.)
(True and False)
@echo off
:start
echo A – (True)
echo.
set /p entry=
if %entry% == A (
goto True
) else (
goto false
)
:True
echo True
exit
:False
echo False
exit
(In this program, if I select A it will go to true, and if I select anything else it will go to false.)
The flow chart you see in the above diagram is a simple if statement flow chart. In that flow chart, you can pick a number of different outcomes, and we call this a nested if statement. If what you entered in the program does not pass any if statement then it goes to the else statement at the end.
I will write a simple program in batch programming to demonstrate this nested if statement:
@echo off
:start
echo A – [Go to A]
echo B – [Go to B]
echo C – [Go to C]
echo.
set /p Entered=
if %Entered% == A (
goto A_Statement
) else if %Entered% == B (
goto B_Statement
) else if %Entered% == C (
goto C_Statement
) else (
goto start
)
:A_Statement
echo I went to A statement
exit
:B_Statement
echo I went to B statement
exit
:C_Statement
echo I Went to C statement
exit
In this program the user is given a choice to choose from A to C. If they choose A it goes to A_statement, if they choose B it goes to B_statement, if they choose C it goes to C_statement, or else it goes back to start or beginning. This program will not be able to complete unless you pick A, B, or C.
We can write this else if program in another way using numbers.
@echo off
:start
echo 0 – [Go to statement_A]
echo 1 – [Go to statement_B]
echo 2 – [Go to statement_C]
echo.
set /p Entered=
if %Entered% LEQ 2 (
goto Part_%Entered%
) else (
goto start
)
:Part_0
echo I went to A statement
exit
:Part_1
echo I went to B statement
exit
:Part_2
echo I Went to C statement
exit
One of the advantages of using numbers is that, we don’t have to use the equals(==) sign. We can use numbers to make selections to shorten the code. In the code above I put if %Entered% LEQ 2, and this simply means if the code is less than equal to 2. Less than equal to two means 0, 1, and 2, because zero is counted as a number. If I select 0, it goes to statement_A, if I select 1, it goes to statement_B, like the previous program. The nested if statement is easy to understand if you draw a flow chart, and in programming we do draw out flow charts when we write complicated programs.