Some suggestions for learning Programming

Source: Internet
Author: User
The point of view in this article is mainly about the mistakes that are easy to make for beginners, the mistakes that are easy to take, and the old mistakes that are easy to make for programmers. Maybe this article is superfluous for you. In addition, this article not only focuses on dogmas, but also on my own practical application and practical ideas in programming. If it is helpful to you, I will not write it in white.

I. Learning programming algorithms

Probably because the visual programming tools are perfect. After learning programming, many people found that programming was so fun that they liked it. Similarly, because visual programming tools are perfect, many people think that the graphic interface is programming.

Two days ago, I had a few colleagues chatting in my office. When talking about programming, I smiled and said, "If you go online and check whether you are a programmer, you may not be able to find more than 0.1 billion ." Of course this is too exaggerated, but you have to admit that there may be more than a few hundred million people who have painted the interface with their mouse in Visual Basic, currently, primary school students and junior high school students have offered Visual Basic programming courses. After a few short courses (only 30 or 40 information technology courses in one semester, more than VB), many students said, "I will also compile the program ." -- Is it true?

I can't help but remember the computer science that I used many years ago. At that time, the computer class was nothing more than DOS, WPS, and basic. In the basic language at that time, except for the input and print statements, the running results are not displayed. Imagine, combine, and understand what is if-else-then, fore-next, and do-loop. Various algorithms have been learned. That's programming.

I saw a post in "programming space" a few days ago, discussing how to program with others, asking questions such as "How to Use the random function RND () generate 20 different positive integers between 1 and, and specify to use VB. We can see to what extent the algorithm is ignored. Now, let me compile such a program. I will compile it with my hands whether it is basic, C, Pascal, or even JavaScript or VBScript, you can search for words that are not used for many years at most. Why? Because the algorithms in these languages are the same. Syntax itself is just a shell, and algorithms are the soul.

After learning the algorithm, you can learn dozens of words and then use a programming language. You will find that basic, C, ASP, Java ...... They are all the same.

2. Define what is object-oriented

A few years ago, programming never mentioned objects. At that time, the input and output in basic and C were compiled as follows:

◆ Basic:
Rem ---------- Input and Output ---------
Input age promapt "Enter your age :"
Print "your age is:"; age; "years old"

 

◆ C:
// ------------ Input/output ---------
Printf ("Enter your age :");
Scanf ("% d", & age );
Printf ("/n your age is: % d/N", age );

Those who have learned programming in those years must still remember such a statement (at least I think it will never be forgotten). At that time, they did not mention the object. First, they did not need this because they only had the keyboard to input the device, the output settings only contain displays. The second reason is that the program at that time is also poor. For example, the designer first asks you the name and then asks you the age. The user must first enter the name and then enter the age. What about now? You can enter a few input boxes, which one do you want to enter first, and then click a button. Functions are complex, and objects must be imported.

Today's programming has been shouting "object-oriented", but many people who have recently learned programming are using object-oriented languages, instead, they do not know what object orientation is.

When I teach Visual Basic to others, I don't talk about programming with this document. I just give examples in my life as follows:

◆ Please tell me the refrigerator. shell color (Access Object Attributes, usage: Object Name. attribute name)
◆ Bicycle. color = Red (set object attributes)
◆ TV set. Open (complete a job, usage: Object Name. method name)
◆ Automobile. Driving (North, 100 km/s, 50 km) (with parameter method)
......

I don't know if this is better than simply speaking Visual Basic, but it can at least let others first feel some ideas in the programming.

I don't recommend anyone to learn face-to-face programming. I learned basic from eight years ago and structured true basic from 97 years ago, visual Basic of the face object will be switched from 98 years. During these two turning points, I encountered a lot of difficulties: the old way of thinking has been stubbornly resisting the entry of new things. At that time in 98, it took me half a year to turn the way of thinking. You are really lucky to have learned programming recently.

3. Detailed comments should be provided in the program

Sometimes I can't stand the program compiled by others: there is almost no comment in his program. If you do this, do you still understand it after a year and a half? -- What? What do you understand? There are two possibilities: You have compiled such a program for a long time, and there are only dozens of lines and hundreds of lines. You can also compile several programs with only seven or eight lines. If you have compiled several large programs, each of which has thousands of lines, you can come back and check the one that was half a year ago. Try again.

I remember when I first learned Visual Basic in 98 years, I transplanted the game HuaRong Road and the game black and white on other electronic toys to my computer, after a while, when I looked at my "HuaRong Dao", I couldn't understand it anymore. -- Note: I have comments, but I still cannot understand them. The so-called does not understand the statements, but does not understand what a statement plays in the entire program segment.

Later, I continued to make the comments more detailed. For example

◆ Description of the content represented by each variable, value range
'------------ Declare the variable -------------
Dim shifouyouqizi (0: 63) as byte 'indicates whether a pawn icon exists at a position. The value ranges from 0 to 2.
'1 indicates a player's pawn
'2 indicates a pawn with a computer,
'0' indicates no pawns

 

◆ Describes the functions, parameters, and return values of each process.
Public Function makepath (byval oldpath as string) as string
'Generally, you can use app. Path & "shuju. dat" to indicate shuju. dat in the current path.
'But if the user installs the program in the root directory of a drive, an error occurs,
'For example, disk C will return "C: shuju. dat"
'The function of this process is to add "/" after a path that does not end with "/"
'Oldpath is a path string that has not been processed
'Return the processed path string
'Usage: makepath (App. Path) & "shuju. dat"
Makepath = oldpath
If right (makepath, 1) <> "/" then makepath = makepath + "/"
End Function

 

◆ Explain the situation of each program Branch
'----------- If-else-end if structure ---------
If Rs. EOF then
'The corresponding user information is not found, and there are no qualified users
Msgbox ("No such person found! ")
Else
'There are qualified user records
'Statement below
End if

4. Structured

Some people may ask: the current language is structured. I don't want it to be difficult. Isn't that what you say?
Here I want to talk about the structure of two aspects: first, the same level of statements should be aligned as much as possible, and different levels of statements should be indented; second, we should try to use subprograms (that is, processes) as much as possible ).

For example, the following program section (this time using ASP as an example)

'-------- User Logon program ---------
If resquest. Form ("username") = "" then
'The user enters the address directly and must direct it to login. asp
Response. Redirect "login. asp"
Else
'Enter from the form
If resquest. Form ("teacherpass") = "1234567" then
'Instructor identity
Application ("teacher") = Application ("teacher") + 1
Session ("teachernumber") = Application ("teacher ")
Response. Redirect "Main. asp"
Else
'Student identity
Application ("student") = Application ("student") + 1
Session ("studentnumber") = Application ("student ")
Response. Redirect "Main. asp"
End if
End if

The above ASP program segment is handwritten when I write this article and has no function. From this program, we can see that the indentation reduces a lot of trouble for future modification.

Let's take a look at the following program. The function of the following program is to print two lines of text on the printer:

'---------- Print program ----------
Printer. currentx= 2500
Printer. currenty = 1000
Printer. fontname = ""
Printer. fontsize = 32
Printer. Print "a letter to students"
Printer. currentx= 100
Printer. currenty = 3000
Printer. fontname = "ter"
Printer. fontsize = 16
Printer. Print strnameofstudent & "Student: hello"
Printer. enddoc

The above program is not only readable, but has a large number of statements and will be difficult to modify in the future. If you use the following method, it will be much better:

'---------- Print program example 2 ----------
Printtext (, "", 32, "a letter to students ")
Printtext (, "", 16, strnameofstudent & "Student: Hello ")
Printer. enddoc

Private sub printtext (byval PTX as Singel, byval Pty as Singel, byval ptfn as string, byval ptfs as Singel, byval ptstr as string)
'Print content subprocess
'Ptx, Pty is the printing position
'Ptfn is the font size and ptfs is the font size.
'Ptstr is the printed content
'You can also add parameters such as bold and italic as needed
Printer. currentx = PTX
Printer. currenty = Pty
Printer. fontname = ptfn
Printer. fontsize = ptfs
Printer. Print ptstr
End sub

 

5. Do not use GOTO

This is a problem many years ago, but now most people do not use the GOTO statement. However, it is undeniable that some people are holding on to those old antiques. The program that uses the GOTO statement is like a noodle. It will be hard to understand it in the future.

I encountered a lot of difficulties when I switched from GW basic to true basic in, because I am very familiar with Goto statements, but I have never insisted on one. Now, I am used to programs without Goto. Two days ago, a friend showed me the program he programmed. The program itself was very good and very functional. That is, I used a few if... Then goto statements in it, which made me very unable to stand it. After so many years, these things should be lost.

Friends who have recently started learning program design should not abuse Goto, because the use of goto is not introduced in any of the current materials. Maybe you have never heard of such a statement, so it's better.

However, the on error goto statement does not need to be discarded. However, I don't like to use numbers as row numbers. My friend except for a few statements like "Goto 100", "Goto 200" in his program, and "on error goto 1000 ". If you use "on error goto killerr" and use a string for row marking, you will always be more comfortable looking at the program later. What if you see the row number before you see "on error goto? Isn't it difficult to use numbers?

6. Learning in applications

Many people learn program design just according to the example of a book. After reading a book, they will continue. This will never take the initiative. If you want to learn something more deeply and learn something useful, you can take a book to learn its examples first. After a general meeting, you can open your own books.

When you compile your own program, do not just think about the functions that can be implemented by the program language, but think about the functions that can be implemented by other application software, or even functions that are not yet implemented by other application software, then try to use the program to complete it.

If you do not compile the program, you need to compile a complete program. In this way, more problems will be encountered. What is it? You don't need to think about it: Have you ever played the jigsaw puzzle? Have you ever played Flying chess? Do you need a good address book now? Can all be edited. Just compile all of these.

Maybe you want to say that these programs are available online. What else do you want me to compile? Error! There is online, and the online is better than what you will compile soon. However, we do not need a program, but to learn.

What should I do if I have never used Visual Basic to write a program? Use F1. Msdn is more complete than any other book. Of course, msdn does not introduce algorithms or combinations of modules. But that is a problem you should have solved.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.