GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing codescracker.com — C++ Assignment Operator and Statement
C++ Assignment Operator and StatementThis post was written and published to describe one of the most important operators in C++, the assignment operator (=). Another subject covered in this post is assignment statement. So, without further ado, let's get started with the assignment operator. C++ Assignment OperatorBasically, the assignment operator is used to assign the value of one variable to another. Or assign a value to a variable. Here is the general form to assign a variable's value or value to a variable: a = b; x = 10; In the above statements, the value of b is assigned to a, and the value 10 is assigned to x. Here is an example. #include<iostream>
using namespace std;
int main()
{
int a, b=10, x;
a = b;
x = 10;
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
cout<<"x = "<<x<<endl;
return 0;
}
Here is the sample output of the above C++ program: a = 10 b = 10 x = 10 Note: In C++, the "=" character is known as the assignment operator. C++ Assignment StatementAs you know, an expression is composed of one or more operations. An expression terminated by a semicolon becomes a statement. Statements from the smallest executable unit within a C++ program Statements are terminated with a semicolon. An assignment statement assigns a value to a variable. The value assigned may be constant, variable, or an expression. The general form of an assignment statement is as follows: a = cve; where a is a variable to whom the value is being assigned, and cve can either be a constant, a variable, or an expression. Following are some examples of assignment statements: x = 16; y = 3.6; z = x + y; z = z × x; a = b + c - d; I already mentioned that the symbol "=" is known as the assignment operator. The assignment operator can be used serially, which is a common feature of C++. The assignment operator = returns the value of the assignment as well as actually assigning the value to the left-hand operand. Because of that, assignments can be chained together. This can be useful when assigning the same value to a number of items. For example, x = y = z = 13 This statement assigns the value 13 to x, y, and z. All the variables in this multiple assignment statement must be declared before. Such a statement works from right to left. First, 13 is assigned to z, then the value of z, which is now 13, is assigned to y, and y's value of 13 is assigned to x. More ExamplesHere are some C++ programs that you might enjoy:
« Previous Tutorial Next Tutorial » Practice Quizzes
Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes. Popular Topics
Explore the most popular programming and computer science tutorials on CodesCracker. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |