Pre-Programming.
About Lesson

Syntax refers to the rules that define the structure of a language. Syntax in computer programming means the rules that control the structure of the symbols, punctuation, and words of a programming language.

Without syntax, the meaning or semantics of a language is nearly impossible to understand.

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?

Programming languages function on the same principles.

If the syntax of a language is not followed, the code will not be understood by a compiler or interpreter.

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.

Interpreters execute programming languages such as JavaScript or Python at runtime. The incorrect syntax will cause the code to fail.

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.

What Is Basic Syntax?

Basic syntax represents the fundamental rules of a programming language. Without these rules, it is impossible to write functioning code.

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 FirstJavaClassLanguages 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

understanding syntax programming woz-u.com

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() {

printf( “My first line of code” );

}

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:

printf( “just printed!” );

}

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.

Line comments can become a distraction if they appear too frequently.

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( “My first line of code” );

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.

Why Is Syntax Important in Programming?

Syntax improves code readability. It ensures that the four C’s of coding are maintained:

  • 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.