Basic C++ Commands

"Basic C++ commands" refers to the syntax and basic instructions of the C++ programming language, which is used extensively in creating a wide variety of software applications. Object-oriented programming (OOP) is one of the new features that C++ brings to the C programming language. Fundamentally, C++ uses several instructions to carry out processes such as function definition, control flow, variable declaration, and input/output. Anyone interested in learning C++ programming should know these foundational commands, the cornerstones for developing organised and effective code.

Key topics in basic C++ commands include arithmetic operations, functions, looping mechanisms (like for and while loops), decision-making structures (like if statements and switch-case constructs), and variable types. Data can be stored in variables, and numerical values can be worked with using arithmetic operations. Programmes can make decisions based on predetermined criteria thanks to decision-making structures, and loops make it easier to repeat specific code blocks. A key component of C++ is functions, which organise and lend modularity to programmes by enclosing groups of instructions. Learning these fundamental commands is essential for those who want to become programmers since they provide the groundwork needed to take on more complex subjects and create intricate software solutions.

Why commands??

In C++, commands are necessary to tell the computer how to carry out particular actions and tasks. They offer a collection of precise and unambiguous instructions that regulate decision-making processes, specify variables, direct programme flow, and carry out repetitive activities. Programmers interact with the computer through commands in C++, which direct it through the procedures required to accomplish the intended results and functionality within a software application.

20 Basic Commands in C++

The following lists 20 fundamental C++ commands:

1. #include <iostream>:

This command lets you conduct input and output operations by including the Input/Output Stream (iostream) library.

Example

Example

#include <iostream>

2. using namespace std;:

By enabling you to use common C++ names like cout and cin without having to declare the namespace, this line is frequently used to simplify code.

Example

Example

#include <iostream>
using namespace std;

3. int main:

This defines the main function, which is where the programme starts to run. The main function returns an integer, as indicated by the int.

Example

Example

#include <iostream>
using namespace std;
int main() {

4. cout >> "Hello, World!";:

This is output to the console by the command cout \\ "Hello, World!" The output is sent to cout.

Example

Example

#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!";
}

Output:

Output

Hello, World!

5. cin >> variable;:

Stores user input in the designated variable after receiving it. For input, cin is utilised.

Example

Example

#include <iostream>
using namespace std;

int main() {
    int variable;
    cin >> variable;
}

6. int variable = 10;:

This declares and initialises an integer variable called "variable" with the value 10.

Example

Example

#include <iostream>
using namespace std;

int main() {
    int variable;
    cin >> variable;
}

7. float floatValue = 3.14;:

This declares and initialises a floating-point variable called "floatValue" with the value 3.14.

Example

Example

#include <iostream>
using namespace std;

int main() {
    float floatValue = 3.14;
}

8. character character = 'A';:

Initialises a character variable called "character" with the character 'A'.

Example

Example

#include <iostream>
using namespace std;

int main() {
    char character = 'A';
}

9. if (condition):

Starts an if statement that causes the code inside the block to run if the given condition is met.

Example

Example

#include <iostream>
using namespace std;

int main() {
    int x = 5;
    if (x > 0) {
        cout << "Positive";
    }
}

Output:

Output

Positive

10. else:

If the condition in the related if statement is false, the code inside the else block of an if statement runs.

Example

Example

#include <iostream>
using namespace std;

int main() {
    int x = -2;
    if (x > 0) {
        cout << "Positive";
    } else {
        cout << "Non-positive";
    }
}

Output:

Output

Non-positive

11. for (int i = 0; i < 5; i++):

Starts a loop that iterates through a code block each time the condition is met. There are five iterations in this example.

Example

Example

#include <iostream>
using namespace std;

int main() {
    for (int i = 0; i < 5; i++) {
        cout << i << " ";
    }
}

Output:

Output

0 1 2 3 4

12. while (condition):

Initiates a while loop that will repeat a section of code if the given condition is met.

Example

Example

#include <iostream>
using namespace std;

int main() {
    int i = 0;
    while (i < 5) {
        cout << i << " ";
        i++;
    }
}

Output:

Output

0 1 2 3 4

13. switch (variable):

Presents a switch statement that lets you compare a variable's value to several possible scenarios.

Example

Example

#include <iostream>
using namespace std;

int main() {
    int day = 3;
    switch (day) {
        case 1:
            cout << "Monday";
            break;
        case 2:
            cout << "Tuesday";
            break;
        case 3:
            cout << "Wednesday";
            break;
        default:
            cout << "Other day";
    }
}

Output:

Output

Wednesday

14. break;:

Ends the operation of a loop or switch statement and moves the focus to the following sentence that is not part of the loop or switch.

Example

Example

#include <iostream>
using namespace std;

int main() {
    for (int i = 0; i < 5; i++) {
        if (i == 3) {
            break;
        }
        cout << i << " ";
    }
}

Output:

15. return 0;:

Denotes that the main function has completed successfully. Usually, a result of 0 indicates that there were no problems during programme execution.

Example

Example

#include <iostream>
using namespace std;

int main() {
    // Program logic goes here
    return 0;
}

16. int array[3] = {1, 2, 3};:

This declares an integer array called "array" with the first three entries set to 1, 2, and 3.

Example

Example

#include <iostream>
using namespace std;

int main() {
    int array[3] = {1, 2, 3};
    cout << array[0] << " " << array[1] << " " << array[2];
}

Output:

17. int sum = addNumbers(5, 3);:

This uses the arguments 5 and 3 to call the "addNumbers" function, assigning the result to the variable "sum."

Example

#include <iostream>
using namespace std;

int addNumbers(int a, int b) {
    return a + b;
}

int main() {
    int sum = addNumbers(5, 3);
    cout << "Sum: " << sum;
}

Output:

Output

Sum: 8

18. int& refVar = variable;:

This declares a reference variable called "refVar," which points to the integer variable "variable," which is already there.

Example

Example

#include <iostream>
using namespace std;

int main() {
    int variable = 42;
    int& refVar = variable;
    cout << "Original: " << variable << ", Reference: " << refVar;
}

Output:

Output

Original: 42, Reference: 42

19. const int SIZE = 5;:

Throughout the programme, a constant variable called "SIZE" with the value 5 is declared and cannot be changed.

Example

Example

#include <iostream>
using namespace std;

int main() {
    const int SIZE = 5;
    cout << "Array Size: " << SIZE;
}

Output:

Output

Array Size: 5

Conclusion

We have studied a complete set of twenty basic C++ commands that are the foundation of C++ programming during our investigation of basic C++ commands. We worked our way up to more complex ideas like arrays, functions, references, and constants after starting with the fundamentals like input/output operations, variable declaration, and control flow structures. The included examples and outputs clarify how each command should be used in practice and provide a useful grasp of how these components work in a C++ program. These fundamental commands serve as the building blocks for more complex software development and make writing organised and effective code easier.

Through exploring the subtleties of C++ programming, we have gained a firm understanding of the syntax and features of the language. The commands in this discussion span a wide range of programming operations, from creating control structures for loops and decision-making to managing data through variables and arrays. Furthermore, functions, references, and constants improve our code's readability, maintainability, and modularity, adding to our arsenal for programming. With this basic understanding, aspiring programmers can easily tackle more complex C++ topics, setting the foundation for developing robust and sophisticated applications.

Input Required

This code uses input(). Please provide values below: