+ 2

Increment operator...

for(k = 1; k < n; ++k) {...} in this line of code, what will be the value of k before entering in a for loop?

4th Apr 2018, 6:29 PM
Nilesh Ingale
Nilesh Ingale - avatar
9 Antworten
+ 3
Assuming you declared your variables prior to the loop, the loop would start off with 'k' being equal to 1. As for if you had 'k' set to another value prior to the loop, I can't answer that without knowing the rest of the code. In this isolated context, 'k' starts as being equal to 1 and increments by one each loop through.
4th Apr 2018, 6:41 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 2
k is local to for loop only, no previous initialisation done
4th Apr 2018, 6:43 PM
Nilesh Ingale
Nilesh Ingale - avatar
+ 2
int fun(int n) { int x=1, k; if (n==1) return(1); for(k=1; k<n; ++k){ x = x+fun(k)*fun(n-k); return(x); } } Now tell me what will be the output of fun(5)? if possible, plz give detailed explanation.
4th Apr 2018, 6:54 PM
Nilesh Ingale
Nilesh Ingale - avatar
+ 1
Nilesh Ingale in this code snippet k isn't declared in the for loop as Jakob Marley already mentioned. The scope of k isn't limited to the for loop on this case. constructing the loop without for would look like this: int k = ??? int n = ??? //code snippet begins k=1; while (k<n){ ... ++k; } //code snippet ends
4th Apr 2018, 6:48 PM
Alex
Alex - avatar
+ 1
wait i'll post whole code here
4th Apr 2018, 6:49 PM
Nilesh Ingale
Nilesh Ingale - avatar
+ 1
This fun() does not go on forever as there will be a condition of fun(1) which will return the value 1 and it will terminate the loop.
6th Apr 2018, 2:25 AM
Nilesh Ingale
Nilesh Ingale - avatar
0
Before the loop actually starts looping k is set to 1 if that was your question Nilesh Ingale
4th Apr 2018, 6:51 PM
Alex
Alex - avatar
0
this is a recursive function which i think probably goes on forever as long as fun() is being called inside.prove me wrong pls
5th Apr 2018, 11:01 PM
Bate Martin
Bate Martin - avatar
0
It should be "2" because ++k means k increment before execution of the loop
14th Nov 2018, 3:31 PM
AKASH GHADI
AKASH GHADI - avatar