Difference between if if and if if else

L

Logan Lee

Sun, 23 Dec 2007 10:34:54 +0000ì—, Logan Lee ì¼ìŠµë‹ˆë‹¤:
Hi. What is the difference between

if {}
if {}

if {}
if else {}

?

I meant else if and not if else.
 
V

vippstar

Sun, 23 Dec 2007 10:34:54 +0000¿¡, Logan Lee ½è½À´Ï´Ù:





I meant else if and not if else.


if( g() == -1 ) {
/* ... */
} else if( h() == -1) {
/* ... */
}

Both g and h write to stdout, but h will be called ONLY if g returns
-1.
 
J

James Kuyper

Logan said:
Sun, 23 Dec 2007 10:34:54 +0000ì—, Logan Lee ì¼ìŠµë‹ˆë‹¤:


I meant else if and not if else.

Let's write something that more closely resembles legal C code:

A:
if (condition1) { statements1;}
if (condition2) { statements2;}

B:
if(condition1) {statement1;}
else if(condition2) {statements2;}

To keep this simple, I'm using statements1 and statments to each
represent a series of statements that doesn't include a return
statement, and doesn't directly or indirectly call exit(), abort(), or
longjmp().

The difference between A: and B: is that in A:, condition2 is evaluated
regardless of what condition1 is; in B:, condition2 is evaluated only if
condition1 compares equal to 0. In A:, statements2 are always executed
if condition2 does not compare equal to 0, regardless of what condition1
is. In B:, statements2 are only executed if condition1 compares equal to
0 and condition two does not compare equal to 0.
 
P

pete

Logan said:
Hi. What is the difference between

if {}
if {}

if {}
if else {}

?

The relationship between "if" and "else" is
not usually described in terms of differences and similarities.
An else statement can only occur
imediately following an if statement.
The else statement is executed if and only if
the condition for the preceding if statement evaluates to zero.
 
M

Mark McIntyre

Hi. What is the difference between

if {}
if {}

if {}
if else {}

This isn't a C question, its a simple logic question. Why not think the
logic through, instead of sitting with your brain in neutral?
 
S

santosh

if( g() == -1 ) {
/* ... */
} else if( h() == -1) {
/* ... */
}

Both g and h write to stdout, but h will be called ONLY if g returns
-1.

Only if g does not return -1.
 
J

Joe Wright

Logan said:
Sun, 23 Dec 2007 10:34:54 +0000ì—, Logan Lee ì¼ìŠµë‹ˆë‹¤:


I meant else if and not if else.

In the first case, given a series of if statements, each will be
evaluated in order and action taken depending on each condition.

The second case, the series of if statements will be evaluated until one
of them is true. The action for that if will be taken and the rest of
the if statements will be ignored.

I find the 'else if' construct more comfortable and flexible that C's
switch() construct.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top