Data Types, Variables and Constants

Assignment Statement

An Assignment statement is a statement that is used to set a value to the variable name in a program.

Assignment statement allows a variable to hold different types of values during its program lifespan. Another way of understanding an assignment statement is, it stores a value in the memory location which is denoted by a variable name.

Assignment Statement Method

Syntax

The symbol used in an assignment statement is called as an operator. The symbol is ‘=’.

Note: The Assignment Operator should never be used for Equality purpose which is double equal sign ‘==’.

The Basic Syntax of Assignment Statement in a programming language is :

variable = expression ;

where,

variable = variable name

expression = it could be either a direct value or a math expression/formula or a function call

Few programming languages such as Java, C, C++ require data type to be specified for the variable, so that it is easy to allocate memory space and store those values during program execution.

data_type variable_name = value ;

                    

Example –

int a = 50 ;

float b ;

a = 25 ;

b = 34.25f ;

 

In the above-given examples, Variable ‘a’ is assigned a value in the same statement as per its defined data type. A data type is only declared for Variable ‘b’. In the 3rd line of code, Variable ‘a’ is reassigned the value 25. The 4th line of code assigns the value for Variable ‘b’.

Assignment Statement Forms

  1. Basic Form

This is one of the most common forms of Assignment Statements. Here the Variable name is defined, initialized, and assigned a value in the same statement. This form is generally used when we want to use the Variable quite a few times and we do not want to change its value very frequently.

                    

(Code in C)

int RollNo = 25 ;

printf("%d",RollNo);

Output –

25

 

  1. Tuple Assignment

Generally, we use this form when we want to define and assign values for more than 1 variable at the same time. This saves time and is an easy method. Note that here every individual variable has a different value assigned to it.

(Code In Python)

                    

a, b = 50, 100 ;

print(a) ;

print(b) ;

Output –

50

100

 

  1. Sequence Assignment

(Code in Python)

                    

x,y,z = 'HEY' ;

print('x = ', x) ;

print('y = ', y) ;

print('z = ', z) ;

Output –

x =  H

y =  E

z =  Y

 

  1. Multiple-target Assignment or Chain Assignment

In this format, a single value is assigned to two or more variables.

                    

a = b = 40 ;

print(a, b) ;

Output –

40 40

 

  1. Augmented Assignment

In this format, we use the combination of mathematical expressions and values for the Variable. Other augmented Assignment forms are: &=, -=, **=, etc.

                    

speed = 40 ;

speed += 10 ;  // equivalent to speed = speed + 10

print (“Speed = ”, speed) ;

Output –

Speed = 50

 

Browse more Topics Under Data Types, Variables and Constants

Few Rules for Assignment Statement

Few Rules to be followed while writing the Assignment Statements are:

  1. Variable names must begin with a letter, underscore, non-number character. Each language has its own conventions.
  2. The Data type defined and the variable value must match.
  3. A variable name once defined can only be used once in the program. You cannot define it again to store other types of value.
  4. If you assign a new value to an existing variable, it will overwrite the previous value and assign the new value.

FAQs on Assignment Statement

Q1. Which of the following shows the syntax of an assignment statement?

  1. variablename = expression ;
  2. expression = variable ;
  3. datatype = variablename ;
  4. expression = datatype variable ;

Answer – Option A.

Q2. What is an expression?

  1. Same as statement
  2. List of statements that make up a program
  3. Combination of literals, operators, variables, math formulas used to calculate a value
  4. Numbers expressed in digits

Answer – Option C.

Q3. What are the two steps that take place when an assignment statement is executed?

  1. Evaluate the expression, store the value in the variable
  2. Reserve memory, fill it with value
  3. Evaluate variable, store the result
  4. Store the value in the variable, evaluate the expression.

Answer – Option A.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.