
Basically every programming language is the same.
Both have variables, loops, functions, statements, and others.
Yet…
…The difference is the syntax and the symbols used.
Syntax is the rules, principles, and processes that govern the structure of a programming language.1
The C++ programming language is a language born from the development of the C language.
C++ programming language syntax is the same as C language.
For those of you who have learned C programming language, I am sure you will easily understand C++ language.
Well, what are the basic syntaxes that must be understood from the C++ language?
Let's discuss…
Basic Structure of C++ Program
The basic shape or structure of the program bolted with C++ consists of three parts:
Part include
Namespace section
Function parts
The Include Declaration
In this section, we define what libraries (libraries) we will use in the program.
We can think of it as another program that we want to use in our program.
In the example above, we use the iostream library. This library contains functions for performing input and output.
Sometimes we will also find libraries that are included with the extension .h, .cpp, .hpp, .cc, .c, etc.
Example:
#includes
#includes
#includes
#include "hello.h"
All have the same meaning, namely: use other libraries into this program.
Differences in the type of file to be imported:
...h means file header from C or C++;
..cpp means the source code of C++;
.hpp means file header from C++;
.cc and .c mean the file header of C.
Then another difference is found in the symbol used to include.
If using the square brackets <...> then the program will search the library into our computer system.
Whereas those who use quotation marks, will search to the specified location there.
Example:
#include "/home/dian/hello.h"
Then the program will search the library into the /home/dian/directory/.
The Namespace Declaration
This section is actually optional, it can be written no.
In the example above, we use the std namespace. Because the functions on iostream are wrapped in std namespace.
If we do not use the std namespace, then to use the cin and cout functions in iostream must start with std::.
Like this:
std::cout << "Hello World!" << endl;
std::cout << "Learn C++!" << endl;
If you do not want to write std: constantly, then use the namespace std.
Function Parts
This section is the most important part, this is where we will write a lot of program code.
In the example above, there is a main function.
The main() function is the function that will be executed first when the program is opened.
This function must be present in every program that is made to be executed.
But…
If you only create programs that function as libraries, fungsimain() may not be created.
In addition to the main() function we can also create other functions in this section.
Example:
#includes
using namespace std;
int main(){
cout << "Hello world!" << endl;
}
void hello(){
cout << "Hello how are you?" << endl;
}
In the example above we create the hell() function under the main() function.
Can it be written above the main() function?
Be able.
Which is not possible, write it in the main function like this:
#includes
using namespace std;
int main(){
cout << "Hello world!" << endl;
void hello(){
cout << "Hello how are you?" << endl;
}
}
Writing Statement and Expression
Statements and expressions are commands written in a function.
Example:
#includes
using namespace std;
int main(){
cout << "Hello world!" << endl;
cout << "Today I learned c++" << endl;
cout << "Learning C++ is easy";
}
Look at these lines:
cout << "Hello world!" << endl;
cout << "Today I learned c++" << endl;
cout << "Learning C++ is easy";
It is a statement and expression.
Writing statements and expressions must end with point-coma (;).
If there is no koma-point, the program will error.
White Space
White Space is usually created with tabs and spaces.
C++ does not have strict rules in writing White space.
For example, we create a program like this:
#includes
usingnamespace std;
int main(){
return
}
The program will remain valid and can be compiled.
However, for the program to be easy to read you should use white space wisely. Because our program code will not only be read by the computer.
Good programmers always write human code. 😊
Code Block Writing
Code block is a collection of several statements wrapped in curly brackets {...}.
The first block is the main() function block and the second block is the loop block for.
The loop block for is inside the main() function block.
Then the question:
Can the main() block be inside the for block?
Answer: no!
Because the main() block is a function.
In addition to function blocks and classes, other blocks can be written inside the for block. Examples such as if block, while, for, do/while, etc.
Comment Writing
A comment is a piece of code that a computer will ignore. There are two ways of writing comments on C++:
Using double slash //;
and using the star slash /**/.
Example:
#includes
using namespace std;
/* This is a comment that is more than one line */
int main(){
// This is a one-line comment
cout << "Hello world!" << endl;
}
Comments are usually used to provide additional information on program code and also to disable statements or code blocks.
Example:
include
using namespace std;
this is a comment that is more than one line */
int main(){
// This is a one-line comment
cout << "Hello world!" << endl;
// cout << "C++ Tutorial for beginners" << endl;
}
Watch this line:
// cout << "C++ Tutorial for beginners" << endl;
This is a comment, not a statement anymore. Because in front of him there is a double slash (//).
String and Character Writing
String is a collection of characters…
…or we can also call it text.
The strings in the C++ program are written with double ptik markings ("...") and for characters written with a single quotation mark ('..').
Example:
#includes
using namespace std;
int main(){
// string writing example
cout << "C++ Tutorial for Beginners" << endl;
// character writing examples
cout << 'a' << endl;
}
What Next?
Congrats! 🎉
You have learned about the basic syntax structure of C++ programming.
Of course this is still not enough.
Therefore, to understand more…
…Please continue learning:
Learn C++ #04: Know Input and Output Functions
Learn C++ #05: Getting to Know Variables, Constants, and Data Types
Learn C++ #06: Using an Operator
source information from (code farmers)