how to program
HOW TO PROGRAM . IT - learn how to program - online interactive programming tutorial and programming course
Getting User Input

08. So far the programs we have created have all been hard-coded, as in what they do and what they output is dictated by the program code. Once we can start getting user input into the program, our programs can be made flexible enough to use this input...

Program Code:

This example differs from the previous example in one way. Rather than assigning the variable with a hardcoded value as in the string "Fred", we are calling a programming environment input function which prompts and collects this information from the user.

   central core language and auxiliary language environment
As before it is important to stress that the environment's input function will be different for each programming environment you develop for. If you are programming server-side pages (writing programs that run on your server) on your website then there is no keyboard or screen or user sitting at your server, instead you might send use input sent to the server via the client browser (e.g. using form post variables in PHP, or request form objects in ASP.NET). If you are programming for the client side web browser environment using JavaScript, then the input function might be window.prompt. If you are programming for event driven windows based graphical environment, then the input might be very different again.

You might also want to write programs that have no interactive user input (e.g. overnight processing programs), in this case you might only get input from text files.


1. Coment out the whole program using block comments /* and */. Then after the comment block add the line env.out( "Hello " + env.userinput( "What is your name?" ) ); - since the input function returns a string it can be used directly where strings are added in the expression used for the output parameter value. Once you have done this, delete the line and remove your commenting out, in order to restore the program to its original state (you might be able to do this by pressing UNDO e.g. CTRL-Z, press CTRL and Z key at the same time).
2. Change the program to use 2 variables, strNameMan, and strNameWoman, ask each person's name and output text using both of those variables.
3. Following on from exercise 2, introduce a third variable strNameCouple which is assigned a value of strNameMan + " and " + strNameWoman, and use this variable in the output statements

To return to previous page click here

by running the tutorial programs on this site, you are agreeing to our terms and conditions (please check these first).
Thu 27 Jan 2022 web design | login © 2022 Abstract Worlds Ltd