473,387 Members | 1,575 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,387 software developers and data experts.

problem with logical operators

18
Hallo everyone,
I have written the below code but it doesn't work right.
The problem is that when I run it, and

the score_main array has a value of -3 or 3 and at the same time

the move_main is 9, I get as a "result" the number 3, which is wrong

according to this:

Expand|Select|Wrap|Line Numbers
  1.  for (i=0; i<8; i++)
  2.       {
  3.         if ((move_main==9) && ((score_main[i]!=3) || (score_main[i]!=-3)))
  4.         {
  5.               result=3;
  6.               }
  7.               }
where is my mistake???@#%#@!!! :(

Here is the whole code:


Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. int main()
  6. {
  7.  int score_main[8]={0,1,-2,-1,-3,1,0,1};
  8.  int move_main=9;
  9.  
  10.  int i,result;  
  11.  
  12.     for (i=0; i<8; i++) 
  13.     {
  14.        if (score_main[i]==3)
  15.               { 
  16.                   result=1;
  17.               } 
  18.               }
  19.  
  20.               for (i=0; i<8; i++) 
  21.               {
  22.                   if (score_main[i]==-3)
  23.                       {
  24.                            result=2;
  25.                       }
  26.  
  27.                       }
  28.  
  29.                       for (i=0; i<8; i++) 
  30.                       {
  31.                           if ((move_main<9) && ((score_main[i]!=3) || (score_main[i]!=-3)))
  32.                       {
  33.                            result=0;
  34.                       }
  35.                       }
  36.                       for (i=0; i<8; i++)
  37.                       {
  38.                           if ((move_main==9) && ((score_main[i]!=3) || (score_main[i]!=-3)))
  39.                       {
  40.                            result=3;
  41.                               }
  42.                               }
  43.  
  44.  
  45.       printf("%d\n", result);      
  46.  
  47.  
  48.  
  49.  system("PAUSE");
  50.  
  51. }
Feb 25 '08 #1
7 1499
weaknessforcats
9,208 Expert Mod 8TB
if ((move_main==9) && ((score_main[i]!=3) || (score_main[i]!=-3)))
If score_main ==3,
score_main[i]!=-3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE

If score_main == -3
score_main[i]!=3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE
If score_main ==2
score_main[i]!=3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE

If score_main ==20
score_main[i]!=3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE
If score_main ==-60
score_main[i]!=3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE

That is, the expression (x != 3) || (x!= -3) is always TRUE.

If you are intending "score_main[i] not equal 3 or not equal -3", then you code:

(score_main[i]!=3) && (score_main[i]!=-3)

This expression will be TRUE only when score_main[i] is not 3 AND not -3.
Feb 25 '08 #2
cube
18
If score_main ==3,
score_main[i]!=-3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE

If score_main == -3
score_main[i]!=3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE
If score_main ==2
score_main[i]!=3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE

If score_main ==20
score_main[i]!=3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE
If score_main ==-60
score_main[i]!=3 is TRUE AND move_main == 9 is TRUE -> Expression is TRUE

That is, the expression (x != 3) || (x!= -3) is always TRUE.

If you are intending "score_main[i] not equal 3 or not equal -3", then you code:

(score_main[i]!=3) && (score_main[i]!=-3)

This expression will be TRUE only when score_main[i] is not 3 AND not -3.
You are right about your thoughts!!!
Actually, this is the way I did it for the first time.
But, even if I tried it, your way (which is the right one) I still get wrong results.
What it happens, is that the result should be result=2
And that is what I get as I run the program, but unfortunatelly, it doesn't stop there.
It goes on to:

for (i=0; i<8; i++)
{
if ((move_main==9) && (score_main[i]!=3) && (score_main[i]!=-3))
{
result=3;
}
}

and returns result=3


WHY?????????
Feb 25 '08 #3
looker
18
You all right!... The last condition must be if ((move_main==9) && (score_main[i]!=3) && (score_main[i]!=-3)).
In your code above, you have 4 loops. In the two above, you check for score_main[i] value, the last 2 more, you check for score_main[i] and move_main.
All the four loops are used to assigned the value to variable result. This is the point.
Check yoru design again, If the condition has already been true in the first loop ( example ), it means the variable result is already set value. Is it necessary to go through the rest of code, i see that is not useful and your program can be more slower.
- In fact, you loops from 0 to 8 incremented by 1. You repeated this loop four times witch different condition stay inside. What if you just have only one loop and 4 condition ?
- If there is not codes below your code posted here, why you not return immediately after variable result is set. because your current design lead your program to go through the rest of code even though that is not necessary.
Feb 26 '08 #4
cube
18
Hi again and thank you for your time!

First of all I would like to mention that I am the newbiest newbie in C programming, (I start reading about C, just 2 weeks ago) though I'm trying hard to figure everything out. Now, about my problem...

- In fact, you loops from 0 to 8 incremented by 1. You repeated this loop four times witch different condition stay inside. What if you just have only one loop and 4 condition ?
I did try to do that, but it didn't work out (due to my mistaken code...of course)...therefore I wrote the above code.
Could I have a hint on how to do that? Pleaseeeeeeee....

- If there is not codes below your code posted here, why you not return immediately after variable result is set. because your current design lead your program to go through the rest of code even though that is not necessary.
Also I would like to know how to do that too!
Feb 26 '08 #5
sicarie
4,677 Expert Mod 4TB
cube-

Please actually make an attempt yourself first. That's how you will learn, by actually trying it. Post again with your updated code.

sicarie
Feb 26 '08 #6
cube
18
cube-

Please actually make an attempt yourself first. That's how you will learn, by actually trying it. Post again with your updated code.

sicarie
I'm sorry if I made you think that I'm looking for someone to solve my assignments, I didn't mean it.
I'm a married working woman with a child (and mature enough to respect the "laws" of universities) and I really really luck of free time.
I do my best and I want to learn as much as I can, though my whole schedual is too hard (to keep up with), I don't want to give up. I have worked to the bone with this one and I couldn't figure out what to do.
The help that I'm waiting for is sthg like "Try to use the "while" or the "else if" ect"
that's why I asked for a hint and not for the solution.
Am I asking too much???
Feb 27 '08 #7
sicarie
4,677 Expert Mod 4TB
You all right!... The last condition must be if ((move_main==9) && (score_main[i]!=3) && (score_main[i]!=-3)).
In your code above, you have 4 loops. In the two above, you check for score_main[i] value, the last 2 more, you check for score_main[i] and move_main.
All the four loops are used to assigned the value to variable result. This is the point.
Check yoru design again, If the condition has already been true in the first loop ( example ), it means the variable result is already set value. Is it necessary to go through the rest of code, i see that is not useful and your program can be more slower.
- In fact, you loops from 0 to 8 incremented by 1. You repeated this loop four times witch different condition stay inside. What if you just have only one loop and 4 condition ?
- If there is not codes below your code posted here, why you not return immediately after variable result is set. because your current design lead your program to go through the rest of code even though that is not necessary.
You do have that. You then asked for more.
Feb 27 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jay Moore | last post by:
If any of you happened to read my earlier posts, I had a dilemma with creating an efficient method of limiting access to data for my users and subusers. My heirarchy looks like this: Admins...
3
by: Andrew Durdin | last post by:
In Python, you can override the behaviour of most operators for a class, by defining __add__, __gt__, and the other special object methods. I noticed that, although there are special methods for...
34
by: sam | last post by:
Please look at the code below #include <stdio.h> int expr(char str, int i){ printf("%s \n",str); return i; }
80
by: Christopher Benson-Manica | last post by:
Of course one can get the effect with appropriate use of existing operators, but a ^^ operator would make for nice symmetry (as well as useful to me in something I'm working on). Am I the only one...
2
by: ThunderMusic | last post by:
Hi, I have a value that contains flags that I must get using a bitmask. I tryied with the && operator, but the compiler outputs this error : Operator '&&' cannot be applied to operands of type...
21
by: Mike | last post by:
I am having a problem getting AND to work. I have a MySQL database field defined as varbinary(8) with X00 in it. Then I have define statements with X01 in it. define('CERTACCESS_MEDALS', x01); ...
6
by: Protoman | last post by:
I'm writing a program to calc truth tables of arguments to prove that they are logically valid. Currently, I have to tell the program to print a truth table, and check it by hand to prove it's...
10
by: Jonathan | last post by:
Hi all, I'm having an argument with a co-worker about the difference between the & and && operators when applied to boolean operands in C#. His point of view is that the expression (false &...
9
by: marko | last post by:
/* code start */ int a = 0; /* expected evaluation and excution order with precedence in mind /* False(3) , True(1), False(2) */ if ( (a=1) == 0 || 0 != 1 && (a =2) == 1) putchar('T');...
7
by: Charles Law | last post by:
This is a very basic question, but I can' turn up a statement on the subject: In C#, if I have If (x == 1 && y == 2) { .... }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.