tag:blogger.com,1999:blog-66116636795457112622024-02-19T08:18:56.062-08:00Hello World Africa.Open TechEd for all - changing the world one line of code at a time...Mohammed E. Katzhttp://www.blogger.com/profile/16957627613529515712noreply@blogger.comBlogger62125tag:blogger.com,1999:blog-6611663679545711262.post-48593625497189428392012-08-28T02:58:00.002-07:002013-10-10T01:01:49.901-07:00Android App Development Tutorial<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijytbZ61FoUIAqN8W_lTRHsWRyeSvtWGN_YwcfW4aRGJinfRdFdnYkh_OEBq0bF84yBv2jksLcW_QHn6GAouVAQmlVoFmbN_GFXJEJjchyMx7f6DVSWAD9GUbmfz42eTxcsSqer5hEipnk/s1600/Android.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="300" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijytbZ61FoUIAqN8W_lTRHsWRyeSvtWGN_YwcfW4aRGJinfRdFdnYkh_OEBq0bF84yBv2jksLcW_QHn6GAouVAQmlVoFmbN_GFXJEJjchyMx7f6DVSWAD9GUbmfz42eTxcsSqer5hEipnk/s400/Android.gif" width="400" /></a></div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">This tutorial is meant to target devlopers who have little or no experience in Android or C# development. If you are brand new in programming you will probably be able to follow but you will gain a lot from reading up on basic programming concepts such as classes, objects, class inheritance and events. If you are experienced you will know what parts to skip :-)</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">To get started with programming for the Android platform we are going to make a very simple application. Though, it will be a bit more advanced than the barebone "Hello World"-app or the simple starter app that is being generated automatically by the development framework when creating a new Android project.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<br />
<h2>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif; font-size: x-large;">Our Basic application</span></h2>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">The app we are gong to build is a reaction tester. A program that will count how many millisconds it takes you to press a button after you have been given the "go" signal. In this first tutorial we will keep it very simple and then we will add more feaures in the upcoming tutorials in our Android App Development-series.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">The program will be doing the following: When it loads it will alert the user to be ready to react. Seconds later a "Go!" message will be given signalling the user to react. The user reacts by pressing a button on the screen. The instant the button is pressed the program will calculate the time from the signal was given until the button was pressed. The resulting time will be displayed on the screen (and the user will be able to start the process again to see if he can beat his previous time).</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<br />
<h3>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Setting up </span><span style="font-family: 'Courier New', Courier, monospace;">namespaces</span></h3>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Our program starts out like this:</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<br />
<span style="font-family: 'Courier New', Courier, monospace;">using System;</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"><br /></span>
<span style="font-family: 'Courier New', Courier, monospace;">using Android.App;</span><br />
<span style="font-family: 'Courier New', Courier, monospace;">using Android.Content;</span><br />
<span style="font-family: 'Courier New', Courier, monospace;">using Android.Runtime;</span><br />
<span style="font-family: 'Courier New', Courier, monospace;">using Android.Views;</span><br />
<span style="font-family: 'Courier New', Courier, monospace;">using Android.Widget;</span><br />
<span style="font-family: 'Courier New', Courier, monospace;">using Android.OS;</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"><br /></span>
<span style="font-family: 'Courier New', Courier, monospace;">namespace ReactionTimer</span><br />
<span style="font-family: 'Courier New', Courier, monospace;">{</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">First we are applying the using directive to be able to reference different elements of the Mono (for Android) Framework that we will be needing for our program. For instance we are going to use a Button which is a specific graphical element with a specific set of features and which is drawn on the screen in a specific way by the Android device. A Button is located in the Android.Widget namespace. A namespace is simply a way of dividing code into logical units giving it a proper a structure that allows programmers to find what they need (easily, hopefully). Without our using directive we would need to write Android.Widget.Button in order to reference the needed class in the Mono Framework. Now, we can instead just write Button in our code. When the program is prepared for execution (compiled) the compiler will know what type of button we are referring to and where to find it.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">In the same way we are also referring the System namespace (in the very first line) which is not a reference to Android specific code elements but to general code from the .NET Framework (now in its Mono implementation as we are using the Mono Framework).</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">After that on line 10 we are creating our very own name space (yay, our first piece of original code) ReactionTimer. As we only have one main file and one namespace we do not have any special benefits from this. However, as our application grows, making use of cleverly named spaces can be quite useful.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<br />
<h3>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">The Android Activity class</span></h3>
<br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Next we come to our first class Activity1 which inherits from the Activity class in the Android.App namespace</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">See the piece of code below:</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<br />
<span style="font-family: 'Courier New', Courier, monospace;">[Activity(Label = "Reaction Tester", MainLauncher = true, Icon = "@drawable/icon")]</span><br />
<span style="font-family: 'Courier New', Courier, monospace;">public class Activity1 : Activity</span><br />
<span style="font-family: 'Courier New', Courier, monospace;">{</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">An activity on Android is logical construct (besides being a class) that refers to something the user can do. For at user to do something a user interface (UI)is required. Thus, a primary function of an Activity is to control a window on which graphical items (UI-elements) are placed for the the user to interact with.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">In our application our activity is to carry our a reaction as fast as possible. This requires only a single screen and so we only have one Activity class. If we needed multiple screens we would create multiple classes. Instaead of Activity1 we could have named our activity differently - for exaple "ReactionScreen".</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Just above our Activity class we see what is called an attribute. An attribute is not part of the code itself and is not influencing the program when it is running. Rather it is a form of annotation that can be used by the programming environment when the code is being processed and made into an executable form.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">In our case we have an ActivityAttribute and it has some parameters called Label, MainLauncher and Icon. These attributes are being used by the Mono framework and what happens specifically is that during the build process the Mono framework looks for all the class attributes and uses the data found in order to generate an XML file. This resulting file is called "AndroidManifest.xml" and is a file that needs to be included within every Android application. It is a kind of settings file that tells Android something about the program and how to deal with it. In our case our device will know that the label to use for our activity is "Reaction Tester" (will be shown on the screen when the app is running), that our activity class is to be launched when the program starts, and it knows where to find the icon to associate with our program.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<br />
<h3>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Content of the Activity class</span></h3>
<br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Our Activity class contains the following code:</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"><br />
</span><br />
<span style="font-family: 'Courier New', Courier, monospace;">private ChangeStateTimer timer;</span><br />
<span style="font-family: 'Courier New', Courier, monospace;">private State state;</span><br />
<span style="font-family: 'Courier New', Courier, monospace;">private long startTime;</span><br />
<span style="font-family: 'Courier New', Courier, monospace;">private Button button;</span><br />
<span style="font-family: 'Courier New', Courier, monospace;">private TextView label;</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"><br /></span>
<span style="font-family: 'Courier New', Courier, monospace;">protected override void OnCreate(Bundle bundle)</span><br />
<span style="font-family: 'Courier New', Courier, monospace;">{</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"> base.OnCreate(bundle);</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"><br /></span>
<span style="font-family: 'Courier New', Courier, monospace;"> // Set our view from the "main" layout resource</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"> SetContentView(Resource.Layout.Main);</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"><br /></span>
<span style="font-family: 'Courier New', Courier, monospace;"> // Get our button from the layout resource and attach an event to it</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"> this.button = FindViewById<Button>(Resource.Id.buttonMain);</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"> this.button.Click += new EventHandler(HandleUserReaction);</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"> //Ensure the button cannot be pressed until we need it to</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"> this.button.Enabled = false;</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"> //Get a reference to the label from the layout resource</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"> this.label = FindViewById<TextView>(Resource.Id.labelResult);</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"><br /></span>
<span style="font-family: 'Courier New', Courier, monospace;"> this.state = State.Stopped;</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"> this.timer = new ChangeStateTimer(10000, 2000, this);</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"> this.timer.Start();</span><br />
<span style="font-family: 'Courier New', Courier, monospace;">}</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">First we have five variables (called Fields in C#). The two first are used to hold references to a timer that we will need and to the state we discussed before. The third of them will hold the time (measure in milliseconds) for when the "go"-signal was given so that we can use it later to see how much time passed until the user pressed the button. Finally, the two last fields will be referring to our two UI-items which is a Button and a TextView (which we will be referring to as a "label").</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Next we have a method that is being called automatically by the system once the program has started and our Activity has been created. This is the way that Android informs us that "Ok, now your program has started and I am going to turn it over to you now. So, get ready.". In this method we have to prepare a few things so that our program can later function as we intend.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Well, first we have to do something for Android which is to call the OnCreate method on the base class. This is so that the Activity class can do it's magic (setting up something that needs to be set-up for an Android program to run - which we do not need to concern ourselves with).</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">The first major thing we do is to load our user interface by calling the SetContentView method of our activity passing it our layout as a parameter. Our layout is stored in an XML template file called "Main" and located amongst our program Resources in the "Layout" folder (see file content later). It needs to be connected to our activity so that it will display the UI-elements we have marked up there.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Then we locate our button and label in our template and save a reference to them so that we can do something with them later. Also, we attach an event handler to the Click event of the button. This is done by passing the name of our method - which is HandleUserReaction to a so-called delegate.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">A delegate is specific concept in C# that acts as a form of a contract. The purpose is to constrict exactly what form a method should have in order to be called by a particular event. The event in this case is the Click event of a button. From documentation we know that it is the EventHandler delegate that holds the contract related this event. So, as long as our method conforms to the contract laid out by this particular delegate we can be sure that it can be executed when the Click event of the button has been triggered.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Now we set the state of our application (see more below). At this point we want to remember that we are not currently Running (but rather Stopped) meaning that we have not yet started the experiment of seeing how fast the user can press the button after we tell him to.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Lastly, we create a new instance of our timer. We give it a few parameters with the most interesting being the first one which is used to set how many milliseconds the countdown should last. A deeper description will follow a few paragraphs down.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<br />
<h2>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Creating state for our application</span></h2>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Next we make use of the enumeration data type (created with the enum keyword) containing two items. These are states which we will logically have to handle in our application and with our enum we are able to asily assign and retrieve them.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">What the states are and their purpose should be evident from the code and the comments:</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<br />
<div class="line number41 index3 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-color: white !important; background-image: none !important; background-origin: initial !important; border-bottom-left-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-width: 0px !important; border-color: initial !important; border-image: initial !important; border-left-width: 0px !important; border-right-width: 0px !important; border-style: initial !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-top-width: 0px !important; bottom: auto !important; box-sizing: content-box !important; float: none !important; height: auto !important; left: auto !important; margin-bottom: 0px !important; margin-left: 0px !important; margin-right: 0px !important; margin-top: 0px !important; min-height: inherit !important; outline-color: initial !important; outline-style: initial !important; outline-width: 0px !important; overflow-x: visible !important; overflow-y: visible !important; padding-bottom: 0px !important; padding-left: 1em !important; padding-right: 1em !important; padding-top: 0px !important; position: static !important; right: auto !important; text-align: -webkit-auto; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<code class="csharp plain" style="background-attachment: initial !important; background-clip: initial !important; background-color: initial !important; background-image: none !important; background-origin: initial !important; border-bottom-left-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-width: 0px !important; border-color: initial !important; border-image: initial !important; border-left-width: 0px !important; border-right-width: 0px !important; border-style: initial !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-top-width: 0px !important; bottom: auto !important; box-sizing: content-box !important; float: none !important; height: auto !important; left: auto !important; margin-bottom: 0px !important; margin-left: 0px !important; margin-right: 0px !important; margin-top: 0px !important; min-height: inherit !important; outline-color: initial !important; outline-style: initial !important; outline-width: 0px !important; overflow-x: visible !important; overflow-y: visible !important; padding-bottom: 0px !important; padding-left: 0px !important; padding-right: 0px !important; padding-top: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><span style="font-family: 'Courier New', Courier, monospace;">private enum State {<br />
Running,//The test is running and we await the reaction from the user<br />
Stopped//The test is not running (which will be the case at start-up or after the user has reacted)<br />
}</span></code></div>
<div class="line number41 index3 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-color: white !important; background-image: none !important; background-origin: initial !important; border-bottom-left-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-width: 0px !important; border-color: initial !important; border-image: initial !important; border-left-width: 0px !important; border-right-width: 0px !important; border-style: initial !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-top-width: 0px !important; bottom: auto !important; box-sizing: content-box !important; float: none !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 14px; margin-bottom: 0px !important; margin-left: 0px !important; margin-right: 0px !important; margin-top: 0px !important; min-height: inherit !important; outline-color: initial !important; outline-style: initial !important; outline-width: 0px !important; overflow-x: visible !important; overflow-y: visible !important; padding-bottom: 0px !important; padding-left: 1em !important; padding-right: 1em !important; padding-top: 0px !important; position: static !important; right: auto !important; text-align: left; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span></div>
<br />
<h3>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">The timer class</span></h3>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Explanation of the timer class to follow</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"><br />
</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"> private class ChangeStateTimer : CountDownTimer</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"> {</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"> Activity1 activity;</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"><br /></span>
<span style="font-family: 'Courier New', Courier, monospace;"> public ChangeStateTimer(long finalTime, long intervals, Activity1 activity)</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"> : base(finalTime, intervals)</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"> {</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"> this.activity = activity;</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"> }</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"><br /></span>
<span style="font-family: 'Courier New', Courier, monospace;"> public override void OnFinish()</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"> {</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"> this.activity.GiveStartSignal();</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"> }</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"><br /></span>
<span style="font-family: 'Courier New', Courier, monospace;"> public override void OnTick(long millisUntilFinished)</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"> {</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"> //Not using this method as we are not using the intervals </span><br />
<span style="font-family: 'Courier New', Courier, monospace;"> }</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"> }</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"> }</span><br />
<span style="font-family: 'Courier New', Courier, monospace;">}</span><br />
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<div>
<h3>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Ready, set, Go!</span></h3>
</div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Next we have the GiveStartSignal method that will be called by the timer once it has finished counting down. At this point we are ready to time the reaction of the user.</span></div>
</div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;">private void GiveStartSignal()</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;">{</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> this.label.Text = "Waiting for your reaction.";</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> this.button.Text = "Now!";</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> this.button.Enabled = true;</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> this.state = State.Running;</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> this.startTime = SystemClock.CurrentThreadTimeMillis();</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;">}</span></div>
</div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">To alert the user that we are awaiting his reaction we change the text of our label to "Waiting for your reaction.". Also, we change the text of our button to "Now!". More importantly we change the Enabled state of the button to true so that it will actually function.</span></div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">We then set the state to Running and register the current time. Note, that it is not the actual time of day that we are recording but the time that has passed since our application has been started (measured in milliseconds). This is totally fine because we are not concerned about the actual time but only interested in being able calculate the difference between "now" and the point at which the user will have pressed the button.</span></div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">You might have noticed that it says CurrentThreadTimeMillis. So, technically we are not necessarily getting the time elapsed since the start of the application itself but rather that of the thread that is handling our application. A thread is a mechanism through which the processer is able to prioritize the execution of different programs (and different parts of a single program in case of multi-threaded programs).</span></div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">So, now the "click is ticking" and the program is waiting for the user to press the "reaction"-button - which brings us to the last part of our code.</span></div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<h3>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Handling states</span></h3>
</div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Finally, we come to the method we assigned to the Click event of our button. Looking throug the code you will see that is uses our State enum to check what part of the code in the method to execute:</span></div>
</div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;">private void HandleUserReaction(Object sender, EventArgs e)</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;">{</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> if (this.state == State.Running)</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> {</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> //We have been waiting for a reaction and now we need to see how long it took</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> long reactionTime = SystemClock.CurrentThreadTimeMillis() - this.startTime;</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> this.label.Text = "You reacted in: " + reactionTime.ToString() + " milliseconds.";</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> this.button.Text = "Restart";</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> this.state = State.Stopped;</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> }</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> else if (this.state == State.Stopped)</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> {</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> //State is stopped and so a new click means we have to start over </span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> this.label.Text = "Stand by for the button to signal a start";</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> this.button.Text = "Stand by ...";</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> this.button.Enabled = false;//Button must not be clickable now because we await for system to give the signal to the user</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> this.timer.Start();</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> }</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;">}</span></div>
</div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">First we check if we are in the Running state. If this is the case the user has just reacted and pressed the button as fast as humanly possible.</span></div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Therefore, we look at the current point in time again - and subtract the value we stored previously when the "go"-signal was given. Then we graciously present the user with the fact that it takes him just about half a second to press a button.</span></div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Lastly, we change the text of the button to "Restart" so the user can see that the way to beat himself trying to squeeze the reaction down to a quarter of a second is to once again press the same darn button. For that to work we also change the state to Stopped so that our program will execute the other part of the method next time it is called.</span></div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">when the other part, and the last part of our program, is run, we know that the user has pressed the button again and we know that the state is Stopped. Consequently, what we must do is to start the timer once again so it can make a new countdown towards a new "go"-signal.</span></div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Also, we disable the button so that our user can see that the button is not currently to be pressed (if it was the timer would simply be restarted). As you test the program you might see that the user can cheat by hammering the button repeatedly hoping to trigger it the very instant it is re-enabled when the new signal is given. This problem we will tackle in the next tutorial where we will generally look at how we can enhance the current version.</span></div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<h2>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">The XML User Interface</span></h2>
</div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Our graphical elements are contained in an XML file that acts as a template for our program. The UI-items can also be added to a screen directly through code but since the programming framework automactially generates a layout file as part of the model application we will stick to this way of going about it. Also, it is generaly good programming practice to separate programming logic from UI-layout.</span></div>
</div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"><?xml version="1.0" encoding="utf-8"?></span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> android:orientation="vertical"</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> android:layout_width="fill_parent"</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> android:layout_height="fill_parent"</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> ></span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> <Button </span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> android:id="@+id/buttonMain"</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> android:layout_width="fill_parent"</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> android:layout_height="wrap_content"</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> android:text=""</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> /></span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> </span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> <TextView</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> android:id="@+id/labelResult"</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> android:text=""</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> android:layout_width="fill_parent"</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> android:layout_height="wrap_content"</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> /></span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"> </LinearLayout></span></div>
</div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">In the file we see that we have our Button and Textview and that they are contained in a LinearLayout. The tags all refer to classes in the Android Framework and LinearLyout is a class responsible for grouping controls and laying them out on the screen in a particular (liniear) way ("linear" means that the controls will be positioned one after another).</span></div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Besides the orientation of the layout which is set to "vertical" we see that special attributes are used for assigning height and width. The value "fill_parent" means that the control will take up as much space as it can limited only by its containing parent - while "wrap_content" means that the control will extend as much as possible to contain the content it holds (in our case the button and label will be as high as needed in order to contain the text we need them to show).</span></div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Now we have a working Android application. Good luck, keep studying and practising. </span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">As always, comments and questions are welcome, we'll try our best to answer them when we can. </span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Helpful and productive suggestions and corrections, if any are needed are very much appreciated through our <a href="http://hello-world-africa.blogspot.com/p/contact-us.html" target="_blank">contact page</a>.</span></div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span></div>
</div>
Mohammed E. Katzhttp://www.blogger.com/profile/16957627613529515712noreply@blogger.com1tag:blogger.com,1999:blog-6611663679545711262.post-14009030156899132482012-08-28T02:56:00.004-07:002012-08-28T02:57:39.887-07:00Pascal Programming Lesson #14 - Linked Lists<br />
<div style="background-color: white; color: #000033; font-family: sans-serif;">
A linked list is like an array except that the amount of elements in a linked list can change unlike an array. A linked list uses pointers to point to the next or previous element.</div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Single linked lists</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
There are 2 types of single linked lists which are called queues and stacks.</div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Queues</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
A queue is like standing in a queue at a shop. The first person that joins a queue is the first person to be served. You must always join at the back of a queue because if you join at the front the other people will be angry. This is called FIFO(First In First Out).</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Item 1 --> Item 2 --> Item 3 --> (Until the last item)</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Each item of a linked list is a record which has the data and a pointer to the next or previous item. Here is an example of how to declare the record for a queue and a pointer to a queue record as well as the variables needed:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program queue;<br /><br />type<br /> pQueue = ^tqueue;<br /> tQueue = record<br /> data: integer;<br /> next: pQueue;<br /> end;<br /><br />var<br /> head, last, cur: pQueue;<br /><br />begin<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
We will now make 3 procedures. The first procedure will add items to the list, the second will view the list and the third will free the memory used by the queue. Before we make the procedures lets first take a look at the main program.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">begin<br /> head := nil; <dfn style="color: #666666;">{Set head to nil because there are no items in the queue}</dfn><br /> add(1) <dfn style="color: #666666;">{Add 1 to the queue using the add procedure}</dfn>;<br /> add(2);<br /> add(3);<br /> view; <dfn style="color: #666666;">{View all items in the queue}</dfn><br /> destroy; <dfn style="color: #666666;">{Free the memory used by the queue}</dfn><br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The add procedure will take an integer as a parameter and add that integer to the end of the queue.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">procedure add(i: integer);<br />begin<br /> new(cur); <dfn style="color: #666666;">{Create new queue item}</dfn><br /> cur^.data := i; <dfn style="color: #666666;">{Set the value of the queue item to i}</dfn><br /> cur^.next := nil; <dfn style="color: #666666;">{Set the next item in the queue to nil because it doesn't exist}</dfn><br /> if head = nil then <dfn style="color: #666666;">{If there is no head of the queue then}</dfn><br /> head := cur <dfn style="color: #666666;">{Current is the new head because it is the first item being added to the list}</dfn><br /> else<br /> last^.next := cur; <dfn style="color: #666666;">{Set the previous last item to current because it is the new last item in the queue}</dfn><br /> last := cur; <dfn style="color: #666666;">{Make the current item the last item in the queue}</dfn><br />end;</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The view procedure uses a loop to display the data from the first item to the last item of the queue.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">procedure view;<br />begin<br /> cur := head; <dfn style="color: #666666;">{Set current to the beginning of the queue}</dfn><br /> while cur <> nil do <dfn style="color: #666666;">{While there is a current item}</dfn><br /> begin<br /> writeln(cur^.data); <dfn style="color: #666666;">{Display current item}</dfn><br /> cur := cur^.next; <dfn style="color: #666666;">{Set current to the next item in the queue}</dfn><br /> end;<br />end;</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The destroy procedure will free the memory that was used by the queue.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">procedure destroy;<br />begin<br /> cur := head; <dfn style="color: #666666;">{Set current to the beginning of the queue}</dfn><br /> while cur <> nil do <dfn style="color: #666666;">{While there is a item in the queue}</dfn><br /> begin<br /> cur := cur^.next; <dfn style="color: #666666;">{Store the next item in current}</dfn><br /> dispose(head); <dfn style="color: #666666;">{Free memory used by head}</dfn><br /> head := cur; <dfn style="color: #666666;">{Set the new head of the queue to the current item}</dfn><br /> end;<br />end;</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Stacks</h2>
<div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
To understand a stack you must think about a stack of plates. You can add a plate to the top of the stack and take one off the top but you can't add or take away a plate from the bottom without all the plates falling. This is called LIFO(Last In First Out).</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Item 1 <-- Item 2 <-- Item 3 <-- (Until the last item)</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
When you declare the record for a stack item you must use previous instead of next. Here is an example.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program stack;<br /><br />type<br /> pStack = ^tStack;<br /> tStack = record<br /> data: integer;<br /> prev: pStack;<br /> end;<br /><br />var<br /> last, cur: pStack;<br /><br />begin<br /> last := nil;<br /> add(3);<br /> add(2);<br /> add(1);<br /> view;<br /> destroy;<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You will see that the numbers are added from 3 to 1 with a stack instead of 1 to 3. This is because things must come off the top of the stack instead of from the head of a queue.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The add procedure adds the item after the last item on the stack.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">procedure add(i: integer);<br />begin<br /> new(cur); <dfn style="color: #666666;">{Create new stack item}</dfn><br /> cur^.data := i; <dfn style="color: #666666;">{Set item value to the parameter value}</dfn><br /> cur^.prev := last; <dfn style="color: #666666;">{Set the previous item to the last item in the stack}</dfn><br /> last := cur; <dfn style="color: #666666;">{Make the current item the new last item}</dfn><br />end;</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The view and destroy procedures are almost the same as with a queue so they will not need to be explained.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">procedure view;<br />begin<br /> cur := last;<br /> while cur <> nil do<br /> begin<br /> writeln(cur^.data);<br /> cur := cur^.prev;<br /> end;<br />end;<br /><br />procedure destroy;<br />begin<br /> while last <> nil do<br /> begin<br /> cur := last^.prev;<br /> dispose(last);<br /> last := cur;<br /> end;<br />end;</code></div>
</div>
Mohammed E. Katzhttp://www.blogger.com/profile/16957627613529515712noreply@blogger.com0tag:blogger.com,1999:blog-6611663679545711262.post-69258842935766941492012-08-28T02:53:00.004-07:002012-08-28T02:57:39.933-07:00Pascal Programming Lesson #13 - Pointers<br />
<div style="background-color: white; color: #000033; font-family: sans-serif;">
A pointer is a type of variable that stores a memory address. Because it stores a memory address it is said to be pointing to it. There are 2 types of pointers which are typed and untyped. A typed pointer points to a variable such as an integer. An untyped pointer can point to any type of variable.</div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Declaring and using typed pointers</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
When you declare a typed pointer you must put a ^ in front of the variable type which you want it to point to. Here is an example of how to declare a pointer to an integer:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Pointers;<br /><br />var<br /> p: ^integer;<br /><br />begin<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The @ sign can be used in front of a variable to get its memory address. This memory address can then be stored in a pointer because pointers store memory addresses. Here is an example of how to store the memory address of an integer in a pointer to an integer:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Pointers;<br /><br />var<br /> i: integer;<br /> p: ^integer;<br /><br />begin<br /> p := @i;<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
If you want to change the value stored at the memory address pointed at by a pointer you must first dereference the pointer variable using a ^ after the pointer name. Here is an example of how to change the value of an integer from 1 to 2 using a pointer:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Pointers;<br /><br />var<br /> i: integer;<br /> p: ^integer;<br /><br />begin<br /> i := 1;<br /> p := @i;<br /> p^ := 2;<br /> writeln(i);<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You can allocate new memory to a typed pointer by using the <em>new</em> command. The <em>new</em> command has one parameter which is a pointer. The <em>new</em> command gets the memory that is the size of the variable type of the pointer and then sets the pointer to point to the memory address of it. When you are finished using the pointer you must use the <em>dispose</em> command to free the memory that was allocated to the pointer. Here is an example:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Pointers;<br /><br />var<br /> p: ^integer;<br /><br />begin<br /> new(p);<br /> p^ := 3;<br /> writeln(p^);<br /> dispose(p);<br />end.</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Declaring and using untyped pointers</h2>
<div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
When you declare an untyped pointer you must use the variable type called <em>pointer</em>.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Pointers;<br /><br />var<br /> p: pointer;<br /><br />begin<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
When you allocate memory to an untyped pointer you must use the <em>getmem</em> command instead of the <em>new</em>command and you must use <em>freemem</em> instead of <em>dispose</em>. <em>getmem</em> and <em>freemem</em> each have a second parameter which is the size in bytes of the amount of memory which must be allocated to the pointer. You can either use a number for the size or you can use the <em>sizeof</em> function to get the size of a specific variable type.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Pointers;<br /><br />var<br /> p: pointer;<br /><br />begin<br /> getmem(p,sizeof(integer));<br /> freemem(p,sizeof(integer));<br />end.</code></div>
</div>
<center style="background-color: white; color: #000033; font-family: sans-serif;">
<ins style="border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; display: inline-table; height: 280px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: relative; visibility: visible; width: 336px;"></ins></center>
Mohammed E. Katzhttp://www.blogger.com/profile/16957627613529515712noreply@blogger.com0tag:blogger.com,1999:blog-6611663679545711262.post-40045847137351147472012-08-28T02:52:00.000-07:002012-08-28T02:57:39.916-07:00Pascal Programming Lesson #12 - Units<span style="background-color: white; color: #000033; font-family: sans-serif;">We already know that units, such as the crt unit, let you use more procedures and functions than the built-in ones. You can make your own units which have procedures and functions that you have made in them.</span>
<br />
<span style="background-color: white; color: #000033; font-family: sans-serif;"><br /></span>
<br />
<div style="background-color: white; color: #000033; font-family: sans-serif;">
To make a unit you need to create new Pascal file which we will call MyUnit.pas. The first line of the file should start with the <em>unit</em> keyword followed by the unit's name. The unit's name and the unit's file name must be exactly the same.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">unit MyUnit;</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The next line is the interface keyword. After this you must put the names of the procedures that will be made available to the program that will use your unit. For this example we will be making a function called<em>NewReadln</em> which is like <em>Readln</em> but it lets you limit the amount of characters that can be entered.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">unit MyUnit;<br /><br />interface<br /><br />function NewReadln(Max: Integer): String;</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The next line is <em>implementation</em>. This is where you will type the full code for the procedures and functions. We will also need to use the crt unit to make <em>NewReadln</em>. We end the unit just like a normal program with the <em>end</em>keyword.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">unit MyUnit;<br /><br />interface<br /><br />function NewReadln(Max: Integer): String;<br /><br />implementation<br /><br />function NewReadln(Max: Integer): String;<br />var<br /> s: String;<br /> c: Char;<br />begin<br /> s := '';<br /> repeat<br /> c := ReadKey;<br /> if (c = #8){#8 = BACKSPACE} and (s <> '') then<br /> begin<br /> Write(#8+' '+#8);<br /> delete(s,length(s),1);<br /> end;<br /> if (c <> #8) and (c <> #13){#13 = ENTER} and (length(s) < Max) then<br /> begin<br /> Write(c);<br /> s := s + c;<br /> end;<br /> until c = #13;<br /> NewReadln := s;<br />end;<br /><br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Once you have saved the unit you must compile it. Now we must make the program that uses the unit that we have just made. This time we will type MyUnit in the <em>uses</em> section and then use the <em>NewReadln</em> function.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<span style="color: #660000; font-family: monospace;">program MyProgram;</span><br style="color: #660000; font-family: monospace;" /><span style="color: #660000; font-family: monospace;"> </span><br style="color: #660000; font-family: monospace;" /><span style="color: #660000; font-family: monospace;">uses</span><br style="color: #660000; font-family: monospace;" /><span style="color: #660000; font-family: monospace;"> MyUnit;</span><br style="color: #660000; font-family: monospace;" /><span style="color: #660000; font-family: monospace;"> </span><br style="color: #660000; font-family: monospace;" /><span style="color: #660000; font-family: monospace;">var</span><br style="color: #660000; font-family: monospace;" /><span style="color: #660000; font-family: monospace;"> s: String;</span><br style="color: #660000; font-family: monospace;" /><span style="color: #660000; font-family: monospace;"> </span><br style="color: #660000; font-family: monospace;" /><span style="color: #660000; font-family: monospace;">begin</span><br style="color: #660000; font-family: monospace;" /><span style="color: #660000; font-family: monospace;"> s := NewReadln(10);</span><br style="color: #660000; font-family: monospace;" /><span style="color: #660000; font-family: monospace;">end.</span>
</div>
<center style="background-color: white; color: #000033; font-family: sans-serif;">
<ins style="border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; display: inline-table; height: 280px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: relative; visibility: visible; width: 336px;"></ins></center>
Mohammed E. Katzhttp://www.blogger.com/profile/16957627613529515712noreply@blogger.com0tag:blogger.com,1999:blog-6611663679545711262.post-66709550317037193592012-08-28T02:50:00.003-07:002012-08-28T02:57:39.929-07:00Pascal Programming Lesson #11 - Data Files<br />
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Data files are different from text files in a few ways. Data files are random access which means you don't have to read through them line after line but instead access any part of the file at any time. Here is how you declare a data file:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
</div>
<code style="color: #660000;">program DataFiles;<br /><br />var<br /> f: file of Byte;<br /><br />begin<br />end.</code><br />
<br />
We then use <em>Assign</em> in the same way as we do with a text file.<br />
<code style="color: #660000;"><br /></code>
<code style="color: #660000;">program DataFiles;<br /><br />var<br /> f: file of Byte;<br /><br />begin<br /> Assign(f,'MyFile.dat');<br />end.</code><br />
<br />
You can use <em>Rewrite</em> to create a new file or overwrite an existing one. The difference between text files and data files when using <em>Rewrite</em> is that data files can be read and written to.<br />
<code style="color: #660000;"><br /></code>
<code style="color: #660000;">program DataFiles;<br /><br />var<br /> f: file of Byte;<br /><br />begin<br /> Assign(f,'MyFile.dat');<br /> Rewrite(f);<br />end.</code><br />
<em><br /></em>
<em>Reset</em> is the same as <em>Rewrite</em> except that it doesn't overwrite the file.<br />
<code style="color: #660000;"><br /></code>
<code style="color: #660000;">program DataFiles;<br /><br />var<br /> f: file of Byte;<br /><br />begin<br /> Assign(f,'MyFile.dat');<br /> Reset(f);<br />end.</code><br />
<br />
When you write to a file using the <em>Write</em> command you must first put the value to be written to the file into a variable. Before you can write to or read from a data file you must use the <em>Seek</em> command to find the right place to start writing. You must also remember that data files start from position 0 and not 1.<br />
<code style="color: #660000;"><br /></code>
<code style="color: #660000;">program DataFiles;<br /><br />var<br /> f: file of Byte;<br /> b: Byte;<br /><br />begin<br /> Assign(f,'MyFile.dat');<br /> Reset(f);<br /> b := 1;<br /> Seek(f,0);<br /> Write(f,b);<br />end.</code><br />
<br />
The <em>Read</em> command is used to read from a data file.<br />
<code style="color: #660000;"><br /></code>
<code style="color: #660000;">program DataFiles;<br /><br />var<br /> f: file of Byte;<br /> b: Byte;<br /><br />begin<br /> Assign(f,'MyFile.dat');<br /> Reset(f);<br /> Seek(f,0);<br /> Read(f,b);<br />end.</code><br />
<br />
You must close a data file when you are finished with it just like with text files.<br />
<code style="color: #660000;"><br /></code>
<code style="color: #660000;">program DataFiles;<br /><br />var<br /> f: file of Byte;<br /> b: Byte;<br /><br />begin<br /> Assign(f,'MyFile.dat');<br /> Reset(f);<br /> Seek(f,0);<br /> Read(f,b);<br /> Close(f);<br />end.</code><br />
<br />
The <em>FileSize</em> command can be used with the <em>FilePos</em> command to find out when you have reached the end of the file. <em>FileSize</em> returns the actual number of records which means it starts at 1 and not 0. The <em>FilePos</em>command will tell at what position in the file you are.<br />
<code style="color: #660000;"><br /></code>
<code style="color: #660000;">program DataFiles;<br /><br />var<br /> f: file of Byte;<br /> b: Byte;<br /><br />begin<br /> Assign(f,'MyFile.dat');<br /> Reset(f);<br /> while FilePos(f) <> FileSize(f) do<br /> begin<br /> Read(f,b);<br /> Writeln(b);<br /> end;<br /> Close(f);<br />end.</code><br />
<br />
The <em>Truncate</em> command will delete everything in the file from the current position.<br />
<code style="color: #660000;"><br /></code>
<code style="color: #660000;">program DataFiles;<br /><br />var<br /> f: file of Byte;<br /><br />begin<br /> Assign(f,'MyFile.dat');<br /> Reset(f);<br /> Seek(f,3);<br /> Truncate(f);<br /> Close(f);<br />end.</code><br />
<br />
One of the most useful things about data files is that you can use them to store records.<br />
<code style="color: #660000;"><br /></code>
<code style="color: #660000;">program DataFiles;<br /><br />type<br /> StudentRecord = Record<br /> Number: Integer;<br /> Name: String;<br /> end;<br /><br />var<br /> Student: StudentRecord;<br /> f: file of StudentRecord;<br /><br />begin<br /> Assign(f,'MyFile.dat');<br /> Rewrite(f);<br /> Student.Number := 12345;<br /> Student.Name := 'John Smith';<br /> Write(f,Student);<br /> Close(f);<br />end.</code><br />
<br />
<center style="background-color: white; color: #000033; font-family: sans-serif;">
<ins style="border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; display: inline-table; height: 280px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: relative; visibility: visible; width: 336px;"></ins></center>
Mohammed E. Katzhttp://www.blogger.com/profile/16957627613529515712noreply@blogger.com0tag:blogger.com,1999:blog-6611663679545711262.post-14316155359560756152012-08-28T02:47:00.003-07:002012-08-28T02:57:39.892-07:00Pascal Programming Lesson #10 - Text Files<div style="background-color: white; color: #000033; font-family: sans-serif;">
You should by now know that a text file is a file with lines of text. When you want to access a file in Pascal you have to first create a file variable.</div>
<center style="background-color: white; color: #000033; font-family: sans-serif;">
<ins style="border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; display: inline-table; height: 280px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: relative; visibility: visible; width: 336px;"></ins></center>
<br />
<br />
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Files;<br /><br />var<br /> f: Text;<br /><br />begin<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
After the variable has been declared you must assign the file name to it.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Files;<br /><br />var<br /> f: Text;<br /><br />begin<br /> Assign(f,'MyFile.txt');<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
To create a new empty file we use the <em>Rewrite</em> command. This will overwrite any files that exist with the same name.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Files;<br /><br />var<br /> f: Text;<br /><br />begin<br /> Assign(f,'MyFile.txt');<br /> Rewrite(f);<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The <em>Write</em> and <em>Writeln</em> commands work on files in the same way they work on the screen except that you must use an extra parameter to tell it to write to the file.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Files;<br /><br />var<br /> f: Text;<br /><br />begin<br /> Assign(f,'MyFile.txt');<br /> Rewrite(f);<br /> Writeln(f,'A line of text');<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
If you want to read from a file that already exists then you must use <em>Reset</em> instead of <em>Rewrite</em>. Use <em>Readln</em> to read lines of text from the file. You will also need a <em>while</em> loop that repeats until it comes to the end of the file.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Files;<br /><br />var<br /> f: Text;<br /> s: String;<br /><br />begin<br /> Assign(f,'MyFile.txt');<br /> Reset(f);<br /> while not eof(f) do<br /> Readln(f,s);<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<em><br /></em></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<em>Append</em> opens a file and lets you add more text at the end of the file.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Files;<br /><br />var<br /> f: Text;<br /> s: String;<br /><br />begin<br /> Assign(f,'MyFile.txt');<br /> Append(f);<br /> Writeln(f,'Some more text');<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
No matter which one of the 3 access types you choose, you must still close a file when you are finished using it. If you don't close it then some of the text that was written to it might be lost.</div>
<br class="Apple-interchange-newline" /><br />
<br />
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Files;<br /><br />var<br /> f: Text;<br /> s: String;<br /><br />begin<br /> Assign(f,'MyFile.txt');<br /> Append(f);<br /> Writeln(f,'Some more text');<br /> Close(f);<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You can change a file's name with the <em>Rename</em> command and you can delete a file with the <em>Erase</em> command.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Files;<br /><br />var<br /> f: Text;<br /><br />begin<br /> Assign(f,'MyFile.txt');<br /> Rename(f,'YourFile.txt');<br /> Erase(f);<br /> Close(f);<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
To find out if a file exists, you must first turn off error checking using the {$I-} compiler directive. After that you must <em>Reset</em> the file and if <em>IOResult</em> = 2 then the file was not found. If <em>IOResult</em> = 0 then the file was found but if it is any other value then the program must be ended with the <em>Halt</em> command. <em>IOResult</em> loses its value once it has been used so we also have to put that into another variable before using it. You must also use {$I+} to turn error checking back on.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Files;<br /><br />var<br /> f: Text;<br /> IOR: Integer;<br /><br />begin<br /> Assign(f,'MyFile.txt');<br />{$I-}<br /> Reset(f);<br />{$I+}<br /> IOR := IOResult;<br /> if IOR = 2 then<br /> Writeln('File not found');<br /> else<br /> if IOR <> 0 then<br /> Halt;<br /> Close(f);<br />end.</code></div>
Mohammed E. Katzhttp://www.blogger.com/profile/16957627613529515712noreply@blogger.com0tag:blogger.com,1999:blog-6611663679545711262.post-7251281819050153692012-08-28T02:45:00.002-07:002012-08-28T02:57:39.941-07:00Pascal Programming Lesson #9 - Procedures and Functions<br />
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Procedures</h2>
<div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Procedures are sub-programs that can be called from the main part of the program. Procedures are declared outside of the main program body using the <em>procedure</em> keyword. Procedures must also be given a unique name. Procedures have their own <em>begin</em> and <em>end</em>. Here is an example of how to make a procedure called Hello that prints "Hello" on the screen.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Procedures;<br /><br />procedure Hello;<br />begin<br /> Writeln('Hello');<br />end;<br /><br />begin<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
To use a procedure we must call it by using its name in the main body.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Procedures;<br /><br />procedure Hello;<br />begin<br /> Writeln('Hello');<br />end;<br /><br />begin<br /> Hello;<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Procedures must always be above where they are called from. Here is an example of a procedure that calls another procedure.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Procedures;<br /><br />procedure Hello;<br />begin<br /> Writeln('Hello');<br />end;<br /><br />procedure HelloCall;<br />begin<br /> Hello;<br />end;<br /><br />begin<br /> HelloCall;<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Procedures can have parameters just like the other commands we have been using. Each parameter is given a name and type and is then used just like any other variable. If you want to use more than one parameter then they must be separated with semi-colons.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Procedures;<br /><br />procedure Print(s: String; i: Integer);<br />begin<br /> Writeln(s);<br /> Writeln(i);<br />end;<br /><br />begin<br /> Print('Hello',3);<br />end.</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></h2>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Global and Local variables</h2>
</div>
<div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The variables we have been using so far have been global because they can be used at any time during the program. Local variables can only be used inside procedures but the memory they use is released when the procedure is not being used. Local variables are declared just underneath the procedure name declaration.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Procedures;<br /><br />procedure Print(s: String);<br />var<br /> i: Integer;<br />begin<br /> for i := 1 to 3 do<br /> Writeln(s);<br />end;<br /><br />begin<br /> Print('Hello');<br />end.</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></h2>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Functions</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Functions are like procedures except they return a value. The <em>function</em> keyword is used instead of <em>procedure</em>when declaring a function. To say what data type the return value must be you must use a colon and the name of the type after the function's name.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Functions;<br /><br />function Add(i, j:Integer): Integer;<br />begin<br />end;<br /><br />begin<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Assigning the value of a function to a variable make the variable equal to the return value. If you use a function in something like <em>Writeln</em> it will print the return value. To set the return value just make the name of the function equal to the value you want to return.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Functions;<br /><br />var<br /> Answer: Integer;<br /><br />function Add(i, j:Integer): Integer;<br />begin<br /> Add := i + j;<br />end;<br /><br />begin<br /> Answer := Add(1,2);<br /> Writeln(Add(1,2));<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You can exit a procedure or function at any time by using the <em>Exit</em> command.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Procedures;<br /><br />procedure GetName;<br />var<br /> Name: String;<br />begin<br /> Writeln('What is your name?');<br /> Readln(Name);<br /> if Name = '' then<br /> Exit;<br /> Writeln('Your name is ',Name);<br />end;<br /><br />begin<br /> GetName;<br />end.</code></div>
</div>
Mohammed E. Katzhttp://www.blogger.com/profile/16957627613529515712noreply@blogger.com0tag:blogger.com,1999:blog-6611663679545711262.post-17431607828480147192012-08-28T02:40:00.003-07:002012-08-28T02:57:39.896-07:00Pascal Programming Lesson #8 - Types, Records and Sets<br />
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Types</h2>
<div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
It is possible to create your own variable types using the <em>type</em> statement. The first type you can make is records. Records are 2 or more variables of different types in one. An example of how this could be used is for a student who has a student number and a name. Here is how you create a type:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Types;<br /><br />Type<br /> Student = Record<br /> Number: Integer;<br /> Name: String;<br /> end;<br /><br />begin<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
After you have created the type you must declare a variable of that type to be able to use it.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Types;<br /><br />Type<br /> StudentRecord = Record<br /> Number: Integer;<br /> Name: String;<br /> end;<br /><br />var<br /> Student: StudentRecord;<br /><br />begin<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
To access the Number and Name parts of the record you must do the following:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Types;<br /><br />Type<br /> StudentRecord = Record<br /> Number: Integer;<br /> Name: String;<br /> end;<br /><br />var<br /> Student: StudentRecord;<br /><br />begin<br /> Student.Number := 12345;<br /> Student.Name := 'John Smith';<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The other type is a set. Sets are not very useful and anything you can do with a set can be done just as easily in another way. The following is an example of a set called Animal which has dog, cat and rabbit as the data it can store:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Types;<br /><br />Type<br /> Animal = set of (dog, cat, rabbit);<br /><br />var<br /> MyPet: Animal;<br /><br />begin<br /> MyPet := dog;<br />end.</code></div>
</div>
<div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You can't use <em>Readln</em> or <em>Writeln</em> on sets so the above way of using it is not very useful. You can create a range of values as a set such as 'a' to 'z'. This type of set can be used to test if a value is in that range.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Types;<br /><br />uses<br /> crt;<br /><br />Type<br /> Alpha = 'a'..'z';<br /><br />var<br /> Letter: set of Alpha;<br /> c: Char;<br /><br />begin<br /> c := ReadKey;<br /> if c in [Letter] then<br /> Writeln('You entered a letter');<br />end.</code></div>
</div>
Mohammed E. Katzhttp://www.blogger.com/profile/16957627613529515712noreply@blogger.com0tag:blogger.com,1999:blog-6611663679545711262.post-66570623638368445642012-08-28T02:39:00.001-07:002012-08-28T02:57:39.925-07:00Pascal Programming Lesson #7 - Arrays<br />
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Arrays are variables that are made up of many variables of the same data type but have only one name. Here is a visual representation of an array with 5 elements:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<table style="background-color: white; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; color: #000033; font-family: sans-serif;"><tbody>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">1</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">value 1</td></tr>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">2</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">value 2</td></tr>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">3</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">value 3</td></tr>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">4</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">value 4</td></tr>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">5</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">value 5</td></tr>
</tbody></table>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Arrays are declared in almost the same way as normal variables are declared except that you have to say how many elements you want in the array.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
</div>
<code style="color: #660000;">program Arrays;<br /><br />var<br /> a: array[1..5] of Integer;<br /><br />begin<br />end.</code><br />
<code style="color: #660000;"><br /></code>
We access each of the elements using the number of the elements behind it in square brackets.<br />
<code style="color: #660000;"><br /></code>
<code style="color: #660000;">program Arrays;<br /><br />var<br /> a: array[1..5] of Integer;<br /><br />begin<br /> a[1] := 12;<br /> a[2] := 23;<br /> a[3] := 34;<br /> a[4] := 45;<br /> a[5] := 56;<br />end.</code><br />
<br />
It is a lot easier when you use a loop to access the values in an array. Here is an example of reading in 5 values into an array:<br />
<code style="color: #660000;"><br /></code>
<code style="color: #660000;">program Arrays;<br /><br />var<br /> a: array[1..5] of Integer;<br /> i: Integer;<br /><br />begin<br /> for i := 1 to 5 do<br /> Readln(a[i]);<br />end.</code><br />
<h2>
Sorting arrays</h2>
<div>
You will sometimes want to sort the values in an array in a certain order. To do this you can use a bubble sort. A bubble sort is only one of many ways to sort an array. With a bubble sort the biggest numbers are moved to the end of the array.<br />
You will need 2 loops. One to go through each number and another to point to the other number that is being compared. If the number is greater then it is swapped with the other one. You will need to use a temporary variable to store values while you are swapping them.<br />
<br />
<code style="color: #660000;">program Arrays;<br /><br />var<br /> a: array[1..5] of Integer;<br /> i, j, tmp: Integer;<br /><br />begin<br /> a[1] := 23;<br /> a[2] := 45;<br /> a[3] := 12;<br /> a[4] := 56;<br /> a[5] := 34;<br /><br /> for i := 1 to 4 do<br /> for j := i + 1 to 5 do<br /> if a[i] > a[j] then<br /> begin<br /> tmp := a[i];<br /> a[i] := a[j];<br /> a[j] := tmp;<br /> end;<br /><br /> for i := 1 to 5 do<br /> writeln(i,': ',a[i]);<br />end.</code><br />
<h2>
2D arrays</h2>
Arrays can have 2 dimensions instead of just one. In other words they can have rows and columns instead of just rows.<br />
<br />
<table style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; color: #000033;"><tbody>
<tr><th style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;"> </th><th style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">1</th><th style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">2</th><th style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">3</th></tr>
<tr><th style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">1</th><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">1</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">2</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">3</td></tr>
<tr><th style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">2</th><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">4</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">5</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">6</td></tr>
<tr><th style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">3</th><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">7</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">8</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">9</td></tr>
</tbody></table>
<br />
Here is how to declare a 2D array:<br />
<code style="color: #660000;"><br /></code>
<code style="color: #660000;">program Arrays;<br /><br />var<br /> a: array [1..3,1..3] of Integer;<br /><br />begin<br />end.</code><br />
<br />
To access the values of a 2d array you must use 2 numbers in the square brackets. 2D arrays also require 2 loops instead of just one.<br />
<code style="color: #660000;"><br /></code>
<code style="color: #660000;">program Arrays;<br /><br />var<br /> r, c: Integer;<br /> a: array [1..3,1..3] of Integer;<br /><br />begin<br /> for r := 1 to 3 do<br /> for c := 1 to 3 do<br /> Readln(a[r,c]);<br />end.</code><br />
<br />
You can get multi-dimensional arrays that have more than 2 dimensions but these are not used very often so you don't need to worry about them.</div>
<center>
<ins style="border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; display: inline-table; height: 280px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: relative; visibility: visible; width: 336px;"></ins></center>
<br />
<center style="background-color: white; color: #000033; font-family: sans-serif;">
<ins style="border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; display: inline-table; height: 280px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: relative; visibility: visible; width: 336px;"></ins></center>
Mohammed E. Katzhttp://www.blogger.com/profile/16957627613529515712noreply@blogger.com0tag:blogger.com,1999:blog-6611663679545711262.post-24837548682985074972012-08-28T02:35:00.003-07:002012-08-28T02:57:39.907-07:00Pascal Programming Lesson #6 - Loops<br />
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Loops are used when you want to repeat code a lot of times. For example, if you wanted to print "Hello" on the screen 10 times you would need 10 <em>Writeln</em> commands. You could do the same thing by putting 1 <em>Writeln</em>command inside a loop which repeats itself 10 times.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
There are 3 types of loops which are the <em>for</em> loop, <em>while</em> loop and <em>repeat until</em> loop.</div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
For loop</h2>
<div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The for loop uses a loop counter variable, which it adds 1 to each time, to loop from a first number to a last number.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Loops;<br /><br />var<br /> i: Integer;<br /><br />begin<br /> for i := 1 to 10 do<br /> Writeln('Hello');<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
If you want to have more than 1 command inside a loop then you must put them between a <em>begin</em> and an <em>end</em>.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Loops;<br /><br />var<br /> i: Integer;<br /><br />begin<br /> for i := 1 to 10 do<br /> begin<br /> Writeln('Hello');<br /> Writeln('This is loop ',i);<br /> end;<br />end.</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
While loop</h2>
<div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The <em>while</em> loop repeats while a condition is true. The condition is tested at the top of the loop and not at any time while the loop is running as the name suggests. A while loop does not need a loop variable but if you want to use one then you must initialize its value before entering the loop.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Loops;<br /><br />var<br /> i: Integer;<br /><br />begin<br /> i := 0;<br /> while i <= 10<br /> begin<br /> i := i + 1;<br /> Writeln('Hello');<br /> end;<br />end.</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Repeat until loop</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The <em>repeat until</em> loop is like the <em>while</em> loop except that it tests the condition at the bottom of the loop. It also doesn't have to have a <em>begin</em> and an <em>end</em> if it has more than one command inside it.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Loops;<br /><br />var<br /> i: Integer;<br /><br />begin<br /> i := 0;<br /> repeat<br /> i := i + 1;<br /> Writeln('Hello');<br /> until i = 10;<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
If you want to use more than one condition for either the <em>while</em> or <em>repeat</em> loops then you have to put the conditions between brackets.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Loops;<br /><br />var<br /> i: Integer;<br /> s: String;<br /><br />begin<br /> i := 0;<br /> repeat<br /> i := i + 1;<br /> Write('Enter a number: ');<br /> Readln(s);<br /> until (i = 10) or (s = 0);<br />end.</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Break and Continue</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The <em>Break</em> command will exit a loop at any time. The following program will not print anything because it exits the loop before it gets there.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Loops;<br /><br />var<br /> i: Integer;<br /><br />begin<br /> i := 0;<br /> repeat<br /> i := i + 1;<br /> Break;<br /> Writeln(i);<br /> until i = 10;<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The <em>Continue</em> command will jump back to the top of a loop. This example will also not print anything but unlike the <em>Break</em> example, it will count all the way to 10.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Loops;<br /><br />var<br /> i: Integer;<br /><br />begin<br /> i := 0;<br /> repeat<br /> i := i + 1;<br /> Continue;<br /> Writeln(i);<br /> until i = 10;<br />end.</code></div>
</div>
<center style="background-color: white; color: #000033; font-family: sans-serif;">
<ins style="border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; display: inline-table; height: 280px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: relative; visibility: visible; width: 336px;"></ins></center>
</div>
Mohammed E. Katzhttp://www.blogger.com/profile/16957627613529515712noreply@blogger.com0tag:blogger.com,1999:blog-6611663679545711262.post-19099861140926754522012-08-28T02:32:00.003-07:002012-08-28T02:57:39.900-07:00Pascal Programming Lesson #5 - Decisions<br />
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
if then else</h2>
<div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The <em>if</em> statement allows a program to make a decision based on a condition. The following example asks the user to enter a number and tells you if the number is greater than 5:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Decisions;<br /><br />var<br /> i: Integer;<br /><br />begin<br /> Writeln('Enter a number');<br /> Readln(i);<br /> if i > 5 then<br /> Writeln('Greater than 5');<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Here is a table of the operators than can be used in conditions:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<table style="background-color: white; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; color: #000033; font-family: sans-serif;"><tbody>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">></td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">Greater than</td></tr>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;"><</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">Less than</td></tr>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">>=</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">Greater than or equal to</td></tr>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;"><=</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">Less than or equal to</td></tr>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">=</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">Equal to</td></tr>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;"><></td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">Not equal to</td></tr>
</tbody></table>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The above example only tells you if the number is greater than 5. If you want it to tell you that it is not greater than 5 then we use <em>else</em>. When you use else you must not put a semi-colon on the end of the command before it.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Decisions;<br /><br />var<br /> i: Integer;<br /><br />begin<br /> Writeln('Enter a number');<br /> Readln(i);<br /> if i > 5 then<br /> Writeln('Greater than 5')<br /> else<br /> Writeln('Not greater than 5');<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
If the condition is True then the <em>then</em> part is chosen but if it is False then the <em>else</em> part is chosen. This is because the conditions such as i > 5 is a Boolean equation. You can even assign the result of a Boolean equation to a Boolean variable.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Decisions;<br /><br />var<br /> i: Integer;<br /> b: Boolean;<br /><br />begin<br /> Writeln('Enter a number');<br /> Readln(i);<br /> b := i > 5;<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
If you want to use more than 1 condition then you must put each condition in brackets. To join the conditions you can use either <em>AND</em> or <em>OR</em>. If you use <em>AND</em> then both conditions must be true but if you use <em>OR</em> then only 1 or both of the conditions must be true.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Decisions;<br /><br />var<br /> i: Integer;<br /><br />begin<br /> Writeln('Enter a number');<br /> Readln(i);<br /> if (i > 1) and (i < 100) then<br /> Writeln('The number is between 1 and 100');<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
If you want to put 2 or more commands for an <em>if</em> statement for both the then and the else parts you must use<em>begin</em> and <em>end;</em> to group them together. You will see that this <em>end</em> has a semi-colon after it instead of a full stop.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Decisions;<br /><br />var<br /> i: Integer;<br /><br />begin<br /> Writeln('Enter a number');<br /> Readln(i);<br /> if i > 0 then<br /> begin<br /> Writeln('You entered ',i);<br /> Writeln('It is a positive number');<br /> end;<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You can also use <em>if</em> statements inside other <em>if</em> statements.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Decisions;<br /><br />var<br /> i: Integer;<br /><br />begin<br /> Writeln('Enter a number');<br /> Readln(i);<br /> if i > 0 then<br /> Writeln('Positive')<br /> else<br /> if i < 0 then<br /> Writeln('Negative')<br /> else<br /> Writeln('Zero');<br />end.</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Case</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The <em>case</em> command is like an <em>if</em> statement but you can have many conditions with actions for each one.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Decisions;<br /><br />uses<br /> crt;<br /><br />var<br /> Choice: Char;<br /><br />begin<br /> Writeln('Which on of these do you like?');<br /> Writeln('a - Apple:');<br /> Writeln('b - Banana:');<br /> Writeln('c - Carrot:');<br /> Choice := ReadKey;<br /> case Choice of<br /> 'a': Writeln('You like apples');<br /> 'b': Writeln('You like bananas');<br /> 'c': Writeln('You like carrots');<br /> else<br /> Writeln('You made an invalid choice');<br /> end;<br />end.</code></div>
</div>
Mohammed E. Katzhttp://www.blogger.com/profile/16957627613529515712noreply@blogger.com0tag:blogger.com,1999:blog-6611663679545711262.post-20156495530133817382012-08-28T02:30:00.001-07:002012-08-28T02:57:39.903-07:00Pascal Programming Lesson #4 - String Handling and Conversions<br />
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
String Handling</h2>
<div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You can access a specific character in a string if you put the number of the position of that character in square brackets behind a string.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Strings;<br /><br />var<br /> s: String;<br /> c: Char;<br /><br />begin<br /> s := 'Hello';<br /> c := s[1];<dfn style="color: #666666;">{c = 'H'}</dfn><br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You can get the length of a string using the <em>Length</em> command.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Strings;<br /><br />var<br /> s: String;<br /> l: Integer;<br /><br />begin<br /> s := 'Hello';<br /> l := Length(s);<dfn style="color: #666666;">{l = 5}</dfn><br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
To find the position of a string within a string use the <em>Pos</em> command.<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Parameters:<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
1: String to find<br />2: String to look in</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Strings;<br /><br />var<br /> s: String;<br /> p: Integer;<br /><br />begin<br /> s := 'Hello world';<br /> p := Pos('world',s);<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The <em>Delete</em> command removes characters from a string.<br />Parameters:<br />1: String to delete characters from<br />2: Position to start deleting from<br />3: Amount of characters to delete</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Strings;<br /><br />var<br /> s: String;<br /><br />begin<br /> s := 'Hello';<br /> Delete(s,1,1);<dfn style="color: #666666;">{s = 'ello'}</dfn><br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The <em>Copy</em> command is like the square brackets but can access more than just one character.<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Parameters:<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
1: String to copy characters from<br />2: Position to copy from<br />3: Amount of characters to copy</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Strings;<br /><br />var<br /> s, t: String;<br /><br />begin<br /> s := 'Hello';<br /> t := Copy(s,1,3);<dfn style="color: #666666;">{t = 'Hel'}</dfn><br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<em><br /></em></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<em>Insert</em> will insert characters into a string at a certain position.<br />Parameters:<br />1: String that will be inserted into the other string<br />2: String that will have characters inserted into it<br />3: Position to insert characters</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Strings;<br /><br />var<br /> s: String;<br /><br />begin<br /> s := 'Hlo';<br /> Insert('el',s,2);<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The <em>ParamStr</em> command will give you the command-line parameters that were passed to a program.<em>ParamCount</em> will tell you how many parameters were passed to the program. Parameter 0 is always the program's name and from 1 upwards are the parameters that have been typed by the user.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Strings;<br /><br />var<br /> s: String;<br /> i: Integer;<br /><br />begin<br /> s := ParamStr(0);<br /> i := ParamCount;<br />end.</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Conversions</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The <em>Str</em> command converts an integer to a string.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Convert;<br /><br />var<br /> s: String;<br /> i: Integer;<br /><br />begin<br /> i := 123;<br /> Str(i,s);<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The <em>Val</em> command converts a string to an integer.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Convert;<br /><br />var<br /> s: String;<br /> i: Integer;<br /> e: Integer;<br /><br />begin<br /> s := '123';<br /> Val(s,i,e);<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<em><br /></em></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<em>Int</em> will give you the number before the comma in a real number.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Convert;<br /><br />var<br /> r: Real;<br /><br />begin<br /> r := Int(3.14);<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<em><br /></em></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<em>Frac</em> will give you the number after the comma in a real number.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Convert;<br /><br />var<br /> r: Real;<br /><br />begin<br /> r := Frac(3.14);<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<em><br /></em></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<em>Round</em> will round off a real number to the nearest integer.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Convert;<br /><br />var<br /> i: Integer;<br /><br />begin<br /> i := Round(3.14);<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<em><br /></em></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<em>Trunc</em> will give you the number before the comma of a real number as an integer.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Convert;<br /><br />var<br /> i: Integer;<br /><br />begin<br /> i := Trunc(3.14);<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Computers use the numbers 0 to 255(1 byte) to represent characters internally and these are called ASCII characters. The <em>Ord</em> command will convert a character to number and the <em>Chr</em> command will convert a number to a character. Using a # in front of a number will also convert it to a character.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Convert;<br /><br />var<br /> b: Byte;<br /> c: Char;<br /><br />begin<br /> c := 'a';<br /> b := Ord(c);<br /> c := Chr(b);<br /> c := #123;<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The <em>UpCase</em> command changes a character from a lowercase letter to and uppercase letter.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Convert;<br /><br />var<br /> c: Char;<br /><br />begin<br /> c := 'a';<br /> c := UpCase(c);<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
There is no lowercase command but you can do it by adding 32 to the ordinal value of an uppercase letter and then changing it back to a character.</div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Extras</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The <em>Random</em> command will give you a random number from 0 to the number you give it - 1. The <em>Random</em>command generates the same random numbers every time you run a program so the <em>Randomize</em> command is used to make them more random by using the system clock.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Rand;<br /><br />var<br /> i: Integer;<br /><br />begin<br /> Randomize;<br /> i := Random(101);<br />end.</code></div>
</div>
Mohammed E. Katzhttp://www.blogger.com/profile/16957627613529515712noreply@blogger.com0tag:blogger.com,1999:blog-6611663679545711262.post-37806200108651601632012-08-28T02:25:00.000-07:002012-08-28T02:57:39.912-07:00Pascal Programming Lesson #3 - Variables and Constants<br />
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Variables are names given to blocks of the computer's memory. The names are used to store values in these blocks of memory.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Variables can hold values which are either numbers, strings or Boolean. We already know what numbers are. Strings are made up of letters. Boolean variables can have one of two values, either True or False.</div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Using variables</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You must always declare a variable before you use it. We use the <em>var</em> statement to do this. You must also choose what type of variable it is. Here is a table of the different variable types:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<table style="background-color: white; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; color: #000033; font-family: sans-serif;"><tbody>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">Byte</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">0 to 255</td></tr>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">Word</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">0 to 65535</td></tr>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">ShortInt</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">-128 to 127</td></tr>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">Integer</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">-32768 to 32767</td></tr>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">LongInt</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">-4228250000 to 4228249000</td></tr>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">Real</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">floating point values</td></tr>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">Char</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">1 character</td></tr>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">String</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">up to 255 characters</td></tr>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">Boolean</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">true or false</td></tr>
</tbody></table>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Here is an example of how to declare an integer variable named i:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Variables;<br /><br />var<br /> i: Integer;<br /><br />begin<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
To assign a value to a variable we use :=.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Variables;<br /><br />var<br /> i: Integer;<br /><br />begin<br /> i := 5;<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You can create 2 or more variables of the same type if you seperate their names with commas. You can also create variables of a different type without the need for another <em>var</em> statemtent.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Variables;<br /><br />var<br /> i, j: Integer;<br /> s: String;<br /><br />begin<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
When you assign a value to a string variable, you must put it between single quotes. Boolean variables can only be assigned the values True and False.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Variables;<br /><br />var<br /> i: Integer;<br /> s: String;<br /> b: Boolean;<br /><br />begin<br /> i := -3;<br /> s := 'Hello';<br /> b := True;<br />end.</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Calculations with variables</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Variables can be used in calculations. For example you could assign the value to a variable and then add the number 1 to it. Here is a table of the operators that can be used:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<table style="background-color: white; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; color: #000033; font-family: sans-serif;"><tbody>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">+</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">Add</td></tr>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">-</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">Subtract</td></tr>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">*</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">Multiply</td></tr>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">/</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">Floating Point Divide</td></tr>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">div</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">Integer Divide</td></tr>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">mod</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">Remainder of Integer Division</td></tr>
</tbody></table>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The following example shows a few calculations that can be done:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Variables;<br /><br />var<br /> Num1, Num2, Ans: Integer;<br /><br />begin<br /> Ans := 1 + 1;<br /> Num1 := 5;<br /> Ans := Num1 + 3;<br /> Num2 := 2;<br /> Ans := Num1 - Num2;<br /> Ans := Ans * Num1;<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Strings hold characters. Characters include the the letters of the alphabet as well as special characters and even numbers. It is important to understand that integer numbers and string numbers are different things. You can add strings together as well. All that happens is it joins the 2 strings. If you add the strings '1' and '1' you will get '11' and not 2.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Variables;<br /><br />var<br /> s: String;<br /><br />begin<br /> s := '1' + '1';<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You can read vales from the keyboard into variables using <em>Readln</em> and <em>ReadKey</em>. ReadKey is from the crt unit and only reads 1 character. You will see that <em>ReadKey</em> works differently to <em>Readln</em></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Variables;<br /><br />uses crt;<br /><br />var<br /> i: Integer;<br /> s: String;<br /> c: Char;<br /><br />begin<br /> Readln(i);<br /> Readln(s);<br /> c := ReadKey;<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Printing variables on the screen is just as easy. If you want to print variables and text with the same <em>Writeln</em>then seperate them with commas.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Variables;<br /><br />var<br /> i: Integer;<br /> s: String;<br />begin<br /> i := 24;<br /> s := 'Hello';<br /> Writeln(i);<br /> Writeln(s,' world');<br />end.</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Constants</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Constants are like variables except that their values can't change. You assign a value to a constant when you create it. <em>const</em> is used instead of <em>var</em> when declaring a constant. Constants are used for values that do not change such as the value of pi.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Variables;<br /><br />const<br /> pi: Real = 3.14;<br /><br />var<br /> c, d: Real;<br /><br />begin<br /> d := 5;<br /> c := pi * d;<br />end.</code></div>
Mohammed E. Katzhttp://www.blogger.com/profile/16957627613529515712noreply@blogger.com0tag:blogger.com,1999:blog-6611663679545711262.post-72282786020643479342012-08-28T02:22:00.004-07:002012-08-28T02:57:39.937-07:00Pascal Programming Lesson #2 - Colors, Coordinates, Windows and Sound<br />
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Colors</h2>
<div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
To change the color of the text printed on the screen we use the <em>TextColor</em> command.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Colors;<br /><br />uses<br /> crt;<br /><br />begin<br /> TextColor(Red);<br /> Writeln('Hello');<br /> TextColor(White);<br /> Writeln('world');<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The <em>TextBackground</em> command changes the color of the background of text. If you want to change the whole screen to a certain color then you must use <em>ClrScr</em>.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Colors;<br /><br />uses<br /> crt;<br /><br />begin<br /> TextBackground(Red);<br /> Writeln('Hello');<br /> TextColor(White);<br /> ClrScr;<br />end.</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Screen coordinates</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You can put the cursor anywhere on the screen using the <em>GoToXY</em> command. In DOS, the screen is 80 characters wide and 25 characters high. The height and width varies on other platforms. You may remember graphs from Maths which have a X and a Y axis. Screen coordinates work in a similar way. Here is an example of how to move the cursor to the 10th column in the 5th row.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Coordinates;<br /><br />uses<br /> crt;<br /><br />begin<br /> GoToXY(10,5);<br /> Writeln('Hello');<br />end.</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Windows</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Windows let you define a part of the screen that your output will be confined to. If you create a window and clear the screen it will only clear what is in the window. The <em>Window</em> command has 4 parameters which are the top left coordinates and the bottom right coordinates.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Coordinates;<br /><br />uses<br /> crt;<br /><br />begin<br /> Window(1,1,10,5);<br /> TextBackground(Blue);<br /> ClrScr;<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Using window(1,1,80,25) will set the window back to the normal size.</div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Sound</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The <em>Sound</em> command makes a sound at the frequency you give it. It does not stop making a sound until the<em>NoSound</em> command is used. The <em>Delay</em> command pauses a program for the amount of milliseconds you tell it to. <em>Delay</em> is used between <em>Sound</em> and <em>NoSound</em> to make the sound last for a certain amount of time.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Sounds;<br /><br />uses<br /> crt;<br /><br />begin<br /> Sound(1000);<br /> Delay(1000);<br /> NoSound;<br />end.</code></div>
</div>
Mohammed E. Katzhttp://www.blogger.com/profile/16957627613529515712noreply@blogger.com0tag:blogger.com,1999:blog-6611663679545711262.post-4113577719232162292012-08-28T02:22:00.003-07:002012-08-28T02:57:39.920-07:00Pascal Programming Lesson #1 - Introduction to Pascal<br />
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
About Pascal</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The Pascal programming language was created by Niklaus Wirth in 1970. It was named after Blaise Pascal, a famous French Mathematician. It was made as a language to teach programming and to be reliable and efficient. Pascal has since become more than just an academic language and is now used commercially.</div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
What you will need</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Before you start learning Pascal, you will need a Pascal compiler. This tutorial uses the <a href="http://www.freepascal.org/" style="color: #9900cc;">Free Pascal Compiler</a>. You can find a list of other Pascal compilers at <a href="http://www.thefreecountry.com/compilers/pascal.shtml" style="color: #9900cc;">TheFreeCountry's Pascal compiler list</a>.</div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Your first program</h2>
<div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The first thing to do is to either open your <acronym title="Integrated Development Environment">IDE</acronym> if your compiler comes with one or open a text editor.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
We always start a program by typing its name. Type <em>program</em> and the name of the program next to it. We will call our first program "Hello" because it is going to print the words "Hello world" on the screen.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Hello;</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Next we will type <em>begin</em> and <em>end.</em> We are going to type the main body of the program between these 2 keywords. Remember to put the full stop after the <em>end</em>.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Hello;<br /><br />begin<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The <em>Write</em> command prints words on the screen.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Hello;<br /><br />begin<br /> Write('Hello world');<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You will see that the "Hello world" is between single quotes. This is because it is what is called a string. All strings must be like this. The semi-colon at the end of the line is a statement separator. You must always remember to put it at the end of the line.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The <em>Readln</em> command will now be used to wait for the user to press enter before ending the program.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Hello;<br /><br />begin<br /> Write('Hello world');<br /> Readln;<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You must now save your program as hello.pas.</div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Compiling</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Our first program is now ready to be compiled. When you compile a program, the compiler reads your source code and turns it into an executable file. If you are using an IDE then pressing CTRL+F9 is usually used to compile and run the program. If you are compiling from the command line with Free Pascal then enter the following:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<kbd style="color: #003300;"><br /></kbd></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<kbd style="color: #003300;">fpc hello.pas</kbd></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
If you get any errors when you compile it then you must go over this lesson again to find out where you made them. IDE users will find that their programs compile and run at the same time. Command line users must type the name of the program in at the command prompt to run it.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You should see the words "Hello world" when you run your program and pressing enter will exit the program. Congratulations! You have just made your first Pascal program.</div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
More commands</h2>
<div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<em>Writeln</em> is just like <em>Write</em> except that it moves the cursor onto the next line after it has printed the words. Here is a program that will print "Hello" and then "world" on the next line:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Hello;<br /><br />begin<br /> Writeln('Hello');<br /> Write('world');<br /> Readln;<br />end.</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
If you want to skip a line then just use <em>Writeln</em> by itself without any brackets.</div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Using commands from units</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The commands that are built into your Pascal compiler are very basic and we will need a few more. Units can be included in a program to give you access to more commands. The crt unit is one of the most useful. The<em>ClrScr</em> command in the crt unit clears the screen. Here is how you use it:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">program Hello;<br /><br />uses<br /> crt;<br /><br />begin<br /> ClrScr;<br /> Write('Hello world');<br /> Readln;<br />end.</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Comments</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Comments are things that are used to explain what parts of a program do. Comments are ignored by the compiler and are only there for the people who use the source code. Comments must be put between curly brackets. You should always have a comment at the top of your program to say what it does as well as comments for any code that is difficult to understand. Here is an example of how to comment the program we just made:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><dfn style="color: #666666;"><br /></dfn></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><dfn style="color: #666666;">{This program will clear the screen, print "Hello world" and wait for the user to press enter.}</dfn><br /><br />program Hello;<br /><br />uses<br /> crt;<br /><br />begin<br /> ClrScr;<dfn style="color: #666666;">{Clears the screen}</dfn><br /> Write('Hello world');<dfn style="color: #666666;">{Prints "Hello world"}</dfn><br /> Readln;<dfn style="color: #666666;">{Waits for the user to press enter}</dfn><br />end.</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Indentation</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You will notice that I have put 3 spaces in front of some of the commands. This is called indentation and it is used to make a program easier to read. A lot of beginners do not understand the reason for indentation and don't use it but when we start making longer, more complex programs, you will understand.</div>
</div>
<center style="background-color: white; color: #000033; font-family: sans-serif;">
<ins style="border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; display: inline-table; height: 280px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: relative; visibility: visible; width: 336px;"></ins></center>
</div>
Mohammed E. Katzhttp://www.blogger.com/profile/16957627613529515712noreply@blogger.com0tag:blogger.com,1999:blog-6611663679545711262.post-4779287527957273212012-08-28T02:06:00.000-07:002012-08-28T02:06:02.527-07:00VB.NET Programming lesson #4 - String Handling<br />
<div style="background-color: white; color: #000033; font-family: sans-serif;">
String handling is the use of various methods to manipulate a string into a form that a program needs it in. You will already know from a previous lesson that a string is a variable that stores text. All programs use text in some way since text is used to show messages to the user and for getting input from the user which makes string handling an important part of programming.</div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
String concatenation</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
String concatenation means joining 2 strings together. To join 2 strings together you can use either an ampersand (&) or a plus (+). Here is an example that uses the ampersand to join 2 strings together with a space between them.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Imports Microsoft.VisualBasic<br />Imports System<br /><br />Class StringHandling<br /> Shared Sub Main()<br /> Dim string1 As String = "Part one"<br /> Dim string2 As String = "Part two"<br /> Dim string3 As String = <b>string1 & " " & string2</b><br /> Console.WriteLine(string3)<br /> End Sub<br />End Class</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You can also use a plus instead of an ampersand.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Dim string3 As String = <b>string1 + " " + string2</b></code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
String handling commands</h2>
<div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Visual Basic.NET has various useful commands for manipulating strings. The following are some of the commands you will use most often.</div>
<h3 style="background-color: white; color: #000033; font-family: sans-serif;">
Upper and lower case</h3>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
To change all the letters in a string to uppercase letters you can use the <i>ToUpper</i> command. You can use the<i>ToLower</i> command to change them all to lowercase letters. Both of these commands and a lot of the others are called on the declared string object which means you use them by putting a dot after the name of the string followed by the command. Here is an example that demonstrates both of the commands.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Imports Microsoft.VisualBasic<br />Imports System<br /><br />Class StringHandling<br /> Shared Sub Main()<br /> Dim MyString As String = "Some Text"<br /> Dim lower As String = <b>MyString.ToLower()</b><br /> Dim upper As String = <b>MyString.ToUpper()</b><br /> Console.WriteLine(lower)<br /> Console.WriteLine(upper)<br /> End Sub<br />End Class</code></div>
<h3 style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></h3>
<h3 style="background-color: white; color: #000033; font-family: sans-serif;">
Getting part of a string</h3>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You can use the <i>Left</i> command to get a certain number of characters from the left of a string and there is also a <i>Right</i> command that does the same from the right side of a string. Both commands take 2 paramaters. The first parameter is the string to work with and the second parameter is the number of characters to get from the string. Here is an example that takes 4 characters from the left of a string and also 4 characters from the right.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Imports Microsoft.VisualBasic<br />Imports System<br /><br />Class StringHandling<br /> Shared Sub Main()<br /> Dim MyString As String = "Some Text"<br /> Dim LeftPart As String = <b>Left(MyString, 4)</b><br /> Dim RightPart As String = <b>Right(MyString, 4)</b><br /> Console.WriteLine(LeftPart)<br /> Console.WriteLine(RightPart)<br /> End Sub<br />End Class</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You can use the <i>Substring</i> command to get part of a string from anywhere in a string. The <i>Substring</i>command has 2 parameters which are the position to start copying characters from and then the number of characters to copy. The start position parameter is a bit confusing because the numbering of the positions of a string start at 0 instead of one so you will have to substract 1 from the actual position in a string. So if you want to start from the 5th character in a string you must actually use 4 as the start position. The number of characters parameter does not start at 0 but rather from 1 because it is counting the number of characters to copy. The 2nd parameter is optional and if left out it will just keep going until the end of the string. Here is an example that uses <i>Substring</i> to extract the middle word in a string.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Imports Microsoft.VisualBasic<br />Imports System<br /><br />Class StringHandling<br /> Shared Sub Main()<br /> Dim Fullname As String = "John Paul Smith"<br /> Dim MiddleName As String = <b>Fullname.Substring(5, 4)</b><br /> Console.WriteLine(MiddleName)<br /> End Sub<br />End Class</code></div>
<h3 style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></h3>
<h3 style="background-color: white; color: #000033; font-family: sans-serif;">
Length of a string</h3>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You can get the number of characters in a string using the <i>Length</i> property of a string. The <i>Length</i> property is not a command but a property so you must not use brackets with it. Here is an example that prints out the length of a string.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Imports Microsoft.VisualBasic<br />Imports System<br /><br />Class StringHandling<br /> Shared Sub Main()<br /> Dim MyString As String = "Some Text"<br /> Console.WriteLine(<b>MyString.Length</b>)<br /> End Sub<br />End Class</code></div>
<h3 style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></h3>
<h3 style="background-color: white; color: #000033; font-family: sans-serif;">
Searching a string</h3>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You can use the <i>IndexOf</i> command to find out if a string contains another string and at what position the string was found. The <i>IndexOf</i> command acts on a string and takes one parameter which is the string to find. It returns a number which is the position in the string that the string to find was found. The position numbering starts from 0 and not 1 so you need to be careful with it. If the string was not found then -1 is returned. Here is an example that shows how to find a string in another string using the <i>IndexOf</i> command.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Imports Microsoft.VisualBasic<br />Imports System<br /><br />Class StringHandling<br /> Shared Sub Main()<br /> Dim MyString As String = "A needle in a haystack"<br /> Dim FoundPosition As Integer = <b>MyString.IndexOf("needle")</b><br /> Console.WriteLine(FoundPosition)<br /> End Sub<br />End Class</code></div>
<h3 style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></h3>
<h3 style="background-color: white; color: #000033; font-family: sans-serif;">
Replacing parts of a string</h3>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You can use the <i>Replace</i> command to replace parts of a string. The command takes 2 parameters. The first parameter is the string to find in the string and the second parameter is the string to replace it with. Here is an example that replaces the word "dogs" with the word "cats".</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Imports Microsoft.VisualBasic<br />Imports System<br /><br />Class StringHandling<br /> Shared Sub Main()<br /> Dim MyString As String = "I like dogs"<br /> Dim NewString As String = <b>MyString.Replace("dogs", "cats")</b><br /> Console.WriteLine(NewString)<br /> End Sub<br />End Class</code></div>
<h3 style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></h3>
<h3 style="background-color: white; color: #000033; font-family: sans-serif;">
Trimming</h3>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You can remove any spaces from both the beginning and the end of a string by using the <i>Trim</i> command. This is useful for when you ask a user to enter something because they sometimes add spaces by mistake. Here is an example of how to use it.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Imports Microsoft.VisualBasic<br />Imports System<br /><br />Class StringHandling<br /> Shared Sub Main()<br /> Dim MyString As String = " Some Text "<br /> Dim NewString As String = <b>MyString.Trim()</b><br /> Console.WriteLine(NewString)<br /> End Sub<br />End Class</code></div>
<h3 style="background-color: white; color: #000033; font-family: sans-serif;">
</h3>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></h2>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
String data type conversions</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You often need to convert other data types such as integers to strings before you can output them to the user. For example, if you want to display a message to the user with a number that was calculated in the program then you will have to convert that number to a string and then concatenate it with the message text. The<i>Convert.ToString</i> command will convert things to a string most of the time. All objects also have a <i>ToString</i>command on them which produces a string representation of the object but this method is not as reliable as<i>Convert.ToString</i> because it sometimes produces unexpected results.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You can convert a string to a lot of other types using the various <i>Convert</i> methods such as <i>Convert.ToInt32</i> to convert a string to an integer. This kind of thing is useful for when a user enters some text that needs to be converted to an integer before calculations can be done on it.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Here is an example that converts both from a string and to a string. It gets the user to enter 2 numbers and then adds them together and then outputs the result along with a nice message.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Imports Microsoft.VisualBasic<br />Imports System<br /><br />Class StringHandling<br /> Shared Sub Main()<br /> Console.WriteLine("Please enter first number:")<br /> Dim Num1 As String = Console.ReadLine()<br /> Console.WriteLine("Please enter second number:")<br /> Dim Num2 As String = Console.ReadLine()<br /> Dim int1 As Integer = <b>Convert.ToInt32(Num1)</b><br /> Dim int2 As Integer = <b>Convert.ToInt32(Num2)</b><br /> Dim result As Integer = int1 + int2<br /> Dim message As String = "The result is " & <b>Convert.ToString(result)</b><br /> Console.WriteLine(message)<br /> End Sub<br />End Class</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></h2>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Characters and strings</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Strings are actually just sequences of characters. If you want to get an individual character within a string all you need to do is put brackets behind the name of the string with the number of the position of the character you want. This position is an index which means it starts at 0 and not at 1. Here is an example that gets the 3rd character (index 2) from a string.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Imports Microsoft.VisualBasic<br />Imports System<br /><br />Class StringHandling<br /> Shared Sub Main()<br /> Dim MyString As String = "Something"<br /> Dim MyChar As Char = <b>MyString(2)</b><br /> Console.WriteLine(MyChar)<br /> End Sub<br />End Class</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></h2>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Escaping characters</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Visual Basic.NET uses double quotes around the contents of a string as you already know very well by now. The problem with this is that when you want to put actual double quotes in a string then it thinks that you are closing the string. To get around this problem you need to escape the double quotes by using 2 of them instead of 1. The 2 double quotes will show up as 1 double quote in the program.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Imports Microsoft.VisualBasic<br />Imports System<br /><br />Class StringHandling<br /> Shared Sub Main()<br /> Dim MyString As String = "Putting <b>""</b>quotes<b>""</b> in a string"<br /> Console.WriteLine(MyString)<br /> End Sub<br />End Class</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></h2>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Other string handling commands</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
There are many more string handling commands that you can use but we have covered the most useful ones. If you are interested in more of them then have a look at <a href="http://msdn.microsoft.com/en-us/library/system.string_members.aspx" style="color: #9900cc;">Microsoft's String class members reference</a>.</div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></h2>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Practice</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Write a program that asks the user for their full name. Extract the first name and the last name from the full name entered by the user by using the commands you have learnt in this lesson. Finding the position of the space between the two names will help you solve this problem.</div>
</div>
Mohammed E. Katzhttp://www.blogger.com/profile/16957627613529515712noreply@blogger.com0tag:blogger.com,1999:blog-6611663679545711262.post-19423034884280051542012-08-28T02:05:00.002-07:002012-08-28T02:05:57.293-07:00VB.NET Programming lesson #3 - Variables<br />
<div style="background-color: white; color: #000033; font-family: sans-serif;">
A variable is something that is used in a program to store data in memory. Variables are used for storing the data that a program is busy working with. Variables can store all sorts of different types of data including things like numbers and text. Variables are important because it is almost impossible to write a useful program that doesn't use them.</div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Declaring a variable</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Here is an example of how to declare a variable followed by an explanation.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Imports Microsoft.VisualBasic<br />Imports System<br /><br />Class Variables<br /><br /> Shared Sub Main()<br /> <b>Dim MyInt As Integer</b><br /> End Sub<br /><br />End Class</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The variable is declared using the <i>Dim</i> keyword followed by the name of the variable which in this case is<i>MyInt</i>. After that is the <i>As</i> keyword followed by the data type of the variable which is an Integer in this example. An Integer is a variable that stores numbers. There are different types of variables for storing different types of data which you will learn about later.</div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Setting the value of a variable</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You set the value of a variable using an equals sign (=). Here is an example of how to set the value of a variable.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Imports Microsoft.VisualBasic<br />Imports System<br /><br />Class Variables<br /><br /> Shared Sub Main()<br /> Dim MyInt As Integer<br /> <b>MyInt = 5</b><br /> End Sub<br /><br />End Class</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
In the above example the variable called <i>MyInt</i> has been set to the value of 5.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The <i>Console.WriteLine</i> command can be used to display the value of a variable. You must not use double quotes with <i>Console.WriteLine</i> when using a variable like you had to when printing words on the screen because then the compiler thinks you want to print the name of the variable instead of the value it holds. Here is how you print the value of a variable.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Imports Microsoft.VisualBasic<br />Imports System<br /><br />Class Variables<br /><br /> Shared Sub Main()<br /> Dim MyInt As Integer<br /> MyInt = 5<br /> <b>Console.WriteLine(MyInt)</b><br /> End Sub<br /><br />End Class</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You can set the value of a variable at the same time as you declare it which is called initializing a variable. Here is an example of how to initialize a variable.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Imports Microsoft.VisualBasic<br />Imports System<br /><br />Class Variables<br /><br /> Shared Sub Main()<br /> <b>Dim MyInt As Integer = 7</b><br /> End Sub<br /><br />End Class</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You can declare more than one variable by adding another variable name to the same declaration in which case you must separate the variable names using a comma. Another way of doing it is to just declare another variable on a new line which a lot of programmers prefer doing because then the variables can be initialized easily.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Imports Microsoft.VisualBasic<br />Imports System<br /><br />Class Variables<br /><br /> Shared Sub Main()<br /> <b>Dim MyInt1, MyInt2 As Integer<br /> Dim MyInt3 As Integer</b><br /> End Sub<br /><br />End Class</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Calculations using variables</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You can perform calculations on variables using one of the 4 basic operations which are addition, subtraction, multiplication and division. The result of an operation must be stored in a variable otherwise it is lost. Here is an example in which the numbers 2 and 3 are added together and the result is stored in a variable called<i>MyInt</i> which is printed on the screen.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Imports Microsoft.VisualBasic<br />Imports System<br /><br />Class Variables<br /><br /> Shared Sub Main()<br /> Dim MyInt As Integer<br /> <b>MyInt = 2 + 3</b><br /> Console.WriteLine(MyInt)<br /> End Sub<br /><br />End Class</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You can see from the example above that the plus sign (+) is used for adding numbers. Here are the signs for the 4 operations.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<table border="1" style="background-color: white; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; color: #000033; font-family: sans-serif;"><tbody>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">+</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">Add</td></tr>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">-</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">Subtract</td></tr>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">*</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">Multiply</td></tr>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">/</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">Divide</td></tr>
</tbody></table>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You can use variables in calculations just like you can use numbers. Here is an example in which 2 variables are declared and given values and then one is subtracted from the other and the result is stored in a third variable.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Imports Microsoft.VisualBasic<br />Imports System<br /><br />Class Variables<br /><br /> Shared Sub Main()<br /> Dim MyInt1 As Integer = 10<br /> Dim MyInt2 As Integer = 8<br /> Dim Result As Integer<br /> Result = MyInt1 - MyInt2<br /> Console.WriteLine(Result)<br /> End Sub<br /><br />End Class</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Data types</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
So far we have only been using the Integer data type which can store only numbers. If you want to store other types of data then you need to use a different data type.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The String data type is used for storing text. When you set the value of a string to some text you must surround it with double quotes because this tells the compiler that the text between the quotes is some text and not part of the code that the program must run. Here is an example of how to declare a string called <i>MyString</i> and set its value.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Imports Microsoft.VisualBasic<br />Imports System<br /><br />Class Variables<br /><br /> Shared Sub Main()<br /> Dim MyString As String<br /> MyString = "Some text"<br /> Console.WriteLine(MyString)<br /> End Sub<br /><br />End Class</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The Char data type is similar to a String except that you can only store 1 character in it. Most of the time you will use Strings instead of Chars but it is still important to understand how Chars work. Here is an example of how to declare a Char variable called <i>MyChar</i> and set its value to the letter V.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Imports Microsoft.VisualBasic<br />Imports System<br /><br />Class Variables<br /><br /> Shared Sub Main()<br /> Dim MyChar As Char<br /> MyChar = "V"<br /> Console.WriteLine(MyChar)<br /> End Sub<br /><br />End Class</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Integers can only store whole numbers and can't store numbers with a decimal point. To be able to work with numbers with a decimal point you must use the Decimal data type. Here is an example in which the number 10 is divided by 4 which gives a result that has a decimal point and therefore needs to be stored in the Decimal data type.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Imports Microsoft.VisualBasic<br />Imports System<br /><br />Class Variables<br /><br /> Shared Sub Main()<br /> Dim MyDec As Decimal<br /> MyDec = 10 / 4<br /> Console.WriteLine(MyDec)<br /> End Sub<br /><br />End Class</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The Boolean data type is used for storing either the value True or the value False and nothing else. It doesn't seem very useful but you will find later that it actually is. Understanding how Booleans work is very important for helping you understand how certain other things in programming work which you will come across later. Here is an example of how to declare a Boolean variable called MyBool and set its value to True and to False.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Imports Microsoft.VisualBasic<br />Imports System<br /><br />Class Variables<br /><br /> Shared Sub Main()<br /> Dim MyBool As Boolean<br /> MyBool = True<br /> Console.WriteLine(MyBool)<br /> MyBool = False<br /> Console.WriteLine(MyBool)<br /> End Sub<br /><br />End Class</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
There are many more data types but the ones you have learnt so far cover the most common things you will come across in VB.NET programming. Your knowledge of the data types you have learnt will be enough to help you figure out how to use any of the other data types.</div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Storing user input</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
If you want to get the user to type something in and use it in your program then you can do that by using the<i>Console.ReadLine</i> command which waits for the user to type some text and press Enter. When the user has pressed Enter the text the user entered is returned by <i>Console.ReadLine</i> so you have to store the value returned by the command in a variable. Here is an example in which the user is asked to type in some text and then the text the user entered is printed out again.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Imports Microsoft.VisualBasic<br />Imports System<br /><br />Class Variables<br /><br /> Shared Sub Main()<br /> Console.WriteLine("Please enter something:")<br /> Dim EnteredText As String<br /> <b>EnteredText = Console.ReadLine()</b><br /> Console.WriteLine("You entered:")<br /> Console.WriteLine(EnteredText)<br /> End Sub<br /><br />End Class</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Type conversions</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You will often have the need to convert variables to other data types. For example the <i>Console.ReadLine</i>command only reads in Strings so if you want the user to enter a number you have to convert the String that was read in to an Integer. There are quite a few different ways of converting between data types but you will only learn a few of them right now.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The first way of converting between data types is using a group of commands that start with the letter C. There is a command called <i>CInt</i> for example which is used to convert other data types such as Strings to Integers. Here is an example of how to use it in which the user is asked to enter a value which is stored in a String and then converted to an Integer using <i>CInt</i> and then shown again to the user.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Imports Microsoft.VisualBasic<br />Imports System<br /><br />Class Variables<br /><br /> Shared Sub Main()<br /> Console.WriteLine("Please enter a number:")<br /> Dim EnteredText As String<br /> EnteredText = Console.ReadLine()<br /> Dim MyInt As Integer<br /> <b>MyInt = CInt(EnteredText)</b><br /> Console.WriteLine("The converted value is:")<br /> Console.WriteLine(MyInt)<br /> End Sub<br /><br />End Class</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Here are the most useful C conversion commands:<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
CInt (Convert to Integer)<br />CStr (Convert to String)<br />CChar (Convert to Char)<br />CDec (Convert to Decimal)<br />CBool (Convert to Boolean)</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Another way to convert between types is to use the commands that begin with <i>Convert.To</i> . The <i>.To</i> is followed by the data type such as <i>Convert.ToString</i>. The command for converting to an Integer in this way is<i>Convert.ToInt32</i>. The advantage of the Convert commands over the C commands is that they are also used in programming languages such as C# which you might want to use one day. Here is an example that is the same as the previous one but using <i>Convert.ToInt32</i> instead of <i>CInt</i>.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Imports Microsoft.VisualBasic<br />Imports System<br /><br />Class Variables<br /><br /> Shared Sub Main()<br /> Console.WriteLine("Please enter a number:")<br /> Dim EnteredText As String<br /> EnteredText = Console.ReadLine()<br /> Dim MyInt As Integer<br /> <b>MyInt = Convert.ToInt32(EnteredText)</b><br /> Console.WriteLine("The converted value is:")<br /> Console.WriteLine(MyInt)<br /> End Sub<br /><br />End Class</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Here are the most useful Convert commands:<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Convert.ToInt32 (Convert to Integer)<br />Convert.ToString (Convert to String)<br />Convert.ToChar (Convert to Char)<br />Convert.ToDecimal (Convert to Decimal)<br />Convert.ToBoolean (Convert to Boolean)</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The <i>CType</i> command can be used for when the data type you want to convert to doesn't have either one of the above conversion methods because it can convert to any type. <i>CType</i> takes 2 parameters which are the value to be converted and then the name of the data type to convert to. Here is the same example from above again but this time it uses <i>CType</i>.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Imports Microsoft.VisualBasic<br />Imports System<br /><br />Class Variables<br /><br /> Shared Sub Main()<br /> Console.WriteLine("Please enter a number:")<br /> Dim EnteredText As String<br /> EnteredText = Console.ReadLine()<br /> Dim MyInt As Integer<br /> <b>MyInt = CType(EnteredText, Integer)</b><br /> Console.WriteLine("The converted value is:")<br /> Console.WriteLine(MyInt)<br /> End Sub<br /><br />End Class</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Constants</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
A constant is a variable whose value can't be changed. You use a constant when you specifically don't want the value of a variable to change during the execution of your program. Constants can be used for things like the company name of the company which wrote the program because the company name must not change anywhere. All you have to do to declare a constant is change the <i>Dim</i> keyword in the normal variable declaration to <i>Const</i>. The value of the constant must be initialized on the same line as it is declared otherwise you will get a compiler error. Here is an example of how to declare a constant for a company name.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Imports Microsoft.VisualBasic<br />Imports System<br /><br />Class Variables<br /><br /> Shared Sub Main()<br /> <b>Const CompanyName As String = "ABC Company"</b><br /> Console.WriteLine(CompanyName)<br /> End Sub<br /><br />End Class</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Practice</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Write a program that asks the user to enter 2 numbers. The program must then add the 2 numbers together and print out the result. Change the program to work with subtraction, multiplication and division and test them all out. Remember that you will have to convert the user's input to an Integer before it can be used in a calculation.</div>
Mohammed E. Katzhttp://www.blogger.com/profile/16957627613529515712noreply@blogger.com0tag:blogger.com,1999:blog-6611663679545711262.post-90817906272237299682012-08-28T02:05:00.001-07:002012-09-06T13:11:11.505-07:00VB.NET Programming lesson #2 - Your First Program<br />
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The first thing you need to do is create a source file. A source file is a text file that contains the source code or in other words the commands that the program must run. VB.NET source files usually have the file extension .vb. We are going to be using Notepad to write our VB.NET code.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Create a text file called hello.vb somewhere where you can easily find it. I am going to create a folder called<i>vbnet</i> in the root of my C drive and then create the source file in there which might be a good idea for you too. Make sure you have the file open in Notepad and then add the lines of code that follow to your source file. Each line of code is explained as I go along.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Imports Microsoft.VisualBasic<br />Imports System</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The <i>Imports</i> keyword is used to import namespaces. This means that you can use the commands in that namespace. The Microsoft.VisualBasic namespace contains commands that you will be using very often that are specific to Visual Basic.NET. The System namespace contains a lot of useful commands and classes, one of which is the command to write things on the screen which you will see later. There are lots of other namespaces to import but the two in the above example are the most important.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Class Hello</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The above line containts the <i>Class</i> keyword which is used to define a class. It is followed by the name of the class which in this case is <i>Hello</i>. A class is a program module and most of the time you need to put your programs in classes but you will find out why in a later lesson.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Shared Sub Main()</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
This line declares the main subprocedure. A subprocedure is a block of code and is used to group parts of a program together. The <i>Shared</i> keyword is something you don't have to worry about now but what it does is allow the subprocedure to be run without instantiating the class. The <i>Sub</i> keyword is used to show that you are defining a subprocedure. <i>Main</i> is the name of the subprocedure and it is where the program starts executing commands from. The two brackets after <i>Main</i> are used for containing parameters but you don't have to worry about that right now.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Console.WriteLine("Hello")</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The <i>Console.WriteLine</i> command writes text on the screen. It is followed by brackets which contain the text to write on the screen. All types of text must be put inside double quotes like in the example above because this helps to make sure that it is interpreted by Visual Basic.NET as text instead of as a command.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Console.ReadKey()</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<i><br /></i></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<i>Console.ReadKey</i> is a command that waits for the user to press a key before carrying on with the program. If you want to double click on the .exe file of your program to run it instead of running it from a command prompt then you will need this command because the console window immediately disappears after writing <i>Hello</i> if you don't add it in.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">End Sub</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
This line is used to end a subprocedure which in this case is the <i>Main</i> subprocedure. Every time you declare a subprocedure you must remember to use <i>End Sub</i> to end it.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">End Class</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<i><br /></i></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<i>End Class</i> works in the same way as <i>End Sub</i> but is used for ending a class. Every class must be ended with it.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
That is all the code for your first program. Here is what the whole program should look like.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Imports Microsoft.VisualBasic<br />Imports System<br /><br />Class Hello<br /><br /> Shared Sub Main()<br /> Console.WriteLine("Hello")<br /> Console.ReadKey()<br /> End Sub<br /><br />End Class</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You will notice that I have moved some parts of the program a little bit to the right. This is called indenting and is used to make it easier to see the structure of your code. Indenting your code will make it much easier to read and write but you will only start appreciating how important it is when you start writing bigger programs. I have also used empty lines in some places to make the code easier to read. All of this is standard practice so you should get used to it now rather than later.</div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Compiling the program</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
To be able to run a program you first need to compile it. When you compile a program, you use a program called a compiler which reads your source code and produces an executable file with a .exe extension. Your source file is a human readable version of what your program should do and the executable is the machine readable program that is actually run by the computer.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
To compile your program you need to make sure that you have followed the instructions in the previous lesson. If you have then you will have the .NET Framework SDK installed which includes a Visual Basic.NET compiler called vbc.exe.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<b><br /></b></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<b>VERY IMPORTANT</b><br />You will not be able to use the vbc command unless the .NET Framework SDK directory is in your system path. You might find a program on your start menu after installing the .NET Framework SDK that runs a command prompt with the path set properly. If you are unable to find it then your need to add it to your path by yourself. This is a very complex process which can be done in many different ways depending on what operating system you have and what version of the .NET Framework SDK you have. One easy way to get around the problem is to create a .bat file like the following one and run it when you want to compile programs using vbc.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<kbd style="color: #003300;"><br /></kbd></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<kbd style="color: #003300;">@ECHO OFF<br />ECHO VB.NET Programming Environment<br />@SET PATH=%PATH%;C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727<br />ECHO PATH=%PATH%<br />CD \vbnet<br />%ComSpec%</kbd></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Save the bat file as vbprgenv.bat in your <i>vbnet</i> folder that you created earlier. You might need to change some things in the .bat file to get it to work if you aren't using the 2.0 version of the .NET Framework but in most cases it should work.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You now need to open a command prompt window or run the .bat file from above depending on how you have chosen to do it. Change to the directory in which you saved your source file if you are not already in it. Now make sure that you have saved the source file you created because the compiler can't compile a file that hasn't been saved. Type the following command and press Enter to compile the program.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<kbd style="color: #003300;"><br /></kbd></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<kbd style="color: #003300;">vbc hello.vb</kbd></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
If you have done everything correctly then it will compile the program without printing any error messages. If you see error messages then you need to go back through the instructions and figure out what you have done wrong. If the program compiled succesfully then an executable file called hello.exe will be created in the same directory as your source file. You can run it be either double clicking on it or by typing in its name on the command prompt and pressing Enter. The program should print <i>Hello</i> on the screen and then wait for you to press a key.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Congratulations on making your first program in Visual Basic.NET.</div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Using comments</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
A comment is something you put in a program that is ignored by the compiler but can be used by you to remind yourself of what a part of a program does or just for anything that must not be compiled into the program. You can add a comment to a program using a single quote. The text that you want in the comment must be put after the single quote. Here is an example of how to do it with the comments in bold.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><b><br /></b></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><b>' This is my first program in Visual Basic.NET</b><br /><br />Imports Microsoft.VisualBasic<br />Imports System<br /><br />Class Hello<br /><br /> Shared Sub Main()<br /> Console.WriteLine("Hello") <b>' Writes Hello on the screen</b><br /> Console.ReadKey() <b>' Waits for the user to press a key</b><br /> End Sub<br /><br />End Class</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Practice</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
To practise what you have learnt, try writing a program that writes things on the screen on multiple lines. After doing that write a program that draws a small tree on the screen using asterisks (*) and use spaces to get the asterisks in the right position.</div>
Mohammed E. Katzhttp://www.blogger.com/profile/16957627613529515712noreply@blogger.com0tag:blogger.com,1999:blog-6611663679545711262.post-14688812649552383842012-08-28T02:05:00.000-07:002012-08-28T02:05:39.566-07:00VB.NET Programming lesson #1 - Getting Started<br />
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
What is VB.NET?</h2>
<center style="background-color: white; color: #000033; font-family: sans-serif;">
</center>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
VB.NET or Visual Basic.NET is a programming language based on Visual Basic and further back than that on the BASIC programming language. It is considered one of the easier programming languages to use. It's ease of use however doesn't make it any less powerful than other programming languages.</div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Why to learn VB.NET from the command line</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
VB.NET is usually learnt using a graphical IDE (Integrated Development Environment) such as Visual Studio which is why it is called <i>Visual</i> Basic.NET. There is nothing particularly wrong with learning it from a graphical IDE but there are quite a few advantages to learning VB.NET from the command line. These advantages include:</div>
<ul style="background-color: white; color: #000033; font-family: sans-serif;">
<li>Learning how the proper flow of a program from beginning to end works</li>
<li>More of a focus is put on coding rather than the completely simple visual design work</li>
<li>It is simple to learn visual programming after learning from the command line but the opposite is not true</li>
<li>You will learn how to solve real programming problems rather than simple things like showing a few message boxes and changing the color of buttons</li>
<li>You will learn to understand what is going on behind the scenes when a program is compiled in a graphical IDE</li>
<li>You won't have to buy an expensive IDE because command line programming is almost always free</li>
<li>You will be able to adapt to ASP.NET a lot more easily later on because it processes code in the same way as a console application</li>
<li>People who first learnt programming using a graphical IDE tend to not be as good at programming as people who didn't</li>
</ul>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
It is not as easy or as much fun learning from the command line but the easiest way of doing something is often not the best way to do it. If you really must use a graphical IDE then you will still be able to follow these lessons but you will have to figure out how to create console applications in your chosen IDE. It is a good idea to use a graphical IDE such as Visual Studio because it makes it a lot easier to start programming console applications.</div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Installing the VB.NET compiler</h2>
<center style="background-color: white; color: #000033; font-family: sans-serif;">
</center>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You will need a compiler to create VB.NET programs. If you are using Visual Studio then everything you need will already be installed. If you aren't using Visual Studio then you can use the <a href="http://www.microsoft.com/downloads/details.aspx?familyid=fe6f2099-b7b4-4f47-a244-c96d69c35dec&displaylang=en" style="color: #9900cc;">.NET Framework Software Development Kit</a>. This tutorial uses the Microsoft VB.NET compiler and version 2.0 of the language. Make sure your have installed the .NET Framework Software Development Kit before continuing with the lessons.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You are now ready to start writing your first VB.NET program which you will learn how to do in the next lesson.</div>
Mohammed E. Katzhttp://www.blogger.com/profile/16957627613529515712noreply@blogger.com0tag:blogger.com,1999:blog-6611663679545711262.post-25009809262510248702012-08-28T01:47:00.002-07:002012-08-28T01:47:13.804-07:00ASP Lesson #5 - Using a Database<br />
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Creating an Access database</h2>
<div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
First we will create a database in Access called Employees.mdb. This will have a table called Employee and here is what it should look like:</div>
<table style="background-color: white; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; color: #000033; font-family: sans-serif;"><caption><br /></caption><caption>Employee</caption><tbody>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">EmpNo</td></tr>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">FirstName</td></tr>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">LastName</td></tr>
</tbody></table>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Insert the following values into the tables:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<table style="background-color: white; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; color: #000033; font-family: sans-serif;"><caption>Employee</caption><tbody>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">1</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">John</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">Smith</td></tr>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">2</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">Mary</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">Jones</td></tr>
<tr><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">3</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">James</td><td style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(204, 204, 204); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(204, 204, 204); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px;">King</td></tr>
</tbody></table>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></h2>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
The Connection object</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The first thing to do before you can use a database is create a Connection object.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Dim cn<br />Set cn = Server.CreateObject("ADODB.Connection")</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Next set the ConnectionString property of the Connection and open the Connection.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">cn.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " & Server.MapPath("Employees.mdb")<br />cn.Open</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
The RecordSet object</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
We now need a RecordSet object to store the results of a query. You must create the RecordSet in a way similar to how you created a Connection.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Dim rs<br />Set rs = Server.CreateObject("ADODB.RecordSet")</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Before you can go on with the next step you need to copy C:\Program Files\Common Files\System\ado\adovbs.inc to C:\Inetpub\wwwroot. Also add the following line after <%Option Explicit%>:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><!--#include file="adovbs.inc"--></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Now we can open the RecordSet like this:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">rs.Open "SELECT * FROM Employee", cn, adOpenStatic, adLockOptimistic</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The first parameter in quotes after rs.Open is a SQL statement that says all records from the Employee table must be retrieved. The second parameter is the connection. The Third parameter is the CursorType and the Fourth parameter is the LockType.</div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Reading from a RecordSet</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Now that we have opened the RecordSet we can read values from it. We will use a loop to write the first name of each employee in the Employee table on the page.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">While Not rs.EOF<br /> Response.Write rs("FirstName") & "<br>"<br /> rs.MoveNext<br />Wend</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The Find command can be used to find a specific record.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">rs.Find "LastName = 'King'"</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Writing to a RecordSet</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You can change the value of a field in the RecordSet by setting its value in the same way as you set the value of a variable. After you have set the value you have to use the Update command or the changes will not happen.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">rs("FirstName") = "Mike"<br />rs.Update</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Cleaning up</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You must always close all Connections and RecordSets when you are finished using them. You must also set them equal to Nothing. If you do not then they will stay in the server's memory until it has not memory left.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">rs.Close<br />Set rs = Nothing<br />cn.Close<br />Set cn = Nothing</code></div>
</div>
Mohammed E. Katzhttp://www.blogger.com/profile/16957627613529515712noreply@blogger.com0tag:blogger.com,1999:blog-6611663679545711262.post-83201563208450213122012-08-28T01:47:00.001-07:002012-08-28T01:47:06.977-07:00ASP Lesson #4 - Session Variables and Cookies<br />
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The variables that we have created so far only exist on the page that they are declared. Session variables are the same as normal variables except that they can be used by other pages. Session variables only exist for a certain amount of time which is 10 minutes by default.</div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Using session variables</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You must use the Session object to read and write session variables. Here is an example of how to set the value of a new session variable on one page and how to use it on another page:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
page1.asp</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /><code style="color: #660000;">Session("MySessionVariable") = Request("txtName")</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
page2.asp<br /><code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Response.Write Session("MySessionVariable")</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
What is a cookie?</h2>
<div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Cookies are similar to session variables because they store data that can be used by other pages. The difference is that cookies are stored on the client's computer and session variables are stored on the server. Cookies also last a lot longer that session variables.</div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Using cookies</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The Response.Cookies object is used to create and set the value of a cookie.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Response.Cookies("Name") = "John Smith"</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The Request.Cookies object is used to read the value of a cookie.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Response.Write Request.Cookies("Name")</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You can also group values under another name such as having first name and last name in the name group.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Response.Cookies("Name")("FirstName") = "John"<br />Response.Cookies("Name")("LastName") = "Smith"</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You can set how long a cookie will be kept on the client's computer before it expires using the </div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Expires property of the cookie. You must add the amount of days you want it to last to the current date when you do this.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Response.Cookies("Name") = "John Smith"<br />Response.Cookies("Name").Expires = Date + 30</code></div>
</div>
Mohammed E. Katzhttp://www.blogger.com/profile/16957627613529515712noreply@blogger.com0tag:blogger.com,1999:blog-6611663679545711262.post-46230207867605486312012-08-28T01:47:00.000-07:002012-08-28T01:47:02.929-07:00ASP Lesson #3 - Control Structures<br />
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
If Statement</h2>
<div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You can make a decision based on user input using an If statement. The If statement tests a condition and if the result is true then it runs the code that follows the If statement but if the result is false then it runs the code after the else statement. Here is an example for when someone enters their age on a form:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Dim Age<br />Age = Request("txtAge")<br />if Age >= 18 then<br /> Response.Write "You are old enough to use this site"<br />else<br /> Response.Write "You are too young to use this site"<br />end if</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Select Case Statement</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The Select Case statement is similar to the If statement because it also used for making decisions. The difference is that the Select Case statement can have many conditions and the code to run for when the result of each condition is true. Here is an example of choosing a country from a dropdown listbox:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Dim Country<br />Country = Request("lstCountry")<br />Select Case Location<br />Case "USA"<br /> Response.Write "You are from the USA"<br />Case "UK"<br /> Response.Write "You are from the UK"<br />Case "SA"<br /> Response.Write "You are from SA"<br />End Select</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
For Loop</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The For loop is used to loop from one number to another. The code inside the loop is repeated until the second number is reached. Here is an example of counting to 10 using a For loop:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Dim i<br />for i = 1 to 10<br /> Response.Write i<br />next</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
While Loop</h2>
<div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The While loop checks if a condition is true before each time that the code inside the loop is repeated. The loop variable must be set before it is used in a While loop and the loop variable must be incremented inside the loop.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Dim j<br />j = 1<br />While j <= 10<br /> Response.Write j<br /> j = j + 1<br />Wend</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Do Loop</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The Do loop is the same as the While loop except that the condition is tested at the bottom instead of at the top of the loop.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">dim k<br />k = 1<br />do<br /> Response.Write k<br /> k = k + 1<br />loop until k > 10</code></div>
</div>
<center style="background-color: white; color: #000033; font-family: sans-serif;">
<ins style="border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; display: inline-table; height: 280px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; position: relative; visibility: visible; width: 336px;"></ins></center>
</div>
Mohammed E. Katzhttp://www.blogger.com/profile/16957627613529515712noreply@blogger.com0tag:blogger.com,1999:blog-6611663679545711262.post-46610442069181250252012-08-28T01:46:00.001-07:002012-08-28T01:46:57.455-07:00ASP Lesson #2 - Forms<br />
<div style="background-color: white; color: #000033; font-family: sans-serif;">
An HTML form is a group of text boxes and buttons other things that allow a user to enter data. Here is the form that you see quite often which we will be using:</div>
<form style="background-color: white; color: #000033; font-family: sans-serif;">
<br />Username: <input type="text" /><br />Password: <input type="text" /><br /><input type="submit" value="Login" /></form>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Here is the HTML code for the form:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><form method="get" action="showdetails.asp"><br />Username: <input type="text" name="txtUsername"></input><br />Password: <input type="text" name="txtPassword"></input><br /><input type="submit" value="Login"></input><br /></form></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The method="get" will show the querystring as part of the address in your web browser. The querystring is all the things that come after the ? in an address. Use method="post" to hide the querystring. action=showdetails.asp means that when the Login button is clicked it will go to showdetails.asp where we will process the data from the form.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You can save this form along with all the other code that you need for a basic HTML page as login.htm. The page with the form doesn't have to be an asp page because it doesn't process anything. It is the showdetails.asp page that does all the processing.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Now we will create showdetails.asp. First type the basic code required for an HTML page and then the code that is required for an ASP page. Here is an example:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><% @Language = VBScript %><br /><% Option Explicit %><br /><html><br /><br /><head><br /><title>My First ASP Page</title><br /></head><br /><br /><body><br /><%<br />%><br /></body><br /><br /></html></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Make sure you save both pages in C:\Inetpub\wwwroot</div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Requesting form field values</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
The Request command is used to get the values from the fields of the form. You should first declare variables to store the values that you request. Use the Dim command to declare the variables like this:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Dim Username, Password</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
If you know Visual Basic then you must remember that you can't choose what data type you want to declare a variable as like you can is Visual Basic.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Next you store the value from the form in the variable using the Request command like this:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Username = Request("txtUsername")<br />Password = Request("txtPassword")</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
We must also write the values on the page to make sure it worked using Response.Write.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Response.Write "Your Username is " & Username & "<br>"<br />Response.Write "Your Password is " & Password</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You will see that the & operator has been used to join the string "Your Username is" and the variable Username. You will also see that you can write HTML tags and that they must be between quotes when you it.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Try open http://localhost/login.htm and type something in the form fields and click the login button. Have a look at the result and make sure you understand what we have done.</div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Functions</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
There are functions such as the Date function which are built into ASP. Here is an example of how to write the date using the Date function:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Response.Write "The date is " & Date</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
There are also functions that you can use on data. The Left function for example can be used to get the first letter of the name a person entered.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Dim Name, Surname, Initial<br />Name = "John"<br />Surname = "Smith"<br />Initial = Left(Name, 1)<br />Response.Write "Hello Mr " & Initial & " " & Surname</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
There are many more functions available and you can find them on <a href="http://www.w3schools.com/vbscript/vbscript_ref_functions.asp" style="color: #9900cc;">w3schools' list of VBScript functions</a>.</div>
Mohammed E. Katzhttp://www.blogger.com/profile/16957627613529515712noreply@blogger.com0tag:blogger.com,1999:blog-6611663679545711262.post-28268099438602371912012-08-28T01:46:00.000-07:002012-08-28T01:46:39.859-07:00ASP Lesson #1 - Introduction to ASP<br />
<div style="background-color: white; color: #000033; font-family: sans-serif;">
This tutorial will teach you how to use ASP(Active Server Pages). It only includes the most important things that you need to know about ASP and leaves out the things that are not used very often. This means that you will learn ASP quicker and not have to go through boring things that you might never use.</div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Requirements</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
If you are using Windows 95 or NT 4.0 you need to download and install the <a href="http://www.microsoft.com/ntserver/nts/downloads/recommended/NT4OptPk/default.asp" style="color: #9900cc;">Windows NT 4.0 Option Pack</a>.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
If you are using Windows 98 then you need to install Personal Webserver from your Windows 98 CD.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
If you are using Windows 2000 or XP then you need to install IIS(Internet Information Services) from Control Panel->Add or Remove Programs->Add/Remove Windows Components.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
If you don't have any of those then you can get yourself an ASP webhosting account but it is a lot more difficult to learn doing it this way.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You need Microsoft Access if you want to learn about using a database with ASP and you need to know how to use Access.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Other things that you are not required to know but that will help you are:</div>
<ul style="background-color: white; color: #000033; font-family: sans-serif;">
<li>HTML Forms</li>
<li>Visual Basic</li>
<li>SQL</li>
</ul>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
First ASP Page</h2>
<div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
We will be creating our ASP pages using Notepad. Open Notepad and type the following code for a basic html page:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><html><br /><br /><head><br /><title>My First ASP Page</title><br /></head><br /><br /><body><br /></body><br /><br /></html></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Now type the following before all the HTML code:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><% @Language = VBScript %></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
This tells the webserver that we will be using VBScript as the scripting language in our ASP page. Next you must add the following line straight after the one you have just typed:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><% Option Explicit %></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
This tells the webserver that all variables must be declared before they can be used. We will learn about variables later. It is important that you put those 2 lines on all your ASP pages.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
All ASP code must be put between an opening <% and a closing %> tag. Your main ASP code should go between the <body> tags.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
We will now write the words "Hello World" on our ASP page. The Response.Write command is used to write things. Type the following between the ASP tags:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">Response.Write "Hello World"</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You can write comments that are ignored by the webserver by typing them after a '. You can write a comment which tells you what the Response.Write to see how it is done. Here is what you should type:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">' Writes the words Hello World</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Here is what you should have so far:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><% @Language = VBScript %><br /><% Option Explicit %><br /><html><br /><br /><head><br /><title>My First ASP Page</title><br /></head><br /><br /><body><br /><%<br />' Writes the words Hello World<br />Response.Write "Hello World"<br />%><br /></body><br /><br /></html></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Another way to write things is to use just an = in front of what you want to write but you can only do it this way if it is put inside its own set of ASP tags. Here is an example:</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><% ="Hello World" %></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
You must now save your ASP page. Save it as first.asp in C:\Inetpub\wwwroot. Make sure you choose All Files for the Save as Type. If you don't have a C:\Inetpub\wwwroot folder then you have not installed PWS or IIS. Now open your web browser and go to the http://localhost/first.asp. If you have done everything right then you will see a page that says </div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Hello World. Congratulations you have just made your first ASP page.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
If you do a View Source on the page that is displayed in your browser you will see that the ASP tags have disappeared. This is because everything between the ASP tags are processed before it is sent to the browser. It is important to understand that the file that the browser receives it not the same ASP file that you created. Don't try editing your ASP file by doing a View Source because it won't work.</div>
</div>
Mohammed E. Katzhttp://www.blogger.com/profile/16957627613529515712noreply@blogger.com0tag:blogger.com,1999:blog-6611663679545711262.post-33364273095206497492012-08-25T04:53:00.001-07:002012-08-25T07:59:34.673-07:00Java programming lesson #8 - Inheritance<br />
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Inheritance is the ability of objects in java to inherit properties and methods from other objects. When an object inherits from another object it can add its own properties and methods. The object that is being inherited from is called the parent or base class and the class that is inheriting from the parent is called the child or derived class.</div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
How to use inheritance</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
We will first create a parent class called Person. We will then create a child class called Student. Then we will create a program to use the Student class. The following is a Person class which has a variable for the person's name. There are also set and get methods for the name.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">class Person<br />{<br /> private String name;<br /><br /> public void setName(String n)<br /> {<br /> name = n;<br /> }<br /><br /> public String getName()<br /> {<br /> return name;<br /> }<br />}</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Now we will create the Student class that has a variable for the student number and the get and set methods for student number. The extends keyword is used to inherit from the Person class in the following example.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">class Student extends Person<br />{<br /> private String stuNum;<br /><br /> public void setStuNum(String sn)<br /> {<br /> stuNum = sn;<br /> }<br /><br /> public String getStuNum()<br /> {<br /> return stuNum;<br /> }<br />}</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Finally we have to create a program that will instantiate a Student object, set the name and student number and then print the values using the get methods.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">public class TestInheritance<br />{<br /> public static void main(String[] args)<br /> {<br /> Student stu = new Student();<br /> stu.setName("John Smith");<br /> stu.setStuNum("12345");<br /> System.out.println("Student Name: " + stu.getName());<br /> System.out.println("Student Number: " + stu.getStuNum());<br /> }<br />}</code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
</h2>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Abstract inheritance</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Abstract classes are created only for the purpose of being inherited from. In fact you can't instantiate an object from an abstract class. You must put the abstract keyword in front of the class name declaration to create an abstract class. We can change the Person class above to show how it works.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">abstract class Person<br />{<br /> private String name;<br /><br /> public void setName(String n)<br /> {<br /> name = n;<br /> }<br /><br /> public String getName()<br /> {<br /> return name;<br /> }<br />}</code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;"><br /></code></div>
<h2 style="background-color: white; color: #000033; font-family: sans-serif;">
Interfaces</h2>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
Interfaces are like abstract classes because you can't instantiate them and they are only used for being inherited from. The difference is that interfaces are not allowed to have variables unless they are constants and methods are not allowed to have a body. The point of an interface is for you to override the methods that are declared in it. You must use the implements keyword instead of extends when using interfaces. Here is an example of the Person class as an interface and the Student class that uses the Person interface.</div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<br /></div>
<div style="background-color: white; color: #000033; font-family: sans-serif;">
<code style="color: #660000;">interface Person<br />{<br /> public void setName(String n);<br /> public String getName();<br />}<br /><br />class Student implements Person<br />{<br /> private String stuNum;<br /> private String name;<br /><br /> public void setStuNum(String sn)<br /> {<br /> stuNum = sn;<br /> }<br /><br /> public String getStuNum()<br /> {<br /> return stuNum;<br /> }<br /><br /> public void setName(String n)<br /> {<br /> name = n;<br /> }<br /><br /> public String getName()<br /> {<br /> return name;<br /> }<br />}</code></div>
Mohammed E. Katzhttp://www.blogger.com/profile/16957627613529515712noreply@blogger.com0