Posted in Computers & Tech / How-To's and Tutorials / Programming / MISC (not applicable to above ..
Author: pyost Total-Replies: 1
This is the second part of my Pascal tutorial for beginners. Here is what the complete tutorial contains, and it might get expanded (some parts are not written yet):
Conditions, as the name itself says, are parts of the code that take some condition into consideration. The easiest answer to the question "why?" would be "to make the program non-linear". Obviously, if you are creating a program, it has to do something useful, which usually means it would have to do various things. But how can you achieve that if you write line after line of code which always gets executed in the same why? That's where conditions come in handy - you can execute different parts of code depending on various things.
The IF condition
The IF condition is a piece of code that is highly likely to be used in every single application. It can also be displayed as IF - THEN - ELSE, because that is exactly how it works. The program gets a variable/condition, which can be either true or false. If it is TRUE, the program moves to "then". Otherwise, it moves to "else". The code is pretty much the same.
As already mentioned, condition can be both variable and condition. It might be a bit confusing because of the names, but the explanation will help a lot. If we want to use a variable, it must be of boolean type. This type can have only two values - true and false. Using it would look like this:
cond := true;
then writeln('True') // This one get's executed
cond := false;
else writeln('False'); // This one get's executed
Simple as that. As for conditions, they also return true and false, but have a different form - two variables being compared. So, we could have:
a < b
a <= b
a > b
a >= b
a = b
In Pascal, the equal sign (=) isn't used for assigning values, but for comparing them. (Remember, := is for assigning)
a, b: integer;
a := 15;
b := 22;
if (a < b)
then writeln('a < b') // This one get's executed
else writeln('a >= b');
One thing to be careful are the commands after THEN and ELSE. You will notice that we don't have a semi-colon after the THEN line, but we do after the ELSE line. This is because semi-colon ends the whole IF condition, and not just a part of it. If you, however, didn't want the ELSE part, you could omit it and enter the semi-colon after THEN commands. This is a completely valid option. Furthermore, THEN and ELSE don't have to contain only one command - it can also be a block of commands, by using begin..end. Again, if you have both THEN and ELSE, there mustn't be a semi-colon after then begin..end
You can always combine several boolean variables and/or conditions in order to achieve a more complex level of the IF statement. However, we will deal with this in one of the following parts. This time I just wanted to explain how the system works.
The CASE condition
It might be a bit wrong to call CASE a condition, but it can be put in the same group as IF, since it allows the use of different command blocks for different values. Unlike IF, where we have either TRUE or FALSE, in CASE we choose a variable and then set commands for the values we choose. Alternatively, it could be replaced with a number of IF commands, where every one would be IF (variable == something) then ...;. Of course, instead of doing so, we can use case.
To start with, here are a few rules. Since each case must be limited, there has to be semi-colon at the end. A consequence of this is that we can't use a semi-colon the end the CASE condition, but have to use end;. Furthermore, besides the wanted number of cases, we can also have (but it is not obligatory) and ELSE case, which matches all the values we haven't mentioned. The following is the CASE syntax incorporate into an example
number := 3
case number of // number is a variable, case..of is the syntax
1: writeln('Number one');
2: writeln('Number two');
3: writeln('Number three'); // this one gets executed
else writeln('Not one nor two nor three');
number := 21
case number of // number is a variable, case..of is the syntax
1: writeln('Number one');
2: writeln('Number two');
3: writeln('Number three');
else writeln('Not one nor two nor three'); // this one gets executed
The variable doesn't have to be a number - it can be a string, a character, and even a boolean (though it doesn't make any sense to use CASE). And once again, you can use begin..end for groups of commands.
If you wanted to do the same thing several times in a row, copying it over and over again wouldn't be a good thing. What's more, what if you don't really know how many times that code should be run (if this number is entered by the user)? That's why you need Loops, another type of statements that is used "on a daily basis". Loops are statements that enclose a command or a block of commands and repeat them certain number of times. This number can either be defined in the code itself, or a variable can be used.
The FOR loop
I consider the FOR loop to be the simplest of those that will be explained in the part of the tutorial. It is because it can be replaced by both WHILE and REPEAT, which give more control. However, it is good to start with this one in order to understand how loops work.
The important thing about FOR is that it works with integer numbers. You have to have a variable which will serve as a counter, since the FOR loop takes a certain interval into consideration. A good "translation" of the loop would be "For each number in the interval [number_one, number_two] do a command/block of commands." Number_one must ALWAYS be assigned to a variable, because it will change after each "round". Number_two can either be a number or a variable containing a number. Number_one will change until it reaches number_two, when the loop will end. If number_one is smaller than number_two (so you want it to increment), you will use TO in the loop. Otherwise, you will use DOWNTO. Here is an example the writes out all the number from 5 to 10, and then from 10 to 5.
for counter := 5 to 10 do
for counter := 10 downto 5 do
As you can see, first we assign the starting number to a variable, which can then be used throughout the loop. The second number is followed by the reserved word DO, after which we enter the command(s) we want to be executed. If there are more commands, they must be enclosed in begin..end.
Using a non-integer number in the FOR loop will produce an error, and telling the program to go from 5 up to 3, or 3 down to 5, will cause the loop not to execute at all, because there are no numbers in the interval. (this might come in handy sooner or later).
The WHILE loop
The WHILE loop is somewhat connected to the IF condition. It runs a command/block of commands IF and AS LONG a certain condition is true. The syntax is WHILE condition DO. One important thing about the while loop is that you MUST, at some point, alter the variables so the condition becomes false. Otherwise, you would get an infite loop and would have to press CTRL+BREAK to stop the execution of the program. Here's an example with a FOR and a WHILE loop, both doing the same thing.
for counter := 5 to 10 do
counter := 5
while (counter <= 10) do
counter := counter + 1;
Before the WHILE loop, we had to assing a value to the counter variable in order to write the proper value on the first "round". Also, notice how we increment the counter every time. If we didn't do so, the condition would always be true.
The REPEAT loop
REPEAT is pretty much the same as WHILE, except it checks the condition at the end of the loop, and runs the loop as long as the condition returns FALSE. The syntax is REPEAT commands UNTIL condition. This "until" tells you that the loop will and as soon as condition becomes true. Furthermore, because of a starting and an ending word in its syntax (REPEAT/UNTIL), this loop doesn't need begin..end for multiple commands.
Unlike the WHILE loop, which might not get executed at all (if the condition is false at the beginning), the REPEAT loop will always run at least once. This is useful if you want to use the thing you read inside the loop in the condition. With WHILE, you wouldn't have a value on the first, because it would be read inside the loop. Because of this, we might not always be able to convert a FOR loop into a REPEAT one - when the interval in FOR is non-existent, and it doesn't run at all. And be careful not to create an infinite loop
Checking whether a number is positive or negative
The user enters a number (doesn't have to be an integer), and the program must check whether the number is positive or negative. If it is zero, a special message should be written.
if (number = 0)
then writeln('The number is zero')
if (number > 0)
then writeln('The number is positive')
else writeln('The number is negative');
After reading the number, we first check whether it is zero. If it is, we write the appropriate message. If it is not, we start the other IF condition, which is inside the ELSE part. It just check if the number is positive or negative. You might wonder why there isn't a begin..end around the second IF condition. That's because IF is one command, and THEN and ELSE are just a part of it. The last readln is there to stop the window from closing immediately after writing the message.
Writing the first positive N numbers
The user enters a positive integer number N (N>0). Our task is to print out all the numbers from one to N.
for counter := 1 to number do
So, the user wants us to write all the numbers up to number? No problem, we will just write one by one for all number from 1 to number The code says it all.
Calculating the sum of positive numbers
The user starts entering positive (real) numbers, hitting enter after each one, and the program adds all those numbers to a sum. It should do so until the user enters a zero, and write the sum after that.
sum := 0;
until (number = 0);
As already mentioned, it is always good to assign a start value to the variable, because we don't know what it contains in the beginning. After that, the program REPEATs reading numbers and adding them to the sum UNTIL it read a number which is zero. Even though we do add the zero to the sum after we read it, it doesn't bothers us much, does it? We could have used a WHILE loop, but that wouldn't suit us, because we wouldn't have had a number to check.
Loops and conditions are probably the two most important branches of programming, and it doesn't matter which language we are talking about. Without understanding these two and mastering them to a sufficiently high level, you cannot expect to make serious applications.
Posted in Computers & Tech / How-To's and Tutorials / Programming / MISC (not applicable to above ..
Author: pyost Total-Replies: 10
This is the first part of my Pascal tutorial for beginners. Here is what the complete tutorial contains, and it might get expanded (some parts are not written yet):
Nowadays, there are so many programming languages that it is quite hard to decide on which to start with. However, what people usually fail to realise is that it's not all about the language - what use is it if you don't know how to solve a problem? When looking at the problem from that angle, the best choice would be a language with syntax that is easy to understand and learn, while at the same time it is powerful enough for complex operations. Starting out with Java or a C is OK with me, but in my opinion, beginners should stick to something "lighter" - like Pascal.
If you have been convinced by the introduction paragraph, continue reading.
What do you need to start?
Since Pascal is as thoroughly developed as C# for example, you won't have to spend hours downloading the tools. There are several good compilers out there - the most popular ones are Free Pascal and Turbo Pascal. You might find it a bit hard to adjust to using it, but it's one of the best choices, being free. As soon as you acquire a decent compiler, you are ready to go!
The program layout (organisation) and syntax
As this tutorial is for complete beginners, the programs will not be complicated. This taken into consideration, I will only mention three (somewhat) important "blocks" of a program.
At the very beginning of the program there can be a line defining its name. I say can be because this part is not obligatory. It is, however, good practice to use it, since you might find it helpful in the future. The correct way to define the program name is:
"Program" is a reserved word that must be used this way. The second part, "programName", is obviously the program name. It can contain lowercase and uppercase letters, as well as numbers underscores. If you leave out this line of code, the program will work all fine, but if you decide to use it, be sure the name is descriptive enough - don't just put "myFirstLongProgramOMG".
The next important part is declaring the variables. You will use these in 99 per cent of your programs, but again, it is not a must-have. The variable block starts with "var" (another reserved word) and continues until the compiler bumps into another reserved word - that tells him there are no more variables. You will be able to read more on this topic in the next chapter.
And finally, the main part of the program - the code that will be executed. Simply enough, it should be enclosed between two reserved words - "begin" which marks the start of the code, and "end." (with a full stop) which marks the end.
Now that we have the main block, we can talk a bit about the commands. The most important thing to remember is that almost every command must end with a semi-colon. There are some exceptions that will be mentioned later in the text. By having these semi-colons, you are limiting each command, so you can practically write the whole program in one line. Of course, this is not advisable, as it will make your code extremely hard to read. A rule of the thumb is to use a new line for each command, and indent it if necessary. You might also want to put blank lines between groups of commands. Here is an example of am (aesthetically) well-written code:
// declaring the variables
// command one
// command two
// command three
// command four
// command five
Hang on, hang on! What are those double slashes? Those are one-line comments. When writing a longer program, these can help you find a specific piece of code easily. Use them well, and they will prove to be very handy. Anyway, back to the example. Here we have all the parts that were talked about - program name, variable declaring, and the main program. As you can see, commands in each block are indented only once, and grouped if necessary. Later on, you will see the advantage of multiple indenting, when the examples get more complicated.
Now we shall concentrate on the "var" part of the program. As already, explained, this is the right place to declare each variable used in the code. For every variable you must use a unique, non-reserved name (consisting of letter, numbers and underscores). Furthermore, every declarations consists of two parts - first come the new variables (their names) and then their type. When declaring more variables for one type, these should be separated by commas. Here are several examples:
var2, var3: type1;
"var1" to "var4" are variable names which will be used in the program, and "type1" and "type2" are data types that the variables will be. In the var block, the variables are assigned a data type by using a colon. And don't forget to put a semi-colon after each declaration!
Let's talk a bit about data types, since these are very important. In Pascal there are many data types - however, since we are just starting off, I will only mention two, both for numbers. The first, and probably most important one is integer. An integer is a whole number between -32768 and 32767 (this might vary depending on the compiler, but this is the safest definition). The second one is real, and it can hold decimal values (e.g. 25.2341). Its range is quite big, so you won't have to worry about it.
OK, so now we have some variables. And how do we assign them values? Quite simple. I guess you remember we used a colon for declaring variables? When assigning them values, we do not use an equal (=) sign, but colon equal - :=. The equal sign is used in true/false statements, which is covered in the next part of the tutorial. To make things more clear, here's an example:
x, y: integer;
x := 24;
z := 2314.8375;
y := 3.1415926; // this line would give you an error, since you are try to give an integer a real value!
In bigger programs, give your variables better names, so you can easily know which one holds what value.
And what if there is an error?
In the previous example, we've had an invalid line. In such cases, you would get a compiling error and the number of the line where it occurred. This is usually enough to help you set things straight. There errors are mostly easy to fix if you know the correct syntax. Other things that might produce an error include a missing semi-colon, no "end." etc.
On the other hand, an error can occur while the program is being executed. For example, you ask the user to enter an integer, and he/she enters a real number. The program would stop working, all because of the user. That's why, besides being careful about not making any coding mistakes, you must also be careful when dealing with user input.
For those not into programming, Hello World might not mean anything, but it is a rather important part of the programming process. Every beginner programmer is advised to make a Hello World program first, so he can get a glimpse of how the language works. Basically, all that this program does is write the sentence "Hello World!" on the screen. Just copy this code into a program and run it (remember, we don't always need the var part):
Short and simple - we only have two commands! The first one, writeln, writes a new line to the screen containing the text specified between the parenthesis. Notice that the text must be limited by apostrophes, and not inverted commas as it is in some other languages. The readln command is there to stop the program from closing itself. If it weren't there, the program would end after writing "Hello World", and the user would see it (it would happen really fast). This way, we tell the program to read one line (the user has to press ENTER), and then close. You can read more about these two commands in the following chapter.
Input & Output
For outputting data you can use two commands - writeln and write. Writeln writes the data and then moves the pointer to a new line. So, several writelns would give you several lines. On the other hand, several writes would print it all out in one line. You can write any variable, as well as custom text (like in the helloWorld program). If you want to write several variables, you can separate them with commas - writeln(a, b, c, 'Text', d);
Sometimes you don't want to assign values to variables yourself, but have the user enter them. There are also two commands, read and readln. When reading data enter from the keyboard, the difference can't be noticed, so you can actually use both; I advise you to use readln whenever possible. Just like for writing data, you can also choose to read multiple variables: readln(a, b, c);
A user is required to enter two integer numbers, which would then be stored in two variables - a and b. The program should then swap the variable values. For example, if user enters 3 and 4, the program would assign a the value 3, and b the value 4, after which it would swap these values, making a contain 4 and b contain 3.
a, b, temp: integer;
write('Please enter two integer numbers: ');
a:=b; // we have "temp" so with don't lose the value contained in "a" in this line
Reading and writing multiple variables
A user is required to five integer numbers, and the program should write them in the opposite order.
a, b, c, d, e: integer;
write('Please enter five integer numbers: ');
readln(a, b, c, d, e); // reading it all at once
writeln(e,' ',d,' ',c,' ',b,' ',a,' '); // we need to have blanks between numbers
readln; // so the program doesn't close
And that's all you need to know to start learning Pascal!
Posted in Computers & Tech / Programming / Scripting / Ruby
Author: Chesso Total-Replies: 14
Yeah learning even the fundamentals of a more complete language like any of the Pascal/C variants will make things a lot easier to hop and skip around. Because most languages of all types will use a fair bit from others (even the basic things like variables and there types, in-built routines, conditional statement, case/switch statements, for/while loops etc).
Posted in Computers & Tech / Programming / Programming General / Misc. Programming Languages
Author: knight17 Total-Replies: 18
Hai every one
i googled upon some good article on why you need to learn c
i am posting it here i got it from www.cprogramming.comwww.cprogramming.com
Why Learn C?
There are an awful lot of programming languages available right now -- everything from the extremely high level (such as Visual Basic) to the low level power of assembly, and a good variety of specialized options in between (Perl, Ruby, and Python are good choices for many tasks). Java has also become quite the hot programming language for some tasks, in part because of its large API and in part because the virtual machine provides some elements of security. (Garbage collection is another nice feature and can make programmers much more efficient.)
Nevertheless, there are some good reasons to learn to program in C. First, age has its advantages: C has been around for 30 years, and there is a ton of source code available. This means there's a lot to learn from, and a lot to use. Moreover, many of the issues with the language have been clearly elucidated -- it's well understood, and you can find a lot of tutorials available. Plus, with C, you get lots of strong opinions mixed with insights that you can understand.
As a result of its age and its use as the language of system programming for Unix, C has become something of the lingua franca of programming. C is a great language for expressing common ideas in programming in a way that most people are comfortable with. Moreover, a lot of the principles used in C -- for instance, argc and argv for command line parameters, as well as loop constructs and variable types -- will show up in a lot of other languages you learn so you'll be able to talk to people even if they don't know C in a way that's common to both of you.
Third, C is reasonably close to the machine. When you're working with pointers, bytes, and individual bits, things like optimization techniques start to make a lot more sense. There's also utility in knowing exactly how something works underneath the hood -- this helps a great deal when something you're trying to do in a higher level language seems way slower than expected, or just doesn't work at all. You also tend to get a better picture of advanced topics like exactly how networking works. A higher level language will make it a little bit simpler, but it'll be harder to understand what's going on, and when things stop working, it's much better to know exactly what's going on so you can fix it. Additionally, if you like computer science as a discipline, or just like knowing how things work learning the details of the system is great fun.
In fact, a lot of fun programming is done in C -- for instance, system software and data managers such as Berkeley DB. If you want to be able to do more than write a simple web app, C is a great language. If you want to write a great, fast game, C is again a great choice. You can write an entire OS in C. It'll be much harder to do so in Java, and nearly impossible in a scripting language. And the language, being succinct as C is, will probably make your fun program more elegant looking to boot.
Posted in Computers & Tech / Programming / Programming General / C, C++ & Visual C++
Author: marijnnn Total-Replies: 66
i learned c++ first and i'm glad. though java has more ready to use functions included, c++ is just better to start because you focus on the basics: you learn to make calculations, you learn to do loops and conditional stuff. you learn about classes, functions,...
once you can handle that, you can switch to java, which offers you the possibility to easily create gui's.
besides, i think that, once you know C, you know a lot of languages: c#, java, php, awk,... they are almost the same as php.
and switching to other languages like vb and stuff goes fine too, because you know how to use loops and stuff