Structure of a program The best way to learn a programming language is by writing programs. Typically, the first program beginners write is a program called 'Hello World', which simply prints 'Hello World' to your computer screen. His is a hard act to follow, and our hope is that Hello World will hold true to the vision behind Switched ON. There are some fantastic features inside Hello World. Educators and teachers will find them invaluable, but we think they’re interesting to anybody into computing in general. Write a program TenHelloWorlds.java that prints 'Hello, World' ten times. Modify UseArgument.java to make a program UseThree.java that takes three names and prints out a proper sentence with the names in the reverse of the order given, so that for example, ' java UseThree Alice Bob Carol ' gives ' Hi Carol, Bob, and Alice.
for Windows10,Windows8,Windows7,VISTA,
XP,NT,Server 2000,Server 2003,Server 2008
- Home
- Source Script
- Tips and Tricks
- Tutorials
- Forum
- General
- Others
DOS - My First Batch File
You have heard a lot about DOS and batch files but you just can't figure out how to create one yourself?
Learn here how to create your first DOS patch program in a few easy steps.
1General
2Create a DOS batch - 'hello.bat'
How To Do Hello World In Eclipse
3Execute a DOS batch - 'hello.bat'
4Summary
5How to continue from here
DOS batch files are written in plain text.Any text editor that can store plain text can be used to create a new DOS batch file. DOS batch files use the file extension .bat or .cmd. In our simple example we will use Notepad to create a batch file right on the desktop. The Notepad program is available on all Windows platforms. | |
Description | Use Notepad to create a new file called hello.bat on your desktop.Add the code shown below to the file and save it. This can be done step by step as follows: |
Do | ·In the Start menu click: Programs - Accessories - Notepad |
The Notepad program starts up showing an empty document with the title: 'Untitled - Notepad' | |
Do | ·In the Notepad menu click:File - Save As |
The Save As dialog shows up | |
Do | ·In the Save As dialog use the Save in drop down box to select: Desktop ·In the File name field overwrite the file name by typing: hello.bat ·Click: Save |
The Save As dialog will close.Notepad shows an empty document with the title: 'hello.bat - Notepad'.A new icon with the name hello.bat appears on the Desktop. | |
Do | ·Type or copy/paste the following code into notepad: |
Code | @echo Hello world. @pause |
Do | ·In the Notepad menu choose: File - Save |
Congrats | Done!Your fist DOS batch file is ready to run. |
Do | ·Minimize all windows and locate the icon called hello.bat on your desktop. ·Double click the hello.bat icon |
A new window will open showing the following message: | |
Hello world. Press any key to continue . . . | |
Do | ·Press any key, e.g. Space |
The window will close. | |
Congrats | You just executed your first DOS batch. |
When double clicking hello.bat on the desktop the file was passed to the DOS command processor.Independent from the executed batch file the command processor will always open a new window.This window will close when the command processor finished executing the batch file. The command processor executes the commands listed in the batch file line by line.Commands in the batch file can instruct the command processor to interact with the user, i.e. to show some text output in the window or to wait for keyboard input. If not instructed differently the command processor echoes each command from the batch to the window before executing it.This can be useful for testing and debugging but is usually annoying in the final version of the batch file and because of this omitted in the example by having the ‘@' sign in front of each line. | |
Inside Hello.bat | The first line instructs the command processor to show the text: 'Hello World.' The second line instructs the command processor to pause till the user hits a key on the keyboard. After pressing a key the batch file finishes and the command processor will close the window. |
Lesson learned | The simple example teaches three features of the command processor. ·The echo command displays text on the screen. ·The pause command causes the command processor to pause until the user hits a key on the keyboard. ·The ‘@' sign at the beginning of a line prevents the command processor to echo the line on the screen while executing. |
Tip | To turn off the echoing of commands for the whole batch file use the '@echo off' command as first command in the batch file.The hello.bat example can be rewritten as follows in order to avoid an ‘@' in front of each line: |
Code | @echo off echo Hello world. pause |
Now you have your first DOS batch running and you may want to know what all you can use it for. A whole group of predefined DOS commands is available with your operating system that can be used to enhance this simple example and make a useful application out of it. To list all predefined dos commands open a command console and type help. This can be done as follows: | |
Do | ·In the Start menu click: Run |
The Run dialog box opens | |
Do | ·In the Open edit box type: cmd.exe |
The command console opens reporting the windows version.A blinking cursor shows up. | |
Do | ·Enter: help and hit theEnter key |
A list of predefined DOS commands with a brief description shows up.Followed by the blinking curser waiting for more input. | |
Do | ·Pick any dos command from the list.Type help and the DOS command to get more information about it's usage.I.e. type: help echo and hit Enter to get the details about the echo command |
Example | C:>help echo Displays messages, or turns command-echoing on or off. ECHO [ON | OFF] ECHO [message] Type ECHO without parameters to display the current echo setting. C:> |
Lesson learned | There is a whole list of predefined DOS commands available.Those DOS commands can be combined in a batch file in order to build a useful DOS batch application. |
F# and the Visual F# tooling are supported in the Visual Studio for Mac IDE. Ensure that you have Visual Studio for Mac installed.
Creating a console application
One of the most basic projects in Visual Studio for Mac is the Console Application. Here's how to do it. Once Visual Studio for Mac is open:
- On the File menu, point to New Solution.
- In the New Project dialog, there are 2 different templates for Console Application. There is one under Other -> .NET which targets the .NET Framework. The other template is under .NET Core -> App which targets .NET Core. Either template should work for the purpose of this article.
- Under console app, change C# to F# if needed. Choose the Next button to move forward!
- Give your project a name, and choose the options you want for the app. Notice, the preview pane to the side of the screen that will show the directory structure that will be created based on the options selected.
- Click Create. You should now see an F# project in the Solution Explorer.
Writing your code
Let's get started by writing some code first. Make sure that the
Program.fs
file is open, and then replace its contents with the following:In the previous code sample, a function
square
has been defined which takes an input named x
and multiplies it by itself. Because F# uses Type Inference, the type of x
doesn't need to be specified. The F# compiler understands the types where multiplication is valid, and will assign a type to x
based on how square
is called. If you hover over square
, you should see the following:This is what is known as the function's type signature. It can be read like this: 'Square is a function which takes an integer named x and produces an integer'. Note that the compiler gave
square
the int
type for now - this is because multiplication is not generic across all types, but rather is generic across a closed set of types. The F# compiler picked int
at this point, but it will adjust the type signature if you call square
with a different input type, such as a float
.Another function,
main
, is defined, which is decorated with the EntryPoint
attribute to tell the F# compiler that program execution should start there. It follows the same convention as other C-style programming languages, where command-line arguments can be passed to this function, and an integer code is returned (typically 0
).It is in this function that we call the
square
function with an argument of 12
. The F# compiler then assigns the type of square
to be int -> int
(that is, a function which takes an int
and produces an int
). The call to printfn
is a formatted printing function which uses a format string, similar to C-style programming languages, parameters which correspond to those specified in the format string, and then prints the result and a new line.Running your code
You can run the code and see results by clicking on Run from the top level menu and then Start Without Debugging. This will run the program without debugging and allows you to see the results.
You should now see the following printed to the console window that Visual Studio for Mac popped up:
Congratulations! You've created your first F# project in Visual Studio for Mac, written an F# function printed the results of calling that function, and run the project to see some results.
How To Do Hello World In F# Visual Studio For Mac
Using F# Interactive
One of the best features of the Visual F# tooling in Visual Studio for Mac is the F# Interactive Window. It allows you to send code over to a process where you can call that code and see the result interactively.
To begin using it, highlight the
square
function defined in your code. Next, click on Edit from the top level menu. Next select Send selection to F# Interactive. This executes the code in the F# Interactive Window. Alternatively, you can right click on the selection and choose Send selection to F# Interactive. You should see the F# Interactive Window appear with the following in it:This shows the same function signature for the
square
function, which you saw earlier when you hovered over the function. Because square
is now defined in the F# Interactive process, you can call it with different values:This executes the function, binds the result to a new name
it
, and displays the type and value of it
. Note that you must terminate each line with ;;
. This is how F# Interactive knows when your function call is finished. You can also define new functions in F# Interactive:The above defines a new function,
isOdd
, which takes an int
and checks to see if it's odd! You can call this function to see what it returns with different inputs. You can call functions within function calls:You can also use the pipe-forward operator to pipeline the value into the two functions:
The pipe-forward operator, and more, are covered in later tutorials.
This is only a glimpse into what you can do with F# Interactive. To learn more, check out Interactive Programming with F#.
Next steps
If you haven't already, check out the Tour of F#, which covers some of the core features of the F# language. It will give you an overview of some of the capabilities of F#, and provide ample code samples that you can copy into Visual Studio for Mac and run. There are also some great external resources you can use, showcased in the F# Guide.