Login Register






Batch. The Ultimate Tutorial. filter_list
Author
Message
RE: Batch. The Ultimate Tutorial. #24
Quote:[spoiler=4. Conditional Statements]

Conditional statements can be very complicated to a newbie in programming languages. But luckily, in Batch, conditional statements are only sand compared to the full beaches of C, C++, Python, and other various Programming Languages.

Try this code!

Code:
@echo off :question echo What's 1+1? set /p input= if '%input%' = '2' goto correct if not '%input%' = '2' goto false :correct cls echo Good job! pause :false echo You are one dumbass. echo Try again echo. goto question

Let's analyze the code!

if: Tells the interpreter to do a command if certain conditions are met. The main conditional statement. What's the variable being used? "Input"! That's right! The ' are optional but highly recommended.

if not: Tells the interpreter to do a command if certain conditions are NOT met.

echo.: Just makes a blank line.

cls: Clears the screen.

[/spoiler]

That is wrong. For a stickied thread there should be no errors in the code lol, but there is unfortunately.

Code:
@echo off :question echo What's 1+1? set /p input= if '%input%' = '2' goto correct if not '%input%' = '2' goto false :correct cls echo Good job! pause :false echo You are one dumbass. echo Try again echo. goto question

You use "=" instead of "==" on these lines:
Code:
if '%input%' = '2' goto correct if not '%input%' = '2' goto false

And why not demonstrate the else part?

Another error I see in this is that, even if you get it correct, there's no goto to define where to go after :correct, meaning it goes to :false right after displaying the correct message, displaying a false echo after saying "Good job!" lol

This:
Code:
:correct cls echo Good job! pause

Should have been:
Code:
:correct cls echo Good job! pause goto:question

And if you wanted a better demonstration of an if logic statement I would have written that like this:
Code:
@echo off echo What is 1 + 1? set /p input= if "%input%"=="2" ( echo Correct! ) else ( echo Wrong... ) pause exit

Please correct the errors.
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply





Messages In This Thread
Batch. The Ultimate Tutorial. - by Reverence - 07-05-2011, 07:05 PM