a += b
a -= b
a *= b
a /= b
a %= b
a &= b
a |= b
a ^= b
a <<= b
a >>= b
++a
--a
a++
a--
+a
-a
a + b
a - b
a * b
a / b
a % b
~a
a & b
a | b
a ^ b
a << b
a >> b
!a
a && b
a || b
a == b
a != b
a < b
a > b
a <= b
a >= b
a[b]
*a
&a
a->b
a.b
a(...)
a, b
(type) a
a ? b : c
sizeof
_Alignof
(since C11)
for Assignment operators |
Time estimate: 45 min.
Compound assignment operators are shortcuts that do a math operation and assignment in one step. For example, x += 1 adds 1 to the current value of x and assigns the result back to x . It is the same as x = x + 1 . This pattern is possible with any operator put in front of the = sign, as seen below. If you need a mnemonic to remember whether the compound operators are written like += or =+ , just remember that the operation ( + ) is done first to produce the new value which is then assigned ( = ) back to the variable. So it’s operator then equal sign: += .
Since changing the value of a variable by one is especially common, there are two extra concise operators ++ and -- , also called the plus-plus or increment operator and minus-minus or decrement operator that set a variable to one greater or less than its current value.
Thus x++ is even more concise way to write x = x + 1 than the compound operator x += 1 . You’ll see this shortcut used a lot in loops when we get to them in Unit 4. Similarly, y-- is a more concise way to write y = y - 1 . These shortcuts only exist for + and - as they don’t really make sense for other operators.
If you’ve heard of the programming language C++, the name is an inside joke that C, an earlier language which C++ is based on, had been incremented or improved to create C++.
Here’s a table of all the compound arithmetic operators and the extra concise incremend and decrement operators and how they relate to fully written out assignment expressions. You can run the code below the table to see these shortcut operators in action!
Operator |
|
|
|
|
|
---|---|---|---|---|---|
Written out | = x + 1 | = x - 1 | = x * 2 | = x / 2 | = x % 2 |
Compound | += 1 | -= 1 | *= 2 | /= 2 | %= 2 |
Extra concise |
|
| |||
Run the code below to see what the ++ and shorcut operators do. Click on the Show Code Lens button to trace through the code and the variable values change in the visualizer. Try creating more compound assignment statements with shortcut operators and work with a partner to guess what they would print out before running the code.
If you look at real-world Java code, you may occassionally see the ++ and -- operators used before the name of the variable, like ++x rather than x++ . That is legal but not something that you will see on the AP exam.
Putting the operator before or after the variable only changes the value of the expression itself. If x is 10 and we write, System.out.println(x++) it will print 10 but aftewards x will be 11. On the other hand if we write, System.out.println(++x) , it will print 11 and afterwards the value will be 11.
In other words, with the operator after the variable name, (called the postfix operator) the value of the variable is changed after evaluating the variable to get its value. And with the operator before the variable (the prefix operator) the value of the variable in incremented before the variable is evaluated to get the value of the expression.
But the value of x after the expression is evaluated is the same in either case: one greater than what it was before. The -- operator works similarly.
The AP exam will never use the prefix form of these operators nor will it use the postfix operators in a context where the value of the expression matters.
1-5-2: What are the values of x, y, and z after the following code executes?
1-5-3: What are the values of x, y, and z after the following code executes?
Use paper and pencil or the question response area below to trace through the following program to determine the values of the variables at the end.
Code Tracing is a technique used to simulate a dry run through the code or pseudocode line by line by hand as if you are the computer executing the code. Tracing can be used for debugging or proving that your program runs correctly or for figuring out what the code actually does.
Trace tables can be used to track the values of variables as they change throughout a program. To trace through code, write down a variable in each column or row in a table and keep track of its value throughout the program. Some trace tables also keep track of the output and the line number you are currently tracing.
Trace through the following code:
1-5-4: Write your trace table for x, y, and z here showing their results after each line of code.
After doing this challenge, play the Operators Maze game . See if you and your partner can get the highest score!
Compound assignment operators ( += , -= , *= , /= , %= ) can be used in place of the assignment operator.
The increment operator ( ++ ) and decrement operator ( -- ) are used to add 1 or subtract 1 from the stored value of a variable. The new value is assigned to the variable.
The use of increment and decrement operators in prefix form (e.g., ++x ) and inside other expressions (i.e., arr[x++] ) is outside the scope of this course and the AP Exam.
Have an account?
University , java arrays, java programming, java operators, c programming, professional development , pointers in c, python basics, 12.3k plays, iterative statements in python.
52 questions
No student devices needed. Know more
Choose a right C Statement.
Loops or Repetition block executes a group of statements repeatedly.
Loop is usually executed as long as a condition is met.
Loops usually take advantage of Loop Counter
All the above.
What is the output of C Program.?
while(a >= 3);
printf("RABBIT\n");
printf("GREEN");
RABBIT GREEN
RABBIT is printed infinite times
None of the above
while(a <= 27)
printf("%d ", a);
Compiler error
while(a <= 30);
if(a > 35)
32 33 34 35
Choose a correct C Statement.
a++ is (a=a+1) POST INCREMENT Operator
a-- is (a=a-1) POST DECREMENT Operator
--a is (a=a-1) PRE DECREMENT Operator
++a is (a=a+1) PRE INCREMENT Operator
Choose correct Syntax for C Arithmetic Compound Assignment Operators.
a+=b is (a= a+ b)
a-=b is (a= a-b)
a*=b is (a=a*b)
a/=b is (a = a/b)
a%=b is (a=a%b)
for(k=1, j=10; k <= 5; k++)
printf("%d ", (k+j));
compiler error
10 10 10 10 10
10 11 12 13 14 15
for(k=1; k <= 5; k++);
printf("%d ", k);
printf("TESTING\n");
for(printf("FLOWER "); printf("YELLOW "); printf("FRUITS "))
} return 0;
FLOWER FRUITS
FLOWER YELLOW
FLOWER YELLOW FRUITS
Loops in C Language are implemented using.?
While Block
Do While Block
All the above
What is the way to suddenly come out of or Quit any Loop in C Language.?
continue; statement
break; statement
leave; statement
quit; statement
Which loop is faster in C Language, for, while or Do While.?
all of the above
Choose correct C while loop syntax.
while(condition) { //statements }
{ //statements }while(condition)
while(condition); { //statements }
while() { if(condition) { //statements } }
Choose a correct C for loop syntax.
for(initalization; condition; incrementoperation) { //statements }
for(declaration; condition; incrementoperation) { //statements }
for(declaration; incrementoperation; condition) { //statements }
for(initalization; condition; incrementoperation;) { //statements }
Choose a correct C do while syntax.
dowhile(condition) { //statements }
do while(condition) { //statements }
do { //statements }while(condition)
do { //statements }while(condition);
while(true)
printf("RABBIT");
RABBIT is printed unlimited number of times.
Compiler error.
while(a==5)
RABBIT is printed unlimited number of times
None of the above.
while(a=123)
int a = 10, b = 25;
a = b++ + a++;
b = ++b + ++a;
printf("%d %d n", a, b);
#include<stdio.h>
float a=0.7;
if(a<0.7)
printf("C");
printf("C++");
Compilation Error
None of the these
int main ()
int x = 24, y = 39, z = 45;
printf ("n%d %d %d", x, y, z);
int main() {
int m = -10, n = 20;
n = (m < 0) ? 0 : 1;
printf("%d %d", m, n);
int x = 0, y = 0;
if(x > 0)
if(y > 0)
printf("True");
printf("False");
Error because of dangling else problem
int a = 1, b=2, c=3;
char d = 0;
if(a,b,c,d)
printf("EXAM");
No Output and No Error
Run time error
Compile time error
int a=2,b=7,c=10;
printf("%d",c);
Compilation error
What will be output of the following c program?
int max-val=100;
int min-val=10;
int avg-val;
avg-val = max-val + min-val / 2;
printf("%d",avg-val);
An external variable is one
Which resides in the memory till the end of the program
Which is globally accessible by all functions
Which is declared outside the body of any function
All of the above
When applied to a variable, what does the unary "&" operator yield?
The variable's address
The variable's right value
The variable's binary form
The variable's value
Which are built-in data structures in C programming?
The size of a character variable in C is
An expression contains relational, assignment and arithmetic operators. If parenthesis are not specified, the order of evaluation of the operators would be:
assignment, arithmetic, relational
relational, assignment, arithmetic
assignment, relational, arithmetic
arithmetic, relational, assignment
Given two literals 0x001B and 033. What are these equal to?
What is the outpur of the following code segment (assuming sizeof(int) returns 4)?
int i=0x1 << sizeof(int) * 8-1;
printf("\n%x", i);
i = i >> sizeof(int) * 8-1;
printf("%d", i);
0x8000000-1
-1 0x0000000
Which is the correct sequence statements that swaps values of two statements?
a=a+b; a=a-b; b=a-b;
a=a+b, b=a-b; a=a-b;
a=a-b; a=a+b; b=b-a;
None of these
Choose the option that contains only Unary operators of C:
sizeof, (type conversion)
short hand operator, &
increment(++), >=, !
What is the value of this expression using integer arithmetic:
What is the value of a in this code fragment
int a=3, b;
Determine the value of this expression using integer arithmetic:
Determine the value of b in this code fragment:
Determine the value of the expression using integer arithmetic:
6 * 9 % 3 / 2
Determine the value of a in the second line of the code fragment:
int a=1, b=2;
Determine the value of b in the second line of the code fragment:
Which line of code is correct for the statement:
Add 1 variable to x.
Which line of code is incorrect for the statement:
Subtract 4 from the variable y.
Read in a value for the integer variable num .
printf("%d", &num);
scanf("%d", num);
scanf("%d", &num);
What is the EXACT output from this code fragment:
float num=3.238;
printf("num=%.2f", num);
int num=10;
if(num> 7 % 3 / 2 * 3)
printf("This is True");
printf("This is False");
This is True
This is False
What symbol is used in conditional operators for AND:
What will be printed on the screen from this fragment:
printf("%d", ++num)
What is the abbreviated way to write this assignment statement:
total=total+num;
total=(total+num)
Explore all questions with a free account
Continue with email
Continue with phone
a+=bis (a= a+ b) a-=b is (a= a-b)
a*=b is (a=a*b) a/=b is (a = a/b)
a%=b is (a=a%b)
All of the above
Posted under Control Flow Statements in C C Programming
Confused About the Answer? Ask for Details Here.
Know the Explanation? Add it Here.
Similar questions, discover related mcqs.
Q. Choose a correct C do while syntax.
View solution
Q. Choose a correct C for loop syntax.
Q. Choose correct C while loop syntax.
Q. Which loop is faster in C Language, for, while or Do While?
Q. Loops in C Language are implemented using?
Q. Choose a right C Statement.
Q. Choose a correct C Statement.
Q. Choose a correct C Operator Priority? Items in one group ( ) has same priority.
Q. What is the Priority of C Logical Operators? NOT (!), AND (&&) and OR (||)
Q. In _______, the bodies of the two loops are merged together to form a single loop provided that they do not make any references to each other.
Q. In the context of "break" and "continue" statements in C, pick the best statement.
Q. Which of the following statement about for loop is true ?
Q. For loop in a C program, if the condition is missing?
Q. c = (n) ? a : b; can be rewritten as exp1 ? exp2 : exp3;
Q. do-while loop terminates when conditional expression returns?
Q. A labeled statement consist of an identifier followed by
Q. Which loop is guaranteed to execute at least one time.
Q. goto can be used to jump from main to within a function?
Q. The continue statment cannot be used with
Q. In the following loop construct, which one is executed only once always. for(exp1; exp2; exp3)
Are you eager to expand your knowledge beyond C Programming? We've curated a selection of related categories that you might find intriguing.
Click on the categories below to discover a wealth of MCQs and enrich your understanding of Computer Science. Happy exploring!
Navigate the basics of information technology with our IT Fundamentals MCQs. Get a...
Hone your computational mathematics skills with our MATLAB MCQs. Understand...
Discover the fascinating world of extracting insights from data with our Data Science...
Learn the leading NoSQL database with our MongoDB MCQs. Understand everything from...
Become proficient with the Unix operating system using our UNIX MCQs. Learn about...
Dive into the world of big data with our Hadoop MCQs. Cover key concepts including...
Enhance your knowledge of Microsoft's event-driven programming language with our...
Discover how businesses leverage technology with our Management Information Systems...
Get to grips with modern communication technologies with our Wireless and Mobile...
Compound-assignment operators provide a shorter syntax for assigning the result of an arithmetic or bitwise operator. They perform the operation on the two operands before assigning the result to the first operand. The following are all possible assignment operator in java:
Implementation of all compound assignment operator
Rules for resolving the Compound assignment operators
At run time, the expression is evaluated in one of two ways.Depending upon the programming conditions:
Examples : Resolving the statements with Compound assignment operators
We all know that whenever we are assigning a bigger value to a smaller data type variable then we have to perform explicit type casting to get the result without any compile-time error. If we did not perform explicit type-casting then we will get compile time error. But in the case of compound assignment operators internally type-casting will be performed automatically, even we are assigning a bigger value to a smaller data-type variable but there may be a chance of loss of data information. The programmer will not responsible to perform explicit type-casting. Let’s see the below example to find the difference between normal assignment operator and compound assignment operator. A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.
For example, the following code is correct:
and results in x having the value 7 because it is equivalent to:
Because here 6.6 which is double is automatically converted to short type without explicit type-casting.
Refer: When is the Type-conversion required?
Explanation: In the above example, we are using normal assignment operator. Here we are assigning an int (b+1=20) value to byte variable (i.e. b) that’s results in compile time error. Here we have to do type-casting to get the result.
Explanation: In the above example, we are using compound assignment operator. Here we are assigning an int (b+1=20) value to byte variable (i.e. b) apart from that we get the result as 20 because In compound assignment operator type-casting is automatically done by compile. Here we don’t have to do type-casting to get the result.
Reference: http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.26.2
Similar reads.
COMMENTS
Your solution's ready to go! Enhanced with AI, our expert help has broken down your problem into an easy-to-learn solution you can count on. See Answer See Answer See Answer done loading
The result of a compound-assignment operation has the value and type of the left operand. #define MASK 0xff00 n &= MASK; In this example, a bitwise-inclusive-AND operation is performed on n and MASK, and the result is assigned to n. The manifest constant MASK is defined with a #define preprocessor directive. See also. C Assignment Operators
Learn about all the different types of operators available in C like Arithmetic, Assignment, Relational and Logical operators. Practice Problems to solidify your knowledge. ... In the division example (a / b), you might notice that the result is 3, not 3.33. This is because when you divide two integers in C, the result is also an integer ...
1. "=": This is the simplest assignment operator. This operator is used to assign the value on the right to the variable on the left. Example: a = 10; b = 20; ch = 'y'; 2. "+=": This operator is combination of '+' and '=' operators.This operator first adds the current value of the variable on left to the value on the right and then assigns the result to the variable on the left.
The Java language specification says that: The compound assignment E1 op= E2 is equivalent to [i.e. is syntactic sugar for] E1 = (T) ((E1) op (E2)) where T is the type of E1, except that E1 is evaluated only once. We note two important points: The expression is cast to the type of E1 before the assignment is made (the cast is in red above) E1 ...
Code language:C++(cpp) The = assignment operator is called a simple assignment operator. It assigns the value of the left operand to the right operand. Besides the simple assignment operator, C supports compound assignment operators. A compound assignment operator performs the operation specified by the additional operator and then assigns the ...
A special case scenario for all the compound assigned operators. int i= 2 ; i+= 2 * 2 ; //equals to, i = i+(2*2); In all the compound assignment operators, the expression on the right side of = is always calculated first and then the compound assignment operator will start its functioning. Hence in the last code, statement i+=2*2; is equal to i ...
Compound-Assignment Operators in Java. Java supports 11 compound-assignment operators: += assigns the result of the addition. -= assigns the result of the subtraction. *= assigns the result of the multiplication. /= assigns the result of the division. %= assigns the remainder of the division. &= assigns the result of the logical AND.
The five arithmetic assignment operators are a form of short hand. Various textbooks call them "compound assignment operators" or "combined assignment operators". Their usage can be explaned in terms of the assignment operator and the arithmetic operators. In the table we will use the variable age and you can assume that it is of integer data type.
The code is a C program that demonstrates the use of compound assignment operators in C. Here is an explanation of each line of the code: int a=10,b=5; declares two variables a and b of type int and assigns them the values 10 and 5 respectively. a+=b; is the compound assignment operator +=, it performs a+b and assigns the value to a. It is equivalent to a=a+b.
The assignment operator ( = ) is used to assign a value to the variable. Its general format is as follows: variable = right_side. The operand on the left side of the assignment operator must be a variable and operand on the right-hand side must be a constant, variable or expression. Here are some examples:
Arithmetical assignment operators are shorthand notations that perform arithmetic operations and assignments in a single step. These operators include: For instance, using +=, you can add a value to a variable without needing a separate assignment statement. Similarly, the other operators work by performing the respective operation and updating ...
Code language: C++ (cpp) C Arithmetic operator's precedence and associativity. Let's discuss the arithmetic operators' precedence and associativity. Arithmetic operator precedence. When you use multiple arithmetic operators in an expression, C evaluates the expression based on the order of precedence of the operators. It works like Math ...
What your ancient compiler is doing is correctly following the C grammar rules and is grouping the expression as. a += (a += (a += 2)) But grouping is not the same as sequencing. From this point the behaviour is undefined. Your compiler appears to evaluate the above to. a += (a += 7) followed by. a += 14.
In this video we are going to study about.. Assignment Operator in C Assignment Operator Example Use of Assignment Operator in CCompound Assignment Operato...
Study with Quizlet and memorize flashcards containing terms like What do compound assignment operators do?, What are the compound assignment operators for the following: Addition Subtraction Multiplication Division Modulo, SECTION REVIEW1A: #1) You are also in charge of keeping track of how many cookies there are at the bake sale. This value is represented by the number of numCookies.
Assignment performs implicit conversion from the value of rhs to the type of lhs and then replaces the value in the object designated by lhs with the converted value of rhs . Assignment also returns the same value as what was stored in lhs (so that expressions such as a = b = c are possible). The value category of the assignment operator is non ...
1.5. Compound Assignment Operators¶. Compound assignment operators are shortcuts that do a math operation and assignment in one step. For example, x += 1 adds 1 to the current value of x and assigns the result back to x.It is the same as x = x + 1.This pattern is possible with any operator put in front of the = sign, as seen below. If you need a mnemonic to remember whether the compound ...
Choose correct Syntax for C Arithmetic Compound Assignment Operators. a+=b is (a= a+ b) a-=b is (a= a-b) ... assignment and arithmetic operators. If parenthesis are not specified, the order of evaluation of the operators would be: ... What is the abbreviated way to write this assignment statement: total=total+num; t=t+n; total+=num. total ...
Dive into the world of Artificial Intelligence with our Neural Networks MCQs. Uncover... Choose correct Syntax for C Arithmetic Compound Assignment Operators. a+=bis (a= a+ b) a-=b is (a= a-b) a*=b is (a=a*b) a/=b is (a = a/b) a%=b is (a=a%b) All of the above. C Programming Objective type Questions and Answers.
The "*= 2" is an example of a compound assignment operator, which multiplies the current value of integerOne by 2 and sets that as the new value of integerOne. Other arithmetic operators also have compound assignment operators as well, with addition, subtraction, division, and modulo having +=, -=, /=, and %=, respectively. Incrementing and ...
Compound-assignment operators provide a shorter syntax for assigning the result of an arithmetic or bitwise operator. They perform the operation on the two operands before assigning the result to the first operand. ... For example, the following code is correct: short x = 4; x += 6.6;
Compound Assignment Operator: These operators combine the simple-assignment operator with another binary operator. Compound assignment operators execute the action provided by the extra operator before assigning the result to the left operand. for eg. exp1 += exp2. All the options are following the above format so D is correct