Solved

the if else statement

Posted on 2000-03-13
5
208 Views
Last Modified: 2010-05-18
how do i write a multiple if and else statement..
0
Comment
Question by:best2380
5 Comments
 
LVL 7

Accepted Solution

by:
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

by:nadt
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

by:DontPanic
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

by:BudVVeezer
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

by:KangaRoo
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dll Dynamic way c/c++ 9 139
Way to improve it 16 61
undefined reference to `bswap_128' 9 136
Trouble linking program with -lcrypt 3 141
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
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.

932 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