Solved

# the if else statement

Posted on 2000-03-13
213 Views
how do i write a multiple if and else statement..
0
Question by:best2380
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 7

Accepted Solution

KangaRoo earned 5 total points
ID: 2612898
int i = something;
if( i == 1)
/* do something */;
else if( i == 2 )
/* do something else */;
else if( i == 3 )
/* do something completely different */;
else
/* out of idea's ;) */;
0

Expert Comment

ID: 2612990
You can use the way KaangaRoo has shown or if the decision depends only on the integer u will be interested using Switch-case Statement.
e.g.
Switch (integer)
{
case 1:{.
.
code
.
}
case 2:{.
.
code
}
//like that
default:{
code
}
}

upon the values the integer takes the code will be executed.  If integer doesn't take any value u have accounted for, default code is executed
0

Expert Comment

ID: 2612995
The switch command works well here too:

switch(i){
case 1:
(do something);
break;

case 2:
(do something else);
break;

default:
(default statement);
break;
}

0

LVL 3

Expert Comment

ID: 2613015
don't forget the confusing but fun ?: statements!

(the if expression)?(then exp):(else exp)

or the standard form

if(some expression)
{
block of then expression
}
else if(some expression
{
else if expression
}
else(optional)
{
else expression
}

~Aaron
0

LVL 7

Expert Comment

ID: 2613025
True, for a straight forward case like my example a swith is nicer, but 'if - else if' can be used with other comparisions then '=='

int i = something;
if( i < 5)
/* do something */;
else if( 1 <= 10 || i > 17)
/* do something else */;
else if( i == 13 )
/* do something completely different */;
else
/* out of idea's ;) */;
0

## Featured Post

Question has a verified solution.

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

### Suggested Solutions

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…
The goal of this video is to provide viewers with basic examples to understand and use pointers 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.
###### Suggested Courses
Course of the Month6 days, 4 hours left to enroll