What is the value of the variable Number after each statement. Each answer is de
ID: 3847335 • Letter: W
Question
What is the value of the variable Number after each statement. Each answer is dependent on the previous answer.
int Number=0; ___________________
Number = 100; __________________
Number /= 3 + 2; ________________
Number--; ________________
9. Based on the last answer above, what will this print: cout << (Number++ + 2);
Explanation / Answer
Okay, so let's start:
int Number=0 --> An integer variable Number as been assigned the integer 0. So, Number holds 0.
Number = 100 --> This is a simple assignment. We just re-assigned the variable Number to the value 100. Now, Number holds 100.
Number /= 3 + 2 --> This is a shorthand operation. What does it do? Let's take an example: a = a + 5, can be written as a += 5. This just simplifies our coding. So, this step can be expanded as: Number = Number/(3+2) --> Number=Number/5, now we know Number is 100. Hence:
Number = 100/5 i.e. 20. Number holds 20.
Number-- --> This is a postfix decrement, as it is not a part of any expression so, we will just decrement the value of Number i.e. 20-- = 19. Number holds 19.
cout << (Number++ + 2) --> Here Number is a part of the expression so postfix and prefix matters, as this is postfix increment so value of Number will be incremented after performing the expression i.e. Number + 12 = 19 + 2 = 21. Now, 21 gets printed, while Number will hold 20 i.e. ++ after the expression evalutaion.
I hope, you clearly understand this. For any further doubts, let me know in the comments !
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.