Solved

if else continue

Posted on 2008-06-23
16
1,322 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 200 total points
Comment Utility
just eliminate the last two sentences
0
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 50 total points
Comment Utility
>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 50 total points
Comment Utility
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
 
LVL 15

Expert Comment

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

Expert Comment

by:sunnycoder
Comment Utility
np mr_egyptian :)
0
 

Author Comment

by:zizi21
Comment Utility
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
Comment Utility
then just omit the last 2 lines as figroc had suggested.
0
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 84

Assisted Solution

by:ozo
ozo earned 50 total points
Comment Utility
a null statement can be written as
  ;
or as
  {}
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 150 total points
Comment Utility
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
Comment Utility
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
Comment Utility
@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
Comment Utility
just  saw this...pls give me some time to read...
0
 
LVL 3

Expert Comment

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

Expert Comment

by:evilrix
Comment Utility
>> 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
Comment Utility
thanks
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures 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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now