472,378 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Conditional statement ALWAYS works

The conditional statement "if(j.... always executes regardless of what
you enter.

BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)
{
char buffer[256]="";
GetWindowText(hwnd,buffer,256);
if(!strcmp(buffer,""))
{
return TRUE;
}
printf("%d. %s\n",i,buffer);

cout << "is this it?: " << flush;
int j = 0;
cin >> j;
cout << endl;
if (j=1) cout << hwnd << buffer << endl; //This always excecutes

//regardless of input
i++;
return TRUE;
}

Futher, if you change it to "int j = 1", it will never execute -
regardless of imput.

Any help would be appreciated.

Nov 22 '05 #1
8 1579
arganx wrote:

The conditional statement "if(j.... always executes regardless of what
you enter.

BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)
{
char buffer[256]="";
GetWindowText(hwnd,buffer,256);
if(!strcmp(buffer,""))
{
return TRUE;
}
printf("%d. %s\n",i,buffer);

cout << "is this it?: " << flush;
int j = 0;
cin >> j;
cout << endl;
if (j=1) cout << hwnd << buffer << endl; //This always excecutes


This is not a condition but an assignment

if( j == 1 )

Now, *this* is a condition

--
Karl Heinz Buchegger
kb******@gascad.at
Nov 22 '05 #2
On 2005-11-14 16:52, Karl Heinz Buchegger wrote:
This is not a condition but an assignment

if( j == 1 )

Now, *this* is a condition


Out of curiosity, can an assignment return false? And if so under which
conditions?

Erik Wikström
--
"I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure
out how to use my telephone" -- Bjarne Stroustrup
Nov 22 '05 #3
Karl Heinz Buchegger wrote:
arganx wrote:
The conditional statement "if(j.... always executes regardless of what
you enter.

BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)
{
char buffer[256]="";
GetWindowText(hwnd,buffer,256);
if(!strcmp(buffer,""))
{
return TRUE;
}
printf("%d. %s\n",i,buffer);

cout << "is this it?: " << flush;
int j = 0;
cin >> j;
cout << endl;
if (j=1) cout << hwnd << buffer << endl; //This always excecutes

This is not a condition but an assignment

if( j == 1 )

Now, *this* is a condition

I would like to add:
If you didn't get a compiler warning for this, you should compiler with
a higher warning level. The compiler can help help you with a lot of
mistakes like this one.

Gabriel
Nov 22 '05 #4
Erik Wikström wrote:
On 2005-11-14 16:52, Karl Heinz Buchegger wrote:
This is not a condition but an assignment

if( j == 1 )

Now, *this* is a condition

Out of curiosity, can an assignment return false? And if so under which
conditions?

Erik Wikström

The assignment returns the content of the variable assigned to.
Thus
if (j=0)
is false and would not execute.
Nov 22 '05 #5
Gabriel wrote:
Karl Heinz Buchegger wrote:
arganx wrote:
The conditional statement "if(j.... always executes regardless of what
you enter.

BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)
{
char buffer[256]="";
GetWindowText(hwnd,buffer,256);
if(!strcmp(buffer,""))
{
return TRUE;
}
printf("%d. %s\n",i,buffer);

cout << "is this it?: " << flush;
int j = 0;
cin >> j;
cout << endl;
if (j=1) cout << hwnd << buffer << endl; //This always excecutes

This is not a condition but an assignment

if( j == 1 )

Now, *this* is a condition

I would like to add:
If you didn't get a compiler warning for this, you should compiler with
a higher warning level. The compiler can help help you with a lot of
mistakes like this one.

Gabriel


Agreed. In fact, I'd suggest you should probably always compile on the
maximum warning level and alter the code (or manually disable specific
warnings) to get a warning-less build.

Also, programming style can help with cases like yours if you train
yourself to put constants first in equality tests:

if( 1 == j )

instead of

if( j == 1 )

Then there can be no confusion and compiler will always complain with
an error if you forget the second equal sign.

Cheers! --M

Nov 22 '05 #6
mlimber wrote:
...
Also, programming style can help with cases like yours if you train
yourself to put constants first in equality tests:

if( 1 == j )

instead of

if( j == 1 )

Then there can be no confusion and compiler will always complain with
an error if you forget the second equal sign.
...


In my personal opinion (and I'm not alone in this, according to many
discussions on that subject I saw) the negative impact this habit has on
the readability of the code far outweigh the planned benefits. I, for
example, always use the "natural" order of operands in comparison

if (j == 1)

and I've never had any problems of the above nature.

The reversed notation '(1 == j)' is just one of those things that seem
to make sense in theory but have very little or no value in practice.

--
Best regards,
Andrey Tarasevich
Nov 22 '05 #7
Andrey Tarasevich wrote:

mlimber wrote:
...
Also, programming style can help with cases like yours if you train
yourself to put constants first in equality tests:

if( 1 == j )

instead of

if( j == 1 )

Then there can be no confusion and compiler will always complain with
an error if you forget the second equal sign.
...


In my personal opinion (and I'm not alone in this, according to many
discussions on that subject I saw) the negative impact this habit has on
the readability of the code far outweigh the planned benefits. I, for
example, always use the "natural" order of operands in comparison

if (j == 1)

and I've never had any problems of the above nature.


Same here.

Those mistakes stopped long ago, when I changed my coding style from
if(j=1)
to
if( j == 1 )

Just adding whitespace (and of course getting more practice) did the trick.

--
Karl Heinz Buchegger
kb******@gascad.at
Nov 22 '05 #8
>In fact, I'd suggest you should probably always compile on the maximum warning level

Usually that is very good advice, except with certain compilers (e.g.
Microsoft Visual Studio) that proceed to emit vast amounts of warnings
about the vendor supplied header files that would swamp any warnings
about your own code. So the next level down might be more helpful.

Nov 22 '05 #9

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

Similar topics

8
by: neblackcat | last post by:
Would anyone like to comment on the following idea? I was just going to offer it as a new PEP until it was suggested that I post it here for comment & consideration against PEP 308. I'm far...
4
by: TheKeith | last post by:
I just wrote the following script for something I'm working on: ---------------------------------------------------------------------------- ------------------- <html> <head> <script...
11
by: Steven T. Hatton | last post by:
I've made no secret of the fact that I really dislike the C preprocessor in C++. No aspect of the language has caused me more trouble. No aspect of the language has cause more code I've read to be...
62
by: Reinhold Birkenfeld | last post by:
Hi, after Guido's pronouncement yesterday, in one of the next versions of Python there will be a conditional expression with the following syntax: X if C else Y which is the same as today's...
2
by: Megan | last post by:
Can you write conditional VBA code that affects only one or two records on a continuous subform? I have a form with a subform on it. The parent/ child field that links the forms is CaseID. The...
2
by: Sara | last post by:
The problem: Conditional formatting bold, red when field Value < date() sets the field background to white - always - whether condition is met or not. I want the field unfilled and just red/bold...
5
by: paulo | last post by:
Can anyone please tell me how the C language interprets the following code: #include <stdio.h> int main(void) { int a = 1; int b = 10; int x = 3;
43
by: dev_cool | last post by:
Hello friends, I'm a beginner in C programming. One of my friends asked me to write a program in C.The purpose of the program is print 1 to n without any conditional statement, loop or jump. ...
13
by: Neal Becker | last post by:
In hindsight, I am disappointed with the choice of conditional syntax. I know it's too late to change. The problem is y = some thing or other if x else something_else When scanning this my...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

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.