Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1339
  • Last Modified:

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...
0
zizi21
Asked:
zizi21
  • 4
  • 3
  • 3
  • +4
5 Solutions
 
figrocCommented:
just eliminate the last two sentences
0
 
sunnycoderCommented:
>is there such thing as continue that we use in while looopp...
continue to what? Can you provide more context?

>else if(i<0)
>value=value;
These lines have no effect and can be safely eliminated.

However, this would mean that your code continues to execute from next line unlike continue where it would begin to the beginning of the loop ...

Here again, it does not make sense to re-execute if (i>0) condition since that would make it an infinite loop. So basically we need more info from you about what you wish to accomplish
0
 
mr_egyptianCommented:
The continue statement is for use in a loop to pass through the loop body to the loop control structure.  In this case figroc is correct, though keep in mind that you're not accounting for i==0.
0
Building an Effective Phishing Protection Program

Join Director of Product Management Todd OBoyle on April 26th as he covers the key elements of a phishing protection program. Whether you’re an old hat at phishing education or considering starting a program -- we'll discuss critical components that should be in any program.

 
mr_egyptianCommented:
Sorry for any repeat info posted by sunnycoder.  I'm a slow typist.
0
 
sunnycoderCommented:
np mr_egyptian :)
0
 
zizi21Author Commented:
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...
0
 
sunnycoderCommented:
then just omit the last 2 lines as figroc had suggested.
0
 
evilrixSenior Software Engineer (Avast)Commented:
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

0
 
ozoCommented:
a null statement can be written as
  ;
or as
  {}
0
 
evilrixSenior Software Engineer (Avast)Commented:
Obviously, if your C compiler doesn't support C99 style comments you'd have to use legacy C style commenting.

As ozo has stated, you can use ; by itself to create a no-op but trying to mix that with a comment can lead to confusing and problematic code.

if(predicate)
   expression
else
   /* comment */ ;

Also, you cannot terminate C99 comments with a ; so they'd have to go on the next line or you'd get unexpected behavior.

if(predicate)
   expression
else
   // comment ; <---- OOPS

if(predicate)
{
   ++value;
}
else
{
    /* We take no action in this case */
}
 

Open in new window

0
 
sistemuCommented:
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.
0
 
evilrixSenior Software Engineer (Avast)Commented:
@sistemu, I'm sure if you read this thread carefully you'll see that point has already been made -- more than once!
0
 
zizi21Author Commented:
just  saw this...pls give me some time to read...
0
 
sistemuCommented:
I've just explained it with more English and less IT :D
That's all :)
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> 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.
0
 
zizi21Author Commented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

  • 4
  • 3
  • 3
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now