C++ is a high-performance programming language used in the creation of computer software such as operating systems, games, and high-performance applications.
C++ syntax is comparable to that of the C programming language, but it also incorporates C++-specific features such as classes and templates.
Variables and Data Structures
Variables are used to store and modify data in C++. In C++, basic data types are int (for integers), float (for floating-point values), and char (for characters). Variables must be declared before they can be used, and they can be initialized with a value during the declaration process.
Example of C++ syntax:
int x = 5;
float y = 3.14;
char z = 'a';
In the example above, x is an integer variable with the value 5, y is a float variable with the value 3.14, and z is a char variable with the value ‘a’ in the above example.
Flow Control
Regulate flow statements in C++, such as if, else, and for loops, can be used to control the flow of execution in a program. If a condition is true, the if statement is used to test it and run a block of code. If the condition in the if statement is untrue, the else statement is used to run a block of code. The for loop is used to iterate across a block of code a certain number of times.
Example:
if (x > 0) {
cout << "x is positive";
} else {
cout << "x is non-positive";
}
The if statement in the above example determines if the value of the variable x is larger than zero. If so, the message “x is positive” will be shown. Otherwise, the message “x is negative” will be shown.
for (int i = 0; i < 10; i++) {
cout << i << " ";
}
The for loop in the above example initializes the variable I to 0, then evaluates whether I is less than 10. If it is, the block of code inside the loop that prints the value of I will be run, and the value of I will be increased by 1. This procedure will be repeated until the value of I no longer falls below 10.
Functions
In C++, functions are used to structure code and execute specialized tasks. A function is a piece of code that other portions of the program can use to access. Functions can also accept arguments as input and return a value.
int add(int a, int b) {
return a + b;
}
In the above example, the function add takes two integers as input and returns the sum of those integers.
Q&A
Q: What is the distinction between C++ and C?
A: C++ is an extension of the C programming language that adds features like classes and templates.
Q: In C++, how do you declare a variable?
A: In C++, variables are declared by stating the data type first, followed by the variable name. In this case, int x;
Q: What are control flow statements used for?
A: Control flow statements are used to direct the execution of a program. They let you to put circumstances to the test and make judgments based on the results.
Exercises
- Write a program that takes in two integers as input and outputs the larger of the two.
#include <iostream>
using namespace std;
int main() {
int x, y;
cout `oaicite:{"index":0,"invalid_reason":"Malformed citation << \"Enter two integers: \";\n cin >> x >>"}` y;
if (x > y) {
cout << x << " is larger." << endl;
} else {
cout << y << " is larger." << endl;
}
return 0;
}
- Write a program that calculates the factorial of a given number using a for loop.
#include <iostream>
using namespace std;
int main() {
int num, factorial = 1;
cout `oaicite:{"index":1,"invalid_reason":"Malformed citation << \"Enter a number: \";\n cin >>"}` num;
for (int i = 1; i <= num; i++) {
factorial *= i;
}
cout << "The factorial of " << num << " is " << factorial << endl;
return 0;
}
Answers
- The program takes in two integers as input and compares them using an if-else statement. If the first integer is greater than the second, it outputs that the first integer is larger. If the second integer is greater, it outputs that the second integer is larger.
- The program takes in a number as input and uses a for loop to calculate the factorial. The variable “factorial” is initialized to 1 and is multiplied by each consecutive integer starting from 1 up to the input number. The final value of “factorial” is then outputted as the result.