Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

if else continue

Posted on 2008-06-23
16
Medium Priority
?
1,336 Views
Last Modified: 2012-06-27
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
Comment
Question by:zizi21
  • 4
  • 3
  • 3
  • +4
16 Comments
 
LVL 2

Accepted Solution

by:
figroc earned 800 total points
ID: 21852157
just eliminate the last two sentences
0
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 200 total points
ID: 21852343
>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
 
LVL 15

Assisted Solution

by:mr_egyptian
mr_egyptian earned 200 total points
ID: 21852357
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:mr_egyptian
ID: 21852370
Sorry for any repeat info posted by sunnycoder.  I'm a slow typist.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 21852381
np mr_egyptian :)
0
 

Author Comment

by:zizi21
ID: 21852774
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 21852778
then just omit the last 2 lines as figroc had suggested.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21852940
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
 
LVL 85

Assisted Solution

by:ozo
ozo earned 200 total points
ID: 21852952
a null statement can be written as
  ;
or as
  {}
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 600 total points
ID: 21853002
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
 
LVL 3

Expert Comment

by:sistemu
ID: 21853915
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
 
LVL 40

Expert Comment

by:evilrix
ID: 21853944
@sistemu, I'm sure if you read this thread carefully you'll see that point has already been made -- more than once!
0
 

Author Comment

by:zizi21
ID: 21854084
just  saw this...pls give me some time to read...
0
 
LVL 3

Expert Comment

by:sistemu
ID: 21854117
I've just explained it with more English and less IT :D
That's all :)
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21854216
>> 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
 

Author Closing Comment

by:zizi21
ID: 31470028
thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question