Q21 17 Marks Answer include kiostream Run 1 sum 2 using name
Solution
in the main function, first statement is declaration and initializaion of two vairables sum=1 and i=0.
then comes, the \"for\" loop. \"for\" loop is used for doing some action repeatedly until some condition is met.
here the for loop will execute until i <6.
\"for\" loop consists of three parts:
i) initilisation of counter variable (i=0)
ii) condition part (i<6)
iii) increment or decrement counter variable (i++)
here, the first part initilisation will be done only one time. that is, i=0.
After initilisation, for loop works in the following manner:
1. checking the condition part,if the condition is true goto 2 otherwise goto 5
2. body of the for loop will be executed
3. increment the counter variable (i++)
4. goto step 1
5. exit
according to the given program,during the first iteration the \"i\" value will be 0 during initilisation part. then condition is checked, i<6 that is 0< 6,condition is true. since condition is true, the control goes to the body of the for loop
in the body of the for loop, the first statement is if(i%2). % is the modulo operator, gives the remainder of the division operation. 0 % 2 is 0. therefore \" if (0) \"is false therefore if block, that is sum=sum+i; will not be executed
else block statement \"continue\" will be executed. continue statement is used to make the compiler to go to increment part, without executing the further statements in the body of the for loop. therefore, cout statement after the else block will not be executed.
after continue statement, the control goes to the increment part, now the value of i will be incremented by 1. now i will 1 (i=i+1), after incrementation, the condition is checked i<6 that 1<6. the condition is true.
since, the condition is true, we are entering the loop body. the first statement in the loop is if (i%2) that is
1%2. the value of the expression 1 % 2 is 1, therefore \" if(1) is true\", therefore the if block will be executed,
that is sum=sum+1, sum will be 1 and i will be 1, therefore sum will contain 2(1+1).Then else block will not executed. then cout statement will be prrint the value of i and sum variables. then goes to the incrementation part and the process continues until i<6. the loop will break at the point when 6<6
after the for loop, the counter variable i is incremented by one and printed.
