What are the values of a and b after the code executes?

  • Thread starter Thread starter courtrigrad
  • Start date Start date
  • Tags Tags
    Method
Click For Summary

Homework Help Overview

The discussion revolves around understanding the behavior of a for loop in a code snippet that initializes two integer variables, a and b, and modifies them through iterations based on specific conditions. Participants are trying to determine the final values of a and b after the loop executes.

Discussion Character

  • Exploratory, Assumption checking, Problem interpretation

Approaches and Questions Raised

  • Participants are analyzing the iterations of the loop and how the values of a and b change with each execution. There are various interpretations of the loop's behavior, particularly regarding the order of operations and the effect of the increment on a.

Discussion Status

The discussion is active, with multiple participants sharing their calculated results and questioning each other's reasoning. Some participants express confusion about the loop's mechanics, while others provide clarifications about the execution order and variable assignments.

Contextual Notes

Participants are working under the constraints of a programming assignment, which may limit their ability to run the code in different environments or languages. There is also an emphasis on understanding the logic behind the loop rather than simply providing the final values.

courtrigrad
Messages
1,236
Reaction score
2
Hello all

Given the code:

Code:
int a = 0;
int b = 0;

for(a = 0; a < 10 && b < 20; a = a+2) {
        a = a + b;
        b = b + a + 1;
}

Find a and b. SO this is what I did:

0 = 0 + 2 (value of a)
2 = 2 + 0 + 1 (value of b)

Now a = 2 and b = 3, and we work from those values until we get to the constraints. However I am not sure whether this is correct?

Any help and tips is appreciated!

Thanks
 
Physics news on Phys.org
I see a mistake... upon your first execution of the for loop,, you forgot to execute
b = b + a +1
=> b = 0 + 0 + 1
so b = 1 after the first iteration
 
why would it be 0 + 0 + 1? wouldn't the values of a and b change?

Thanks
 
Last edited:
post your final result... I'll check it with mine
 
a = 12
b = 16
 
I got the same thing
 
That is not what I got.

Code:
.                      a  b 
. initial:             0  0
. after 1st iteration: 0  1
. after 2nd iteration: 5  5
(don't forget that the loop incerments a by 2)

(I ran the code in JavaScript, and lo! I got the same answers as by hand. pat pat pat)
 
would it be a = 10 b = 16?

0 = 0 + 0 = 0
0 = 0 + 0 + 1 = 1

2 = 2 + 1 = 3
1+ 3 + 1 = 5;

5 = 5 + 5 = 10
5 = 5 + 10 + 1 = 16?
 
DaveC426913 said:
That is not what I got.

Code:
.                      a  b 
. initial:             0  0
. after 1st iteration: 0  1
. after 2nd iteration: 5  5
(don't forget that the loop incerments a by 2)

(I ran the code in JavaScript, and lo! I got the same answers as by hand. pat pat pat)
Keep going.. it doesn't end after the second iteration...
post your code.. cause something is wrong
 
  • #10
courtrigrad said:
would it be a = 10 b = 16?

0 = 0 + 0 = 0
0 = 0 + 0 + 1 = 1

2 = 2 + 1 = 3
1+ 3 + 1 = 5;

5 = 5 + 5 = 10
5 = 5 + 10 + 1 = 16?
No... that would be the values after the third iteration... but it then increments a by 2 before checking the conditional statements
you had the correct answer
 
  • #11
Why am I getting a = 10, b = 16 instead of a = 12 b = 16?

Thanks
 
  • #12
courtrigrad said:
why would it be 0 + 0 + 1? wouldn't the values of a and b change?

Thanks
yes... and they do... everytime you come across an assignment (=) statement the variable to the left changes.

when you first come to the for loop both a and b are 0.
the for loop first initializes a to be 0 (it only does this once)... then checks the conditional statements (a<10 && b<20 ).
As long as these statements return a value of true. it executes the body of the for loop.
Since 0<10 and 0<20 it goes inside the body of the for loop and executes the first line of code.
The first line of the for loop is basically assigning to a the value of 0 + 0... so a remains zero.
The second line then assigns to b the value of b + the value of a + 1...
a and b are both still 0 so this looks like
b = 0 + 0 + 1.
Once the body of the for loop has been executed, it goes to the incremental stage and increments a by 2.
then goes back to check the conditional statements skipping the initialization. This process is repeated until one or both of the conditions returns a value of false, in which the program exits the for loop.
Does this make sense?
 
  • #13
courtrigrad said:
Why am I getting a = 10, b = 16 instead of a = 12 b = 16?

Thanks
The last statement you execute should be a = a +2;
do you see why?
 
  • #14
well if you go back up, 10 < 10 is false. so it should not increment a by 2. H.. I am not sure why it increments by 2.
 
  • #15
i think i got it now. it increments immediately after its down iteration.

thanks
 

Similar threads

Replies
3
Views
1K
Replies
25
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 20 ·
Replies
20
Views
3K
Replies
16
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 16 ·
Replies
16
Views
2K