Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Can I use statements in the condition of an If Else statement?
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
lastlegion




PostPosted: Sat Aug 29, 2009 4:25 am   Post subject: Can I use statements in the condition of an If Else statement?

Basically I am trying to write a code
If (condition)
{
x == 5
}
else
{
y == 6
}
What I want is that both x == 5 and y ==6 should be executed in a single condition. I am planning to use a loop for it.
But whenever I use a loop inside the condition I get an error. So are statements allowed in the condition part of a programming language(any) or only expressions?
Sponsor
Sponsor
Sponsor
sponsor
[Gandalf]




PostPosted: Sat Aug 29, 2009 4:44 am   Post subject: RE:Can I use statements in the condition of an If Else statement?

I'm not sure what you're looking for exactly, about the x = 5 and y = 6 part, however to answer your question directly... Usually (I cannot say always, because programming languages differ wildly), only expressions can be used in the conditional part. If there was a statement involved, would that default to true or to false?

However, in some languages, so-called statements also return values, and in these cases you may include statements in conditionals.

All of that said, I think you're going about this the wrong way, however I'm not clear enough on what you're trying to do to help more.
DtY




PostPosted: Sat Aug 29, 2009 10:27 am   Post subject: RE:Can I use statements in the condition of an If Else statement?

It sounds like you want to check if x is 5 and y is 6? (If that's the case, your code is wrong) you can do

c:
if ((x==5) && (y==6))

Python:
if (x==5) and (y==6):


Depending on the language, it will look something like that.
DemonWasp




PostPosted: Sat Aug 29, 2009 3:02 pm   Post subject: RE:Can I use statements in the condition of an If Else statement?

When the condition on an IF statement is going to be more complex than a couple of lines, you can break it up into boolean values before the IF, which are then possibly to test for in the IF.

For example:
code:

boolean myTestBoolean = true;
loop
    // some conditions here that may change the value of myTestBoolean
end loop

if ( myTestBoolean ) {
    x = 5;
} else {
    y = 6;
}


If this isn't what you meant, then I suggest you try rephrasing your question.
lastlegion




PostPosted: Wed Sep 02, 2009 7:19 am   Post subject: Re: Can I use statements in the condition of an If Else statement?

Hey sorry...I meant '=' and not '=='

code:

If (condition)
{
x = 5
}
else
{
y = 6
}


I want both x=5 and y = 6 to be implemented by a single condition.

@DemonWasp: I have tried that...but I am allowed to modify the condition only :/
jbking




PostPosted: Wed Sep 02, 2009 8:54 am   Post subject: Re: Can I use statements in the condition of an If Else statement?

I would guess you could, but there is the question of what operator do you want to use to combine the results? If you always want both assignments done regardless of the results, then that rules out using the simple AND, OR, NAND and NOR if the language uses any short-circuit evaluation. You could use an XOR if the language supports it, though I would question why you'd want to do the assignments within a condition in that case as you are only catching the case where the assignments don't have the same result. There is the question of whether or not the language returns something on an assignment, as I don't think all languages would do that.
Drahcir




PostPosted: Wed Sep 02, 2009 3:57 pm   Post subject: Re: Can I use statements in the condition of an If Else statement?

lastlegion @ Wed Sep 02, 2009 7:19 am wrote:
I want both x=5 and y = 6 to be implemented by a single condition.

code:

if (condition) {
    x = 5;
    y = 6;
}

What? Your phrasing is confusing. Please elaborate.
OneOffDriveByPoster




PostPosted: Thu Sep 03, 2009 1:06 pm   Post subject: Re: Can I use statements in the condition of an If Else statement?

I think what the OP means is that s/he wants x to be 5 and y to be 6 after this block of code no matter what, but s/he is only allowed to change the code in the if-condition.

code:
if (condition)
{
   x = 5;
}
else
{
   y = 6;
}


So what jbking was saying.

A trick with C and similar languages is to make the condition do one of the assignments:
code:
if (y = 6)
{
   x = 5;
}
else
{
   y = 6;
}
Sponsor
Sponsor
Sponsor
sponsor
octopi




PostPosted: Thu Sep 03, 2009 2:01 pm   Post subject: Re: Can I use statements in the condition of an If Else statement?

When would such a situation exist?

code:

if (y = 6) {
   x = 5;
}
else {
   y = 6;
}


is the same as

code:

x = 5;
y = 6;
jbking




PostPosted: Thu Sep 03, 2009 2:45 pm   Post subject: Re: Can I use statements in the condition of an If Else statement?

octopi @ Thu Sep 03, 2009 12:01 pm wrote:
When would such a situation exist?

code:

if (y = 6) {
   x = 5;
}
else {
   y = 6;
}


is the same as

code:

x = 5;
y = 6;


Not quite. If the assignment to y fails, it is retried in the first case and x isn't necessarily 5 in that situation unlike the second code example that just does the assignment directly. Granted this wouldn't normally happen but how often are computers running with buggy data or something went down that wasn't supposed to go down? That is meant as a legitimate question as I think it happens more than we'd like to think if you look at The Daily WTF or Shark Tank.
OneOffDriveByPoster




PostPosted: Thu Sep 03, 2009 10:52 pm   Post subject: Re: Can I use statements in the condition of an If Else statement?

jbking @ Thu Sep 03, 2009 2:45 pm wrote:
Not quite. If the assignment to y fails, it is retried in the first case and x isn't necessarily 5 in that situation unlike the second code example that just does the assignment directly. Granted this wouldn't normally happen but how often are computers running with buggy data or something went down that wasn't supposed to go down? That is meant as a legitimate question as I think it happens more than we'd like to think if you look at The Daily WTF or Shark Tank.

Assuming C/C++ I think the code I suggested would not work if y does not hold a non-zero value when evaluated for the if-statement. I suppose this could legitimately happen if y is volatile or (depending on the memory model, etc.) with cases where there are multiple threads.

We can change the condition with a comma operator like so
code:

if (y = 6, 1)


This gets us y = 6 before x = 5. I believe that all the tools are here for a solution that sets x = 5 first.

EDIT: and of course, y could be some object of class type in C++...
wtd




PostPosted: Fri Sep 04, 2009 12:26 pm   Post subject: RE:Can I use statements in the condition of an If Else statement?

Perhaps part of the solution to this is to be sure that everyone here (especially lastlegion) is clear on the difference between a statement and an expression.
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: