Increment and Decrement Operators
In C++, the increment and decrement operators are used to increase or decrease the value of a variable by 1, respectively. These operators come in two forms: prefix and postfix.
Increment Operator (++
):
Prefix increment (
++variable
): The value of the variable is incremented first, and then it is used in the expression.Postfix increment (
variable++
): The value of the variable is used in the expression first, and then it is incremented.
Decrement Operator (--
):
Prefix decrement (
--variable
): The value of the variable is decremented first, and then it is used in the expression.Postfix decrement (
variable--
): The value of the variable is used in the expression first, and then it is decremented.
Here's an example illustrating the use of increment and decrement operators in C++:
// Module 1 Demo
// Author: Sam Student
// Created on 5-19-2024.
// Demonstrate C++ Increment and Decrement
#include <iostream>
using namespace std;
int main() {
// create some variables
int myAge;
string myName;
cout << "Enter your Name: ";
cin >> myName;
cout << "Enter a value for your age: ";
cin >> myAge;
cout << "Name: " << myName << " Age: " << myAge << endl;
cout << "Next year you will be: " << ++myAge << endl;
return 0;
}
This C++ program demonstrates the usage of cin
for reading input from the user and the increment/decrement operators. Here's a breakdown of the code:
Include Statements: The program includes the necessary header file
iostream
to provide input/output stream functionalities.Namespace: The
using namespace std;
statement is used to avoid having to prefix standard library elements withstd::
later in the code.Variable Declaration: The program declares two variables:
myAge
of typeint
andmyName
of typestring
. These variables will hold the user's age and name, respectively.Input using cin: The program uses the
cin
object, which is associated with the standard input stream, to read values entered by the user. It prompts the user to enter their name using thecout
statement and reads the input into the variablemyName
using the>>
operator.Input using cin (continued): Similarly, the program prompts the user to enter a value for their age and reads the input into the variable
myAge
using the>>
operator.Output using cout: The program uses the
cout
statement to display the values entered by the user. It outputs the user's name and age using the variablesmyName
andmyAge
, respectively.Increment Operator: The program uses the increment operator (
++
) to increment the value ofmyAge
by 1. It then outputs the incremented value using thecout
statement.End of Program: The program reaches the end, and the
main
function returns 0, indicating that the program has finished successfully.
Overall, this program demonstrates how to read user input using cin
and perform arithmetic operations using the increment operator. It prompts the user to enter their name and age, displays the entered values, and shows the age incremented by 1.
COSC-1336 / ITSE-1302 Computer Science - Author: Dr. Kevin Roark