#include <stdio.h>
int main() {
int n = 3, count, value=0;
for(count=1,count<=n; ++count)
{
value+=count;
}
printf("value=%d", value);
Answers
but what's your question
Given Program :
#include <stdio.h>
int main()
{
int n = 3, count, value=0;
for(count=1;count<=n; ++count)
{
value+=count;
}
printf("value=%d", value);
}
Dry Run :
n = 3
1) When count = 1
Condition : count<=n ⇒ 1 <= 3 (True)
So, block will execute
value+=count; ⇒ value = value + count; ⇒ value = 0 + 1 = 1
So, value = 1
Update : ++count = ++1 = 2
So, count = 2
2) When count = 2
Condition : count<=n ⇒ 2 <= 3 (True)
So, block will execute
value+=count; ⇒ value = value + count; ⇒ value = 1 + 2 = 3
So, value = 3
Update : ++count = ++2 = 3
So, count = 3
3) When count = 3
Condition : count<=n ⇒ 3 <= 3 (True)
So, block will execute
value+=count; ⇒ value = value + count; ⇒ value = 3 + 3 = 6
So, value = 6
Update : ++count = ++3 = 4
So, count = 4
4) When count = 4
Condition : count<=n ⇒ 4 <= 3 (False)
So, block will not execute
So, finally value = 6