Solved

if else continue

Posted on 2008-06-23
16
1,325 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
ID: 21852157
just eliminate the last two sentences
0
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 50 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 50 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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 84

Assisted Solution

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

Assisted Solution

by:evilrix
evilrix earned 150 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

770 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