c sharp Logo

Basic


Syntax

C++ syntax is the set of rules that define how C++ code is written. It includes rules for declaring variables, writing statements, and defining functions.

Variables

Variables are used to store data in C++. Variables must be declared before they can be used. The declaration specifies the name of the variable and its data type.

int my_number; // declares a variable named my_number of type int

 

Data types

Data types specify the type of data that a variable can store. C++ has a variety of built-in data types, such as int, float, and char.

int my_integer_variable; // can store integer values

float my_floating_point_variable; // can store floating-point values

char my_character_variable; // can store single characters

 

Operators

Operators are used to perform operations on data. C++ has a variety of built-in operators, such as +,-, *, and /.

int sum = my_integer_variable + 5; // adds 5 to my_integer_variable and stores the result in sum

float average = (my_integer_variable + my_floating_point_variable) / 2; // calculates the average of my_integer_variable and my_floating_point_variable and stores the result in average

 

Control statements

Control statements are used to control the flow of execution of a program. C++ has a variety of control statements, such as if, else, for, and while.

if (my_integer_variable > 0) {

  // do something

} else {

  // do something else

}

 

Functions

Functions are used to group together related code and to reuse code. Functions can take parameters and can return values.

int add_two_numbers(int a, int b) {

  return a + b;

}

 

This function takes two integer parameters and returns the sum of the two parameters.

Conclusion

These are just a few of the basics of C++. There is much more to learn, but this should give you a good foundation to get started. There are many resources available online and in libraries to help you learn more about C++.