What Is Syntax in C++?
The C++ programming language serves as an extension of the C programming languages, one of the first modern languages in the history of computation. Like any other language before and after it, C++ relies on the clarity of its syntax for its effectiveness. The syntax of C++ falls into certain categories, such as structure and declarations, operations, and flow of control.
-
Structure
-
A single line of code in C++ is called a "statement," and all statements terminate with a semicolon (";") to signal the end of that statement. Programmers call a collection of statements that work together to perform a task a "block," typically separated from other blocks by curly brackets ("{}"). By convention, line breaks are placed between statements and blocks, but the C++ compiler only pays attention to the semicolons and curly brackets when reading code. A programmer can also include comments using double backslashes ("//") , which the compiler will ignore but other programmers can read.
Declarations: Variables
-
Programmers mainly utilize two forms of declarations in C++. The first is the variable declaration. A variable contains a single value. A programmer declares a variable by defining its type and giving it a name. The name can include any character as long as that character is a letter, number, or underscore. The programmer can then assign a value to that variable using the equals sign ("="). The following example illustrates function declaration and assignment:
int x; //an integer variable named "x"
char letter; //a single character variable
float y = 5.6; //a floating-point (decimal) number, assigned the value 5.6
-
Declarations: Functions
-
The second declaration is a function declaration. A function is a block of code given a name so that it can be used repeatedly throughout a program by invoking that name. A function declaration requires the programmer to specify the return type of the function, which determines what value the function returns when completed. The declaration also requires a pair of parentheses following the name, containing the argument list. Then, a pair of curly braces enclose the function's block of code. The following examples illustrate function declaration:
int myFunc(int argument1){ //function with return value "int" and an integer argument
/*block of code*/
}
Operators
-
C++ contains the functionality to perform various mathematical and comparison operations during the course of code execution. Indeed, mathematical computation is a necessary part of C++ programming. Typical math operators exist for addition (+), subtraction (-), multiplication (*), and division (/). Operators also exist for comparison operations, such as greater than (>), less than (<), equal to (==), and not equal to (!=). The following example illustrates some simple math operations in C++:
int x = 5;
int y = 6;
int z = x + y; // z=11
z = x - y; //z=-1
z = z * x // z = (-1 x 5) = -5
x = y / y; // x = 1
Flow of Control: If...else
-
To control how a program operates, the programmer uses flow-control statements. These statements rely on conditional statements, which use the comparison operators built into C++. Conditional statements compare two or more values and return a true or false value based on the comparison. The "if" statement uses a condition to determine a course of action. If a statement is true, then the if statement will execute a block of code. Otherwise, either nothing happens, or another block of code executes if a corresponding "else" statement exists. The following example illustrates if-else statements:
if (x = 5)
{
//do something
}
else
{
//do something else
}
Flow of Control: Loops
-
In addition to if-else statements, C++ also contains loops. A loop takes a block of code and repeats it based on a conditional. The "while" loop continues to loop over a block of code until a condition is met. For example, the following while loop will continue until variable x is greater than or equal to y, and then move on:
while(x < y){
//code
}
A "for" loop works in a similar fashion, but is more suited for loops in which a specific amount of iterations are needed. A for loop takes an integer value, followed by a condition, followed by an increment number. In the following example, the for loop takes an integer "i", and continues until i equals 50. The variable i will be incremented by one for each loop repetition:
for (i; i < 50; i = i + 1){
//code
}
-