HackYou HackYou
Totul despre Hacking,Tutoriale,Programare
HackYou
FAQFAQ  CautareCautare  InregistrareInregistrare  ProfilProfil  MembriMembri  GrupuriGrupuri  Mesaje privateMesaje private  AutentificareAutentificare 

Programming Fundamentals

 
Raspunde la subiect    Pagina de start a forumului HackYou -> General
Subiectul anterior :: Subiectul urmator  
Autor Mesaj
Connekt
Administrator
Administrator


Data inscrierii: 06/Apr/2008
Mesaje: 11

MesajTrimis: 06/04/2008 06:41    Titlul subiectului: Programming Fundamentals Raspunde cu citat (quote)

1. What is Programming ?
Programming is not at all difficult. It's just a matter of putting words in the right order, so that a computer will understand them and do what it is asked to do.
The hard part is to figure out what you want the computer to do, and to poză how a list of instructions wil explain to a machine how you want it to solve a given problem.
The other hard part is to write a good program : one that does exactly what it is sopposed to, in an efficient way, and preferably with a touch of beaty. Anybody can take an axe to a piece of wood, not everybody can make furniture with an axe.

A program is basically a list of instructions, in a certain order, to be executed by a computer. This is most obvious in one of the oldest (but not necessarily obsolete) forms of programming : batch programming. A batch is like a list of things to do. A very common batch program used to be the autoexec.bat on DOS systems. It can also be used on Windows 95/98 machines. An autoexec.bat on a DOS/Windows for Workgroups machine could look something like this :

SET TEMP=C:\TEMP

keyb be,,C:\DOS\keyboard.sys

LH C:\DOS\MSCDEX.EXE /d:mscd001

WIN


The autoexec.bat is executed automatically at startup, and this one would

SET TEMP=C:\TEMP
SET TMP=C:\TEMP
make sure that temporary files are put in the 'temp' directory
keyb be,,C:\DOS\keyboard.sys
load a driver for a standard belgian azerty keyboard
LH C:\DOS\MSCDEX.EXE /d:mscd001
start a program that gives access to CD-ROMS under DOS
WIN
start Microsoft Windows

Another example could be a 'program', say backup.bat, that copies files from one directory to another.


XCOPY C:\MYDOC D:\BACKUP /S


This copies all files from the MyDoc folder to the Backup Folder on drive D:.Files in Subdirectories of the MyDoc folder are included, by adding the /S switch.

Something similar is done with scripting. A script is a text file that is read by a program, the program then 'executes' the commands in the script. This can be used to avoid having to repat the same commands again and again : you write them down once, then tell the PC or the program to read it and do what is written there.
Let's say that you have made website. All the files are still on your computer, in the folder MySite on your hard disk (c:\mysite). You want to upload it to your webspace at Yoohoo.com. You could now create an ftp script (myscript.txt) containing the location of your web space (on youhoo's ftp server), your Yoohoo user name (Pete), password (3211), an ftp command to upload all files from your MySite folder, then close the connection. Maybe something like this :

pete
3211
lcd c:\mysite
mput *.*
bye

At a command prompt, the command ftp -s:myscript.txt would then always upload all files (mput *.*). If you have a website, and use a program such as CuteFTP to upload your pages, you can monitor the exchange of commands and replies between the server and your computer, and you'll notice this is quite similar to this script.



2. DOS Batch Programming
In the example of an autoexec.bat above, the batch file is very much like a script to configure the computer, to put it in a certain state (default location for temp files, belgian keyboard, access to CD-ROMS, then run windows). It doesn't feel like a real program. So let's add a couple of things.


interaction with users
Lets say that, after the computer has started, you want to be reminded to check your mail. One way could be to add a line that puts "don't forget to check your e-mail" on your monitor. In DOS batch language that would be ECHO don't forget to check your email . ECHO is the DOS Batch language for 'show on screen'.

Better even would be that the user is asked :"do you want to check your mail ? ", then if the user answers Yes, the e-mail program is started. If the user answers NO, nothing happens. The program should give the user a choice, then check what the user has choosen. This is done with a test.

tests, conditions
@echo off

choice "start Pegasus mail to check e-mail ?" /T:n,06
if errorlevel 2 goto end
c:\progra~1\pegasu~1\winpm-32.exe

:end


CHOICE is a DOS batch command that puts a question on the screen("start Pegasus mail to check e-mail ?" ) and then waits for the user to give an answer : by default Y for Yes, N for No. The T:n,06 switch makes that if no answer is given within 6 seconds, the program will continue as if the answer were "No" (n).This allows the you to ignore the e-mail reminder and go get some coffee while you wait for Windows to get ready with whatever it's doing during startup.
The errorlevel is a number that indicates (in this case) whether Y or N has been answered. if errorlevel 2 goto end - this is almost English; it means something like if the 2nd option (N) is chosen, go to the line (in this batfile) that starts with :end, and in all other cases, just continue with the next command.
c:\progra~1\pegasu~1\winpm-32.exe is a statement that starts my favorite email program : Pegasus Mail for Windows. You'll understand that if the user answers 'N(o)' to the question 'Start Pagasus Mail ?", this line will be skipped, as the program will go directly to the ':end'. Simple but effective.

the "IF errorlevel 2" statement is called a TEST : It can be true or false. If it's true, one thing happens, if it's not true, another (or nothing at all). Tests are very important in programming.



3. Structure in a program
Good programmers write "structured" programs. Structure in a program is obtaine d through the following means : >/p>

SEQUENCE : put things in the right order. Start with the beginning. Avoid jumping around. IF ... GO TO ... ELSE GO TO ... is a recipy for disaster.Especially if you Go To another of these GoTo contraptions. There's no way you can keep track of what this program will do, and the code will read like a plate of pasta. ("Spaghetti programming").
This is one of the reasons that some people consider programming languages such as (Visual) Basic kid's toys rather than real programming languages - the BASIC family allows the use of GOTO.
CHOICE (TEST) : If something is true, then do this, if it's not true, do something else. This is known as IF ... THEN .. ELSE ... in (Visual) Basic. A more complex test works with several cases: in case 1, do action a, in in case 2, do action b, in case 3, do action c, and so on.
REPEAT (LOOP) : Traditionally, one distinguishes between thye following kinds of loops :
repeat a given number of times
conditionally : repeat as long as (WHILE) or UNTIL a condition is true
The exact way (vocabulary, syntax)to structure your program will differ from one languague to another, but that's just the language. It's the story that counts, not the language it's written in.
In Visual Basic you may find it slightly easier to read because it sounds more like (Tarzan) English :

If a > b Then max = a Else max = b
obviously, this is part of a program (or a function) to decide which of two numbers is bigger (a or b).
In C++ it would look something like this :

if (a > b) {max = a} else {max = b};
'Real' programmers prefer a well-structured program, thus using only assignments, tests, and (structured) loops.

If a program requires the same lines of coe to be repeated more than once, you can park those lines somewhere ouside your 'main' program, and 'call' them every time you want to use them. This is called an'function' or a 'subroutine'. Say the programming language you're using (Micrusoft Visual C++ ?) has no command to clear the screen. Yet you're writing a program that asks for user input a few times, then calculates something, and you want a blank screen before you show the result. This happens several times throughout the program.
You might create a 'ClearScreen' function by having the program returning 25 blank lines. Blank lines in C++ are created with cout << ""; and you'll use a 'For' loop to repeat that 25 times. That may look as follows :


for (int i=0; i <= 25 ; i++)
{ cout << "";};

In stead of repeating those lines every time you need a blank screen, you can put them somewhere separate, in a function, and give it an obvious name, such as ClearScreen(), Every time you mention ClearScreen() in your program, it will go to (!) the function, execute the code in it, and return to the program. Very useful technique indeed.

How exactly you go about 'putting this code somewhere separate, in a function or subroutine', varies from language to language. We won't go in to that - it would lead us to far beyound the scope of this paper. So would a discussion on the many uses of subroutines and functions. But you get the idea.
>/p>



4. Variables
Imagine you write a program to add the numbers 4 and 2 together, and show the result on the monitor of your computer. In some form of BASIC, this may look like this :

Result = 4 + 2
PRINT Result


This will work, but it's rather silly. You'd have to write a different program every time you want to add different numbers. It makes sense tyo try and write something where you could give 2 numbers, let the program add them together, and put the result on your screen. The program (in Qbasic) might look as follows :

FirstNumber = INPUT
SecondNumber = INPUT

Result = FirstNumber + SecondNumber

PRINT Result



Result, firstnumber and secondnumber are variables. With the QBasic command 'INPUT' you can let the user type a number on the keyboard. It is stored in the computer's memory for later use. The name of a variable is how your program remembers which value is stored exactly where in the computer's memory. That way it can be retrieved from the memory when needed (e.g. to add the two numbers together and store the result in another memory location. You as a programmer don't need to worry where that is, the program will know it by the name of the variable.

Assigning a value to a variable
Note that assigning a value to a variable has to be written correctly : the name of the variable to the left, the value to the right.
So although FirstNumber + SecondNumber = Result is mathematically correct, you can not program it that way. You want to assign a value (the sum of the 2 numbers) to the variable 'Result', so 'Result' has to be on the lefthand side, and the value (Firstnumber+SecondNumber) to the right.

In Pascal (a programming language that was originally devellopped as a tool to teach programming), the assignment is written =: and programmers are advised to read it as 'becomes' or 'changes to' rather than 'is' or 'is equal to'. So (the value of) "Result changes to (the sum of FirstNumber and SecondNumber). This is a good way to distinguish between = (is equal to) and =: (changes to), which are in fact two separate things. C++ uses the same distinction, but uses other symbols : == (equal to) and = (changes to).
Read the following out loud, saying "is equal to" for == and "changes to" for =, and you'll see (and hear) the difference;

if (username == "Peter")
{ greeting = "Hello Peter"}
else
{ greeting = "Access Denied"};


Note (again) that the BASIC languages do not make this distinction

Variable : type, declaration, initialization.
There are different 'types' of variables. This has to do with the fact that variables are, in fact, parts of the computer's memory. How big a part ? Well, they need to be large enough to hold their value, but if they are too big they just use up memory for no good reason. Say you'll give 1 variable 16 bits of memory. 16 bits of memory can hold 2 ^ 16 different binary numbers. (Each bit kan be 1 or 0. 16 bits allow for 2 ^ 16 different combinations of 1 or 0. That's 65.536 (integer) numbers. So a variable of 16 bits can hold any number between -32.768 and +32.768. Often, that's more than enough, so you can use this type of variable (called 'integer'). In case you need larger numbers, you can use a larger type, e.g. a 'long integer' that uses more bits of memory, and thus can hold larger numbers. For characters or strings, there are also types of variabels : 1 character takes 8 bits. There are also variables that can hold decimal numbers (such as 2/3 or 3.14159 ). Depending on how precise they need to be (how many digits after the decimal point), they'll eat up less or more memory.

Giving an name to your variables and indicating their type is called the "declaration" of variables. It is usually done at the beginning of the program. Not all languages require that you declare variables at the beginning of the program - they can then be declared anywhere in the program. Some languages (Basic, ...) allow the use of variables without any declaration. It is however better to declare variables at the beginning : It makes it clear what variables will be used, it is easier to make modifications if necessary, and declaring variables (in a well-choosen type) improves performance of the program.

Variables are part of the computer's memory : it is a name for a specific location in the memory. It is always possible that this location still contains a value that is no longer used but has not been removed. Not all programming languages remove 'old' values when they look for memory locations for a new variable. (C/C++ is a typical example). Your variable would then -unexpectedly- have a value that it should not have. It is therefore usual to assign a value to all variables, as soon as possible (i.e. in or right after the declaration). This is called 'initialization' of the variables.


'Declaration'
Dim UserName as String

'Ask for user name if UserName is empty, repeat untill a value for Userbname is given
Do While Username = ""
UserName=InputBox("Enter username:")
Loop



In such a situation, if the variable 'UserName' is not empty at the start of the program (because the memory location contains a few forgotten bits ...), the program will not show the inputbox where the user can enter his/her name. It is therefore a good idea to make sure Username is empty , before the Do... Loop.


'Declaration'
Dim UserName as String

'Initalization'
UserName=""

Do While Username = ""
UserName=Input(
Loop




5. Compile or Interprete ?


6. A well-written program
Since programming is not at all difficult, anyone with more that an average interest in computers will sooner or later have a go at it and learn to write a few lines of code, probably Visual Basic (for Applications) or some form of scripting on a web page or so. It takes a bit more than that to write e good program.

A well-written program works correctly. It does what it is supposed to do. Always. Even in unexpected situations. The changes that your program woks correctly increase if you think things through before you start writing the code, then write your code in a structured manner.
A well-written program has an end. It does not allow situations that would cause the program to keep on running endlessly (get in to an endless loop). Ideally, the program also produces results within a reasonable time. Software that can forecast the wheather for tomorrow, but needs 3 days to do so, might be considered rather useless.
A well-written program is robust. It can handle unexpected situations. e.g. What would happen if you ask the user to enter a number, and he enters a character. (They will do that, by mistake or because they never read the instructions). Will your program crash ? Will it continue to work with the wrong information and therefore return a result that you can't rely on ? Or have you foreseen this so that your program will tell the user he's made a mistake, and ask him to enter the correct information ? Or what is your e-mail-program supposed to do when you hit the Send And Receive Mail button while there is no connection to the internet ?
A well-written program is efficient : no program should use up more memory, processor time or other resources than necessary.
A well-written program is easy to maintain. That means that when you look at the source code 3 months later, or if another programmer loos at your source code, you can read the code, and understand what the program does and how it does it. This means you'll have to take care of the layout of your code (use indents, ..), add explanations and comments in the code, declare variables at the beginning of the program, etc.). Jumps in the code (GoTo ...) usually do not contribute to a maintenance-friendly program.
Some people might add that a well-written program also has to be 'user-friendly'. Others will just tell the users to 'Read the Fucking Manual".
Sus
Vezi profilul utilizatorului Trimite mesaj privat
Afiseaza mesajele pentru a le previzualiza:   
Raspunde la subiect    Pagina de start a forumului HackYou -> General Ora este GMT + 2 ore
Pagina 1 din 1

 
Mergi direct la:  
Nu puteti crea un subiect nou in acest forum
Nu puteti raspunde in subiectele acestui forum
Nu puteti modifica mesajele proprii din acest forum
Nu puteti sterge mesajele proprii din acest forum
Nu puteti vota in chestionarele din acest forum
Fa-ti si tu propriul forum GRATUIT, pe www.myforum.ro