Quick Question about Scope
Dec 6, 2011 at 10:19am UTC
Hi Guys,
Can anybody lead me in the right direction in regards to why my variable i is losing scope in the following code:
1 2 3 4 5 6 7 8 9 10 11
for (int i = 1; i <= 2*no_of_divisions-1; i++)
if (initial_stock_price*pow(up_factor, i) > barrier_price)
float probability_stock_path_has_hit_barrier_by_the_time_it_got_here =
exp( (-2.0/(volatility*volatility*expiration_time)) *
log(initial_stock_price/barrier_price)*
log(initial_stock_price*pow(up_factor, ((float ) i))/barrier_price) );
V_T(i ,1) = (1.0/pow(R, no_of_divisions)) *
max(0.0, (initial_stock_price*pow(up_factor,i-no_of_divisions)) - strike_price);
I bolded and underlined the i that is losing scope. Is there an issue with imbedding an if inside a for statement that is taking i out of scope?
Dec 6, 2011 at 10:33am UTC
well, you need curly braces. Currently it's like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
for (int i = 1; i <= 2*no_of_divisions-1; i++)
{
if (initial_stock_price*pow(up_factor, i) > barrier_price)
{
float probability_stock_path_has_hit_barrier_by_the_time_it_got_here =
exp( (-2.0/(volatility*volatility*expiration_time)) *
log(initial_stock_price/barrier_price)*
log(initial_stock_price*pow(up_factor, ((float ) i))/barrier_price) );
}
}
V_T(i,1) = (1.0/pow(R, no_of_divisions)) *
max(0.0, (initial_stock_price*pow(up_factor,i-no_of_divisions)) - strike_price);
Dec 6, 2011 at 10:56am UTC
I understand the practicality of using the curly braces but it isn't absolutely necessary is it? For instance I can compile the code without the aforementioned curly braces if I switch around a few things.
Dec 6, 2011 at 11:04am UTC
But why would you? Curly braces allow you more freedom in organizing your code ánd help with clarity.
Dec 6, 2011 at 11:13am UTC
For instance I can compile the code without the aforementioned curly braces if I switch around a few things.
How so?
Topic archived. No new replies allowed.