Showing posts with label WriteLine. Show all posts
Showing posts with label WriteLine. Show all posts

Tuesday, August 10, 2010

Programming- Lesson #4 ~ Looping and Iteration

" If you don't think carefully, you might think that programming is just typing statements in a programming language." - Ward Cunningham

When I first started leaning programming which is few months ago, I didn't know there is so much to learn and understand. We have to pay attention to each inch of the code because any mistake may cause an error. Programming isn't about writing a statement with wired signs only, it is an artificial language that designed for a computer only to understand. Programming language create programs that control the machine itself. They have form, meaning and errors. Don't be fooled by the statements that you only see, there is much more than what it looks!

What is looping? and what does it do with programming? Basically,the programs needs to do and preform tasks repetitively sometimes, the program itself do this. We call this behavior Looping. When there is instructions that are repeating, it will stop repeating till a current condition is met, and thats called a Loop. There is usually Boolean Expression in a loop so we know if it will continue looping or not. Here is an example of the whole thing I have:
Do you see the line where it says " Please enter your name " ? That's what we are talking about today; how to make one code or a task be repeated more than one time. When you run the program, it will tells you " Please enter your name: " you write any name you like and then press Enter. It will greet the name you wrote by Hi or Hello. You can try it as many times you like, and when you get bored just press Enter (without writing any name ) to dismiss the program. Let's take a look at the program.



Now, what is Console.Write and Console.ReadLine? Console.Write is similar to Console.WriteLine but it does not add aline that breaks the text. Console.ReadLine reads the console not write it and we use it when we keep pressing Enter so the same line gets repeated ( or in other words, the program pasues till the user press Enter).

The loop works this way...when we run the program, the Boolean expression ( false or true value ) will evaulate the code and tells us wether this value is false or true. When we run the program the expression will tell us the value is wrong and do the follwoing which is " enter your name ", when we enter the name and press Enter, the code will loop again ( go back ) and do the same thing, which is enetring the name and it.


As I noticed while experimenting the program above, I pressed Enter without writing anything...for exmaple, I run the program and it told me " Please enter your name". I didn't write any name and pressed enter.. that way the expression will be false and the return value from Write.ReadLine is empty string.

The next lesson will be also about loops. I hope I am making it as simple as possible.

Friday, July 30, 2010

Programming- Lesson #1 ~ Creating Your First Program

"The function of good software is to make the complex appear to be simple" - Grady Booch

It is interesting how everything appear to us in the screen simpler than what it really is. Look now at the header of my blog, this has a long code. Look at the title of the blog, it has a code to be appeared this way. Everything on the computer, windows, and internet is edited by Computer Language, written as codes, tested and then appear to us in this way. We can use it easily by just clicking the link. I am sure almost anyone who use the computer knows this. But I like to share what I am learning now; how to program. It is not a very hard thing once you understand it; it is just like a chain. Things are linked and connected to each others...if you didn't understand the first step then you should not move on. I did not program anything yet, I am still learning and sharing my readings with you.

I first started reading about
Visual Basic Programming Language, because this language uses symantics that somehow more readable language than other programming languages which make it easier to grab and apply. So obviously, I will understand everything from the roots in a fun, easy way. You can download it from here to your computer so you can start programming. It's free! let's get started.

Open a new project to start with, but before that you need to make sure that the button "Start without Debugging" is one of the options of "Debug" in the tool bar.

This is a screenshot of the bar itself:




" Debug" is the process that trace the bugs (errors) and fix them so you use the debugger to fix them or find where they are. For example, if you have a minor mistake in the code, this debugger will fix it and tell you why the program didn't run as you expected. If you " Start Without Debugging " then the compiler will start the program without enabling the debugger to trace the errors. The next step is creating the simple program which beginner programmers always call it "Hello World!"... I found it funny, because it is really a new world (programming) to say "Hi" to.


As you insert the "Hello , world!" code between the
Sub Main , you can run the program by clicking " Start Without Debugging" in the tool bar. It is the button we added previously. And as you can see, whenever a code end or starts there should be an End Sub or Sub Main (starting the code). The "Sub" is actually a “subroutine" which what do the actual work in the program, everything in between is part of the subroutine.

Are you wondering what contains the subroutine? Well, there are two statements, they called "
Module" or "Class"…and what they do is having all the information and codes under them. And of course, there is "End Module" that ends the code. Yes, I told you everything is like a chain; everything is part of something and contains different information.


Now, you need to define the variable ( where it stores the data on the computer's memory like text, numbers, integer..etc) and specify the type of it. For example, I want to store this text value " Hello, world!". So what I do is define this variable and name it whatever I like, I would name it " World " or " Message " or any subject you want; in order to save this text in the variable, I will define it as String so I can store them on the computer. " String " is a type to define a variable, and there are many other types we will learn later on.


"Console.WriteLine" is a function in this VB Program that print the codes on the screen while running the program. So when you write Console.WriteLine(" Hello, world!") it will print " Hello , world! "on the screen. It basically print the value of the variable.


Those are the types of Variables:
1) Integer - values between -2,147,483,648 and 2,147,483,647.
2) Byte - values from 0 to 225.
3) Decimal - values with decimal places between ±1.0×10-28 and ±7.9×1028.
4) String - sequence of character.
5) Char - value of one character.
6) Boolean - logical value, True or False.

Let me give another example of different variable type.
I want to calculate the sum of 1 and 3 . Logically, they are integer, so I will use variable of type Integer. So I will write " Dim Total as Integer " - this tells the computer to define the variable as Integer type. Then I would use it as " Total = 4 ". The computer will finally calculate it and save the results as a Total variable.

And by the way, you can't use as a "Integer" value when you put quotes ("") around your variable. VB Program will receive it as a "String".

Now as you put the codes in the right places and define the variables correctly, this what you should see before running the program:



Press any key to dismiss the window.


That was our first lesson..I hope I will get to more complex programs after a while. My next lesson will be about how to use mixed Variables and deal with errors in your program.