Professional Documents
Culture Documents
What Is Syntax in Computer Programming
What Is Syntax in Computer Programming
What Is Syntax in Computer Programming
For example, a series of English words, such as — subject a need and does sentence a verb —
has little meaning without syntax.
Applying basic syntax results in the sentence — Does a sentence need a subject and verb?
Compilers convert programming languages like Java or C++ into binary code that computers
can understand. If the syntax is incorrect, the code will not compile.
That’s why it is crucial that a programmer pays close attention to a language’s syntax. No
programmer likes to get a syntax error.
Every language has its own set of rules that make up its basic syntax. Naming conventions are a
primary component of basic syntax conventions and vary by language.
Case Sensitive. Java, C++, and Python are examples of languages that are case-sensitive.
Identifiers such as world and World have different meanings in these languages.
Languages such as Basic and SQL are insensitive, meaning world and World have the
same meaning.
Class Names. Java requires the first letter of each word in class names be upper case. For
example, class FirstJavaClass. Languages such as C or C++ use an underscore to
separate words. In C, the class name would be first_java_class.
Program Filenames. The name of a Java program file must match the class name with
the extension ‘*.java” added to the name. For example, FirstJavaClass.java would be the
name of the program file for the class FirstJavaClass. C and C++ files require a “*.c” or
“*.cpp” extension but have no other stipulations.
Different languages may have rules for adding comments, using white space, or declaring
variables.
Object-oriented languages such as Java and C use methods that have different syntax
requirements.
The first step in learning any programming language is to understand the basics such as phrase
structure, proper syntax and correctly structured code.
Understanding Syntax
Human languages have syntax. These rules stipulate word order, punctuation and sentence
structure.
Without these rules, it would be impossible to communicate in a given language. When learning
a foreign language, one of the first steps is learning its syntax.
Writing code requires the same focus on syntax. Once the code is written, it is read multiple
times by different people.
Sometimes the code may be read years after it is written, making coding standards necessary.
Coding standards can make the code easy to understand.
C Syntax
Let’s look at a C program that prints the sentence — My first line of code. All C instructions are
written in lower case.
#include
int main() {
For this example, ignore the include statement at the start of the program. All C programs must
begin with main() followed by a left curly bracket ( { ).
This convention indicates the start of the program. A right curly bracket ( } ) indicates the end.
The print function (printf) is called, followed by what is to print. The text to print must be
surrounded by quotation marks and enclosed in parentheses.
All statements must end with a semicolon ( ; ). For example, to print a second sentence add the
following statement before the right curly bracket:
The executed program would display: My first line of code just printed!
Adding Comments
Placing comments at the start of each function or subroutine is a best coding practice.
It is a concise way of letting others know what the code’s purpose was, although some
programmers prefer to use line comments.
In C, a comment would be enclosed between /* comments*/ and appear after the main()
function.
#include
int main() {
/*print function to display My first line of code just printed!*/
printf(“just printed!” );
This short example illustrates how important syntax is to writing quality code.
Failure to add a semicolon after a statement can prevent the code from compiling.
Communication
Code integration
Consistency
Clarity
The concept behind conventions is to make the code explain itself. If the code is self-
explanatory, the focus can be on design and program improvements and not on what does this
mean?
Using consistent standards means that code is predictable and discoverable when read by other
programmers.
When code does not follow conventions, it becomes disorganized and difficult to read. It
becomes what is known as spaghetti code.
The term has a negative connotation indicating that the programmer did not have the skills or
experience needed to write readable code.
Don’t give up. Look into opportunities like those offered in programs powered by Woz U. If
you’re ready for that promotion or raise, there’s no better place to begin than at Woz U.