Link to home
Start Free TrialLog in
Avatar of zizi21
zizi21

asked on

if else continue

hi,

this is stupid question..i know but i dont know how to do it..could you pls help....

for instance,

if(i>0)
value+=1;

else if(i<0)
value=value;

now instead of putting value=value, is there such thing as continue that we use in while looopp...
ASKER CERTIFIED SOLUTION
Avatar of figroc
figroc
Flag of China image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry for any repeat info posted by sunnycoder.  I'm a slow typist.
np mr_egyptian :)
Avatar of zizi21
zizi21

ASKER

sorry...i was lazy typing everything...

i have this long if else statements where it needs to evaluate

if the first if statement is true do this

otherwise do nothing....

not the real thing but something like this

if(expr1 > 0)
do this...maybe add the value by one or somehting like that

else
do not add the value but just let the value remain...
then just omit the last 2 lines as figroc had suggested.
Are you looking to code this construct just to make the code self documenting? If so then just make the else part a non-operation with a comment, but make sure you use braces, otherwise the compiler will take the very next line and assume it is part of the else.

As an aside, value=value is almost certainly going to be optimized away by the compiler to be a no-op.


if(predicate)
{
   ++value; // Pre-increment is a better bet than += 1
}
else
{
    // We take no action in this case (you MUST use braces to encapsulate this comment)
}

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
C++ sintax doesn't necesarily need an else statement.
The code:
if(i>0)
value+=1;
Does everything you want:
if i is bigger than 0 then augments the variable value with one,
otherwise it remains unmodified.
No need for more instructions.
@sistemu, I'm sure if you read this thread carefully you'll see that point has already been made -- more than once!
Avatar of zizi21

ASKER

just  saw this...pls give me some time to read...
I've just explained it with more English and less IT :D
That's all :)
>> I've just explained it with more English and less IT :D
>> That's all :)
Why would that be necessary? This is an IT thread! If ziz21 needed clarification I'm sure he (or she) is more than capable of requesting it.
Avatar of zizi21

ASKER

thanks