IC010 – C++ Assignment Operations

Flying Fish BLOG 2023.3.31

Information Technology Teaching

Python-Based Teaching

Research Topics

Academic Level Examination

Python Program Design

C++ Programming

……

IC010 - C++ Assignment OperationsIC010 - C++ Assignment Operations

In a C++ program, to makedata participate in calculations, it is necessary to use assignment expressions to assign a certain amount of data to variables. An assignment statement consists of an assignment expression followed by a semicolon, for example, int a=126, which means assigning the integer 126 to the variable a.

Most statements in C++ programs are assignment statements, which can be divided into simple assignment operations and compound assignment operations.

Simple Assignment Operations

In C++ programs, simple assignment operations are implemented using the assignment operator “=”. The “=” is called the assignment operator, not the equality sign in mathematics. For example, c=a+b is read as “assign the value of a+b to c”.

Assignment Expressions

An assignment expression connects a variable and an expression using the assignment operator “=”. This type of expression is called an assignment expression. The assignment operator “=” can be used consecutively. For example, a=b=c=9 means that the value of a is 9; a=(b=3)+(c=5) means that the value of a is 8.

Assignment Statements

By adding a statement terminator “;” after the assignment expression, it becomes an assignment statement.

Format: variable= value(expression);

Function: Assign the value (or the value of the expression) on the right side of the assignment operator to the variable on the left side.

For example: c=m+n; means assigning the sum of m and n to the variable c.

Practice Program:

#include <iostream>
using namespace std;
int main(){
    long s,c;
    float p;
    s=52;
    c=696;
    p=s*1.0/c*100;
    cout<<p<<'%';
    return 0;
}

Running Result:

IC010 - C++ Assignment Operations

Extension and Improvement

1. Read the following program segment, think about the program execution process, and fill in the final running result on the line below.

#include <iostream>
using namespace std;
int main(){
    int x,y,m=0,n=0;
    x=y=30;
    m=(x=5)*3+(x++);
    n=(y++)+(y=5)*3;
    cout<<"m="<<m<<'';
    cout<<"n="<<n<<endl;
    return 0;
}

Running Result: _________________

Compound Assignment Operations

Compound assignment operations combine operators and assignment symbols into complex assignment operators. For example, a=a+4 can be simplified to a+=4, where “+=” is the compound assignment operator.

Format of Compound Assignment Operations

In C++, compound assignment operators consist of an operator and an assignment operator, and the application format is as follows:

Format: variable compound assignment operator expression

Function: Assign the result of the operation between the variable and the expression to the variable.

Common Compound Assignment Operators

Common operators include: + – * / %, correspondingly, the compound assignment operators are += -= *= /= and %=.

Practice Program:

#include <iostream>
using namespace std;
int main(){
    int age=12;
    age+=2;
    age+=4;
    age+=5;
    cout<<age;
    return 0;
}

Running Result:

IC010 - C++ Assignment Operations

Order of Operations for Compound Assignment

The right side of a compound assignment operator can be a variable, constant, or expression. When it is an expression, the order of operations must be considered.

For example: s=300,t=5, executing the statement s/=t+25, first calculates t+25 to be 30, then calculates s=s/30, resulting in s=10.

Extension and Improvement

1. Read the following program segment, think about the program execution process, and fill in the final running result on the line below.

#include <iostream>
using namespace std;
int main(){
    int i=1,s=0;
    s+=i;
    i*=2;
    s+=i;
    i*=2;
    cout<<i<<""<<s<<endl;
    return 0;
}

Running Result: ____________

2. Programming Problem

The little monkey’s mother picked 30 bananas, and the little monkey, unable to resist temptation, immediately took the bananas and ate half of them that day, feeling unsatisfied, it ate one more. The next morning, it ate half of the remaining bananas and then one more, and this continued for the third day. How many bananas does the little monkey find remaining on the fourth morning? Try to calculate the result using C++ programming.

IC010 - C++ Assignment Operations1.IC005 – C++ Operators2.IC006 – C++ Expressions3.IC007 – C++ Data Types (Integer)4.IC008 – C++ Data Types (Floating Point)5.IC009 – C++ Data Types (Character)/Type CastingIC010 - C++ Assignment Operations

Improve by1% every day, and after a year you can achieve37 times growth; if you regress by1% every day, after a year you will weaken, even regress to zero; a small change or a good habit can produce an amazing compounding effect!

Leave a Comment