Can someone write a program using alpha=beta-- in c++? Alpha and beta being integers.
#include <iostream> int main(int argc, char** argv) { int alpha, beta = 10; while ((alpha = beta--)) // Extra pair of parentheses suppresses warnings { std::cout << "T minus " << alpha << " and counting" << std::endl; } std::cout << "Lift-Off!" << std::endl; return 0; } Alpha isn't really needed in this case, but still.
If you modify it a little so that you see the value of beta alongside the value of alpha, you can see that alpha is being assigned the initial values of beta that beta had before beta was decremented.
Yes you can write a program like this in c++. I assume the question is "what happens to alpha and beta?" Answer in this case is: (1) alpha is assigned the value of beta (2) beta is decremented by one Explanation: the -- operator written behind a variable evaluates the entire expression to the current value of the variable BEFORE the decrementing operation is executed. Example: int beta, alpha; beta = 3; alpha = beta--; // now alpha is 3 and beta is 2
Thank You!
Join our real-time social learning platform and learn together with your friends!