eetaylog Posted November 20, 2009 Share Posted November 20, 2009 im writing an if-else loop that calls on float values x, y and z but need to include an if statement that says: if(x y and z are all equal in value, then)... else... im not really sure about how to construct the if(..) statement though, would it be if(x=y=z)? Link to comment Share on other sites More sharing options...
Stu Posted November 20, 2009 Share Posted November 20, 2009 (edited) if(a==1 && b==1 && c=1) { do something } else { do something else } Edited November 20, 2009 by Stu Link to comment Share on other sites More sharing options...
eetaylog Posted November 20, 2009 Author Share Posted November 20, 2009 if(a==1 && b==1 && c=1) { do something } else { do something else } of course, bleedin obvious now youve pointed it out! cheers stu. Link to comment Share on other sites More sharing options...
stoo2000 Posted November 20, 2009 Share Posted November 20, 2009 if(a==1 && b==1 && c==1) { do something } else { do something else } You missed a bit Link to comment Share on other sites More sharing options...
eetaylog Posted November 20, 2009 Author Share Posted November 20, 2009 (edited) if(a==1 && b==1 && c=1) { do something } else { do something else } hang on a minute, ive just looked at your answer again and i dont think its exactly what im looking for, what i need is a statement that says if(x,y and z are all the same value) {function} else {function} what yours says is: if(x=1 and b=1 and c=1) {function} else {function} or am i missing something? Edited November 20, 2009 by eetaylog Link to comment Share on other sites More sharing options...
Stu Posted November 20, 2009 Share Posted November 20, 2009 if(a==1 && b==1 && c==1) { do something } else { do something else } You missed a bit I missed a lot, but the essence is there. I'd expect people understand the use of semi-colons if they're writing C! if(a==1 && b==1 && c=1) { do something } else { do something else } hang on a minute, ive just looked at your answer again and i dont think its exactly what im looking for, what i need is a statement that says if(x,y and z are all the same value) {function} else {function} what yours says is: if(x=1 and b=1 and c=1) {function} else {function} or am i missing something? I think you must be missing something. Link to comment Share on other sites More sharing options...
eetaylog Posted November 20, 2009 Author Share Posted November 20, 2009 (edited) ok, so if i use the program below (some of the maths is missing but youll get the idea) with your if statement, then if x,y and z are equal (for any value, i.e x,y and z = 5 or x,y,z = 10) i will get OUTPUT1, otherwise ill get OUTPUT2. correct? main() { float a1, float a2 float b1, floatb2, float c1 float c2, float x, float y, float z; printf("please enter values a1,b1 and c1\n");scanf(%f%f%f,a1,b1,c1); printf("please enter values a2,b2 and c2\n");scanf(%f%f%f,a2,b2,c2); x = a2/a1; y = b2/b1; z = c2/c1; if(a==1 && b==1 && c=1) { printf("OUTPUT1\n"); } else { printf("OUTPUT2\n"); } } Edited November 20, 2009 by eetaylog Link to comment Share on other sites More sharing options...
Stu Posted November 20, 2009 Share Posted November 20, 2009 No.. You'd need to have something like matchvar = 1; if (x == matchvar && y == matchvar && z == matchvar) { do something; } else { do something else; }; Link to comment Share on other sites More sharing options...
eetaylog Posted November 20, 2009 Author Share Posted November 20, 2009 No.. You'd need to have something like matchvar = 1; if (x == matchvar && y == matchvar && z == matchvar) { do something; } else { do something else; }; right, i thought so, that makes more sense. ill have a play around.. Link to comment Share on other sites More sharing options...
Stu Posted November 20, 2009 Share Posted November 20, 2009 Why don't you tell me what you're trying to achieve, logically, as i'm not convinced this is the best way to go (for anything) ? Link to comment Share on other sites More sharing options...
eetaylog Posted November 20, 2009 Author Share Posted November 20, 2009 (edited) fyi, ive done it this way now. seems to work: x = a2/a1;y = b2/b1;z = c2/c1; test = (x+y+z)/3; if(test=x)... else... Edited November 20, 2009 by eetaylog Link to comment Share on other sites More sharing options...
Stu Posted November 20, 2009 Share Posted November 20, 2009 There are many ways to skin a cat.. but not many ways to correctly architect a piece of software! Link to comment Share on other sites More sharing options...
bluliz Posted November 20, 2009 Share Posted November 20, 2009 (edited) fyi, ive done it this way now. seems to work: x = a2/a1;y = b2/b1;z = c2/c1; test = (x+y+z)/3; if(test=x)... else... But what if X=5Y=6 Z=4 test = (5+6+4)/3;If(5=5) But X was not the same as Y or Z... Maybe you want to compare X to Y if true then compare X to Z Edited November 20, 2009 by bluliz Link to comment Share on other sites More sharing options...
eetaylog Posted November 20, 2009 Author Share Posted November 20, 2009 (edited) But what if X=5Y=6 Z=4 test = (5+6+4)/3;If(5=5) But X was not the same as Y or Z... Maybe you want to compare X to Y if true then compare X to Z <3 i know what youre saying, i spotted this earlier as well. eg, x=5y=7z=3 would slip through the net so to speak. i think ive got around it now though by using x = a2/a1;y = b2/b1;z = c2/c1; test1 = x/y;test2 = y/z; if(test1&&test2=1)... else... i will have to wait til monday to test it on a DSP now though. @stu.. im writing a program that solves simultaneous equations, ie, the solutions to a1x + b1y = c1a2x + b2y = c2 ive got most of the program wrapped up, just couldnt get the if-else loop bit right. ill stick my full program on here later on if youre interested. Edited November 20, 2009 by eetaylog Link to comment Share on other sites More sharing options...
stoo2000 Posted November 20, 2009 Share Posted November 20, 2009 surely in your If statement you need == not just = otherwise you are assigning the value (1) to test2, so it's always going to be true. what about... if( a==b && b==c ){... Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now