Operators for assignment – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Code Learning #CPP#1734 siteicon   siteicon   siteicon  

Operators for assignment

Here we demonstrate the simple assignment and its variations.

Learning Objectives

  • Learning simple assignment and assignment within declaration.
  • Learning composite assignments with use of +, – , * and / operator along with assignment operator.

Source Code

TC++ #1734

 

Source Code

Run Output

20
10
100
10

Code Understanding

int a; a=10;
This is simple variable declaration followed by simple assignment using a single = operator. This can also be done in one instruction as int a=10;

a+=10; cout<<a<<endl;
This a composite assignment along with addition. The instruction a+=10 is equivalent to a=a+10; This is an efficient way of incrementing the variable by given number. Later we print it to produce 20 as the initial value of a was 10.

a-=10; cout<<a<<endl;
This a composite assignment along with subtraction. The instruction a-=10 is equivalent to a=a-10; This is an efficient way of decrementing the variable by given number. Later we print it to produce 10 as a was made 20 in previous instruction.

a*=10; cout<<a<<endl;
This a composite assignment along with multiplication. The instruction a*=10 is equivalent to a=a*10; This is an efficient way of multiplying the variable by given number. Later we print it to produce 100 as a was made 10 in previous instruction.

a/=10; cout<<a<<endl;
This a composite assignment along with division. The instruction a/=10 is equivalent to a=a/10; This is an efficient way of dividing the variable by given number. Later we print it to produce 10 as a was made 100 in previous instruction.

Notes

While using assignment and composite assignments, care must be taken that the value assigned fits the data size of target data type like int, float, long, char etc.


Suggested Filename(s): operators-assign.cpp, assignments.cpp



Share

CSKC| Created: 15-Dec-2017 | Updated: 28-Aug-2018|






×
Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues


Back