How does one declare a variable inside an if () statement?
c++
Solution
If you want specific scope for value, you can introduce a scope block.
#include <iostream>
int get_value() {
return 101;
}
int main() {
{
int value = get_value();
if(value > 100)
std::cout << "Hey!";
} //value out of scope
}
Problem
Possible Duplicate: Declaring and initializing a variable in a Conditional or Control statement in C++ Instead of this... ``` int value = get_value(); if ( value > 100 ) { // Do something with value. } ``` ... is it possible to reduce the scope of value to only where it is needed: ``` if ( int value = get_value() > 100 ) { // Obviously this doesn't work. get_value() > 100 returns true, // which is implicitly converted to 1 and assigned to value. } ```