Beginning Programming All-in-One For Dummies. Wallace Wang
Читать онлайн книгу.and end of a block of code. Descriptive words look clearer but can be more cumbersome to type. To eliminate curly brackets or descriptive words like
Begin
or End
, Python uses indentation to define the beginning and end of a block of code.
Learning programming with C
The most popular curly-bracket language is C. The C language is popular for several reasons:
Power
Efficiency
Portability
The power of C
The C language is a curious combination of assembly language and high-level languages, like BASIC. Like assembly language, C provides commands for directly manipulating every part of the computer, including memory, hard disks, and printers. Like a high-level language, C lets you focus on the logic of your program without worrying about the technical details of the computer, so you get the best of both assembly language and high-level languages.
Because C programs are nearly (note the emphasis on the word nearly) as easy to write and understand as higher-level languages but still give you the power of accessing the computer's hardware like assembly language, C is often used for creating large, complicated programs (such as operating systems and word processors) along with more exotic programs (like antivirus utilities or disk diagnostic programs).
With great power comes great responsibility, and C is no exception. Because C programs can access every part of the computer’s hardware, C programs can fail dramatically by crashing other programs, including the entire operating system.
The efficiency of C
A C compiler tends to create smaller, faster, more efficient programs than compilers for other programming languages. The reason is that the C language is much simpler and, thus, easier to translate into equivalent machine language commands.
What makes the C language simpler is its small number of commands or keywords. Keywords are special commands used in every programming language. The more keywords a programming language uses, the fewer commands you need to make the computer do something. The fewer keywords a programming language offers, the more commands you need to make the computer do something.
Think of keywords like words in a human language. The fewer words you know, the more limited your communication is. If a little kid only knows the word hot, they can only express themselves in a limited manner, such as describing something as “very hot,” “a little hot,” or “not so hot.” However, if the kid knows a lot of different words, they can express themselves much better. Rather than use two or more words to describe something as “very hot,” “a little hot,” or “not so hot,” a kid with a richer vocabulary could describe the same items as “scalding,” “warm,” or “cool.”
A programming language with a lot of keywords allows you to write a program with fewer commands. That’s great from the programmer’s point of view but inefficient from the computer’s point of view.
The more keywords used in a language, the more work the compiler needs to do to translate all these keywords into machine language. As a result, programs written in languages that use a lot of keywords tend to run much slower than programs written in C.
A C program compiles to smaller, more efficient machine language commands because instead of offering a large number of keywords, the C language offers just a handful of keywords. This makes it easy for a compiler to translate the limited number of keywords into machine language.
However, as a programmer, you need to use C’s limited number of keywords to create subprograms that mimic the built-in commands of other programming languages. Because this can be impractical, the C language often includes libraries of subprograms that mimic the built-in commands of other programming languages.
The bottom line is that C programs tend to run faster and more efficiently than equivalent programs written in other programming languages. So, if you need speed, efficiency, and access to the computer hardware, the C language is the most popular choice.
The portability of C
By using much fewer commands than most programming languages, the C language makes it easy to create compilers that can translate a C program into machine language. Because it’s so much easier to create C compilers than it is to create compilers for other programming languages, you can find a C compiler for nearly every computer and operating system.
Theoretically, this means it’s possible to take a C program, written on Windows, copy it to another computer and operating system, and run that program on a different operating system, like Linux or macOS, with little or no modifications. When you can copy and run a program on multiple computers and operating systems, the program and the language it’s written in are portable.
So, not only does C create small, fast, and efficient programs, but C also allows you to copy and run your program on different operating systems and computers. Given all these advantages, the C language remains popular despite its age (it was created in 1972).
Adding object-oriented programming with C++
Although the C programming language is popular, it’s not perfect. When object-oriented programming became popular for designing and maintaining large programs, computer scientists created an object-oriented version of C called C++.
Because more people are writing and organizing large programs with object-oriented programming, more programs are being written in C++. Some people study C so they can understand the peculiarities of the C language. When they feel comfortable with C, they start studying C++ and object-oriented programming.
Other people just skip C and start studying C++ right away. The theory is that as a professional programmer, you’ll probably wind up writing and modifying C++ programs anyway, so you may as well study C++ from the start. After you know C++, you pretty much know enough to teach yourself how to write and modify C programs, too.
A far less popular object-oriented version of C is Objective-C, which used to be Apple’s official programming language until Apple switched to Swift.
Gaining true portability with Java
Although C and C++ programs are supposed to be portable — you can copy and run them on other computers — they’re not really. Sometimes you have to make minor changes to get a C/C++ program to run on another computer, but more often, you have to make major changes.
That’s why Sun Microsystems created the Java programming language. Like C++, Java is also based on the C language, but it includes several features to make Java programs safer than C or C++ programs. Specifically, Java isolates the programmer from directly accessing the computer’s memory. This reduces the power of Java somewhat but translates into safer programs that (hopefully) won’t crash as often as C/C++ programs do.
Perhaps the most important feature of Java is its portability. Rather than try to compile a Java program into machine language for different types of processors, Java compiles Java programs into an intermediate file format called bytecode or pseudocode (also called p-code).
To run a Java program that’s compiled into bytecode, you need a free Java virtual machine (VM). As long as a computer has a Java VM, it can run a Java compiled bytecode program.
Like most promises made by computer scientists,