473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

variable declaration in if statement

i can write something like this

int foo()
{
return 100;
}

if( int x=foo() ) {
....
}else{
....
}

but, for example, how can i check that x>50 in the "if" statement? So,
the if will be true only if x>50.

Aug 5 '06 #1
9 6777
On 5 Aug 2006 12:09:44 -0700 in comp.lang.c++, "Denis Petronenko"
<pe********@gmail.comwrote,
>if( int x=foo() ) {
...
}else{
...
}

but, for example, how can i check that x>50 in the "if" statement? So,
the if will be true only if x>50.
Write it like:

int x = foo();
if( x < 50 ) {
....

With no looping, there's nothing about "If" to keep you from
splitting it into two more understandable statements like that.

Aug 5 '06 #2

"David Harmon" <so****@netcom.comschrieb im Newsbeitrag
news:45****************@news.west.earthlink.net...
On 5 Aug 2006 12:09:44 -0700 in comp.lang.c++, "Denis Petronenko"
<pe********@gmail.comwrote,
>>if( int x=foo() ) {
...
}else{
...
}

but, for example, how can i check that x>50 in the "if" statement? So,
the if will be true only if x>50.

Write it like:

int x = foo();
if( x < 50 ) {
...

With no looping, there's nothing about "If" to keep you from
splitting it into two more understandable statements like that.
Why not use parantheses?

if( (int x=foo()) 50 ) {
....
}else{
....
}

Aug 5 '06 #3

David Harmon wrote:
On 5 Aug 2006 12:09:44 -0700 in comp.lang.c++, "Denis Petronenko"
<pe********@gmail.comwrote,
if( int x=foo() ) {
...
}else{
...
}

but, for example, how can i check that x>50 in the "if" statement? So,
the if will be true only if x>50.

Write it like:

int x = foo();
if( x < 50 ) {
...

With no looping, there's nothing about "If" to keep you from
splitting it into two more understandable statements like that.
i don't want to use x variable after if statement and want to reduce
scope of x variable to if statement only.

Aug 5 '06 #4

Jens Marder wrote:
"David Harmon" <so****@netcom.comschrieb im Newsbeitrag
news:45****************@news.west.earthlink.net...
On 5 Aug 2006 12:09:44 -0700 in comp.lang.c++, "Denis Petronenko"
<pe********@gmail.comwrote,
>if( int x=foo() ) {
...
}else{
...
}

but, for example, how can i check that x>50 in the "if" statement? So,
the if will be true only if x>50.
Write it like:

int x = foo();
if( x < 50 ) {
...

With no looping, there's nothing about "If" to keep you from
splitting it into two more understandable statements like that.

Why not use parantheses?

if( (int x=foo()) 50 ) {
...
}else{
...
}
because it doesn't compile. at least using this
g++ (GCC) 4.0.4 20060507 (prerelease) (Debian 4.0.3-3)

Aug 5 '06 #5
Jens Marder wrote:
"David Harmon" <so****@netcom.comschrieb im Newsbeitrag
news:45****************@news.west.earthlink.net...
>>On 5 Aug 2006 12:09:44 -0700 in comp.lang.c++, "Denis Petronenko"
<pe********@gmail.comwrote,
>>>if( int x=foo() ) {
...
}else{
...
}

but, for example, how can i check that x>50 in the "if" statement? So,
the if will be true only if x>50.

Write it like:

int x = foo();
if( x < 50 ) {
...

With no looping, there's nothing about "If" to keep you from
splitting it into two more understandable statements like that.


Why not use parantheses?

if( (int x=foo()) 50 ) {
...
}else{
...
}
That doesn't compile for me with g++. It seems that the 'int' has to
come before the second '('. However

#include <iostream>
using namespace std;

int foo() { return 100; }

int main(int argc, char *argv[]){
if (int x=foo() 50) {
cout << "condition true" << endl;
cout << "x == " << x << endl;
} else {
cout << "condition false" << endl;
cout << "x == " << x << endl;
}
}

does compile and seems to work as desired.

(int x= foo() 50) and (int x= (foo() 50)) seem to be equivalent in
this context.

(int x=foo() && (x>50)) seems to be legal but doesn't have the desired
meaning. '&&' has higher precedence than '=', so that the assignment
expression computes x in terms of its uninitialized self so that the
result is indeterminate.
Aug 5 '06 #6
* Jens Marder:
>
Why not use parantheses?

if( (int x=foo()) 50 ) {
...
}else{
...
}
Because it doesn't conform to the 'if' statement syntax; it's the same
as you cannot put parentheses around a declaration elsewhere.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 5 '06 #7
Matt wrote:
Jens Marder wrote:
>"David Harmon" <so****@netcom.comschrieb im Newsbeitrag
news:45****************@news.west.earthlink.net.. .
>>On 5 Aug 2006 12:09:44 -0700 in comp.lang.c++, "Denis Petronenko"
<pe********@gmail.comwrote,

if( int x=foo() ) {
...
}else{
...
}

but, for example, how can i check that x>50 in the "if" statement? So,
the if will be true only if x>50.
Write it like:

int x = foo();
if( x < 50 ) {
...

With no looping, there's nothing about "If" to keep you from
splitting it into two more understandable statements like that.


Why not use parantheses?

if( (int x=foo()) 50 ) {
...
}else{
...
}


That doesn't compile for me with g++. It seems that the 'int' has to
come before the second '('. However

#include <iostream>
using namespace std;

int foo() { return 100; }

int main(int argc, char *argv[]){
if (int x=foo() 50) {
cout << "condition true" << endl;
cout << "x == " << x << endl;
} else {
cout << "condition false" << endl;
cout << "x == " << x << endl;
}
}

does compile and seems to work as desired.
whoops. x is left with the value of the '>' expression, namely 0 or 1,
so that this doesn't do what the OP wanted.
Aug 5 '06 #8
Denis Petronenko wrote:
David Harmon wrote:
>>On 5 Aug 2006 12:09:44 -0700 in comp.lang.c++, "Denis Petronenko"
<pe********@gmail.comwrote,
>>>if( int x=foo() ) {
...
}else{
...
}

but, for example, how can i check that x>50 in the "if" statement? So,
the if will be true only if x>50.

Write it like:

int x = foo();
if( x < 50 ) {
...

With no looping, there's nothing about "If" to keep you from
splitting it into two more understandable statements like that.


i don't want to use x variable after if statement and want to reduce
scope of x variable to if statement only.
{
int x = foo();
if( x < 50 ) {
...
}
}
--
Ian Collins.
Aug 5 '06 #9
Denis Petronenko wrote:
David Harmon wrote:
>On 5 Aug 2006 12:09:44 -0700 in comp.lang.c++, "Denis Petronenko"
<pe********@gmail.comwrote,
>>if( int x=foo() ) {
...
}else{
...
}

but, for example, how can i check that x>50 in the "if" statement? So,
the if will be true only if x>50.
Write it like:

int x = foo();
if( x < 50 ) {
...

With no looping, there's nothing about "If" to keep you from
splitting it into two more understandable statements like that.

i don't want to use x variable after if statement and want to reduce
scope of x variable to if statement only.
Then put braces around both the variable declaration and the if statement.

{
int x = foo();
if ( x < 50 ) { ... }
}
Aug 5 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
7
by: YGeek | last post by:
Is there any difference between declaring a variable at the top of a method versus in the code of the method? Is there a performance impact for either choice? What about if the method will return...
5
by: Neil Zanella | last post by:
Hello, Unlike in pre-C99 versions of C where variables can only be defined at the beginning of blocks, C99 allows variables to be defined in arbitrary places inside blocks. However, gcc 3.2.2...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
2
by: Florian Loitsch | last post by:
hi, What should be the output of the following code-snippet? === var x = "global"; function f() { var x = 0; eval("function x() { return false; }"); delete x; alert(x); }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.