[Webinar] Streamline your web hosting managementRegister Today

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
The Firewall Audit Checklist

Preparing for a firewall audit today is almost impossible.
AlgoSec, together with some of the largest global organizations and auditors, has created a checklist to follow when preparing for your firewall audit. Simplify risk mitigation while staying compliant all of the time!

 
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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