+ 1

I need a little help with this code. Whoever can complete it and find an answer for a,b, and c will get all their codes upvoted

12th Jan 2017, 8:45 AM
Dan Hernandez
Dan Hernandez - avatar
7 Answers
+ 2
a =3 b = 15 c=0 but the value of c depends on compiler. some compilers return a random value from ram but others use 0 as a initialization.
12th Jan 2017, 8:53 AM
mojtaba noghabaee
mojtaba noghabaee - avatar
+ 1
#include <iostream> using namespace std; int main() { int a = 0; int b = 0; int c; while(a<3){ b = b+5; a++;} return 0; }
12th Jan 2017, 8:45 AM
Dan Hernandez
Dan Hernandez - avatar
+ 1
I see. I didn't know that. how did you get b=15? I know that when you run the code that is what comes up. But is there a way to break it down? and does the a++ have any effect on a's value?
12th Jan 2017, 8:57 AM
Dan Hernandez
Dan Hernandez - avatar
+ 1
b = b +5 means add 5 to b . a ++ means add one to a . a++ equals to a= a+1.
12th Jan 2017, 9:00 AM
mojtaba noghabaee
mojtaba noghabaee - avatar
+ 1
when execution enters while loop it checks the condition, (here a<3) 1st iteration - a=0, condition satisfied so b = b+5 and a++ (a = a+1) are executed now b=5 and a=1 now execution goes back to the condition 2nd iteration - a=1, condition satisfied so b = b+5 and a++ (a = a+1) are executed now b=10 and a=2 now execution goes back to the condition 3rd iteration - a=2, condition satisfied so b = b+5 and a++ (a = a+1) are executed now b=15 and a=3 now execution goes back to the condition 4th iteration - a=3, condition not satisfied so its skips all the while loop statements to return 0 statement.
12th Jan 2017, 9:14 AM
Akash Middinti
+ 1
wow.. well, this kind of explanation should be part of the training here. you just broke it down barnie style. thanks a bunch! @mojtaba @akash. up votes coming right up!
12th Jan 2017, 9:29 AM
Dan Hernandez
Dan Hernandez - avatar
0
if I follow that, wouldn't a= 4 and b= 5? this is the part that gets me
12th Jan 2017, 9:04 AM
Dan Hernandez
Dan Hernandez - avatar