473,385 Members | 1,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Difference between If statements and Cases

What is the difference between them and when to use them where?
Nov 13 '07 #1
9 13900
Technically, there is no difference. It's simply a matter of formatting your code.
To explain, if you had multiple boolean conditions to test, your code starts to get jumbled-looking with multiple IF statements. Compare the two methods below:

Expand|Select|Wrap|Line Numbers
  1. If text = "Black" Then
  2.     'Code executes here...
  3. Else If text = "Yellow" Then
  4.     'Code executes here...
  5. Else If text = "Green" Then
  6.     'Code executes here...
  7. Else If text = "Red" Then
  8.     'Code executes here...
  9. End If
  10.  
  11. **********   Now compare the CASE statement  *******
  12.  
  13. Select Case text
  14.     Case "Black"
  15.          'Code executes here...
  16.     Case "Yellow"
  17.          'Code executes here...
  18.     Case "Green"
  19.          'Code executes here...
  20.     Case "Red"
  21.          'Code executes here...
  22. End Case
So in the end it's up to the programmer but it makes your code far more readable to use CASE on multiple condition testing.

Cheers!
Richard
Nov 13 '07 #2
Technically, there is no difference. It's simply a matter of formatting your code.
To explain, if you had multiple boolean conditions to test, you code starts to get jumbled looking with multiple IF statements. Compare the two methods below:

Expand|Select|Wrap|Line Numbers
  1. If text = "Black" Then
  2.     'Code executes here...
  3. Else If text = "Yellow" Then
  4.     'Code executes here...
  5. Else If text = "Green" Then
  6.     'Code executes here...
  7. Else If text = "Red" Then
  8.     'Code executes here...
  9. End If
  10.  
  11. **********   Now compare the CASE statement  *******
  12.  
  13. Select Case text
  14.     Case "Black"
  15.          'Code executes here...
  16.     Case "Yellow"
  17.          'Code executes here...
  18.     Case "Green"
  19.          'Code executes here...
  20.     Case "Red"
  21.          'Code executes here...
  22. End Case
So in the end it's up to the programmer but it makes your code far more readable to use CASE on multiple condition testing.

Cheers!
Richard
note that the spaces between "Else" and "If" in richard's code ("Else If" for each new condition) really shouldn't be there. if you put a space between, you need a separate End If at the end of each If clause, and they would all pile up at the end, making the code look even worse... but the idea was right ;)

and his main point stands uncorrected: the code is far more readable (and therefore easier to amend or troubleshoot), and also as far as i know slightly more effective to run. i might be corrected on that point though...
Nov 13 '07 #3
Killer42
8,435 Expert 8TB
and his main point stands uncorrected: the code is far more readable (and therefore easier to amend or troubleshoot), and also as far as i know slightly more effective to run. i might be corrected on that point though...
Couldn't say for sure, but my guess would be that a bunch of ElseIf's would produce the same compiled code as Case statements.

Note however, the main difference is that Select Case works based on the value of an expression which is evaluated at the start, while the If/ElseIf tests can be totally unrelated to each other if necessary. In other words, where the example says...
Expand|Select|Wrap|Line Numbers
  1. Select Case text
  2.   Case "Black"
  3.     ' Code executes here...
  4.   Case "Yellow"
  5.     ' Code executes here...
  6.   Case "Green"
  7.     ' Code executes here...
  8.   Case "Red"
  9.     ' Code executes here...
  10. End Case
You could be wanting to check different situations. For instance...
Expand|Select|Wrap|Line Numbers
  1. If Text = "BLACK" Then
  2.   ' Do something.
  3. ElseIf AgeNextBirthday < 38 Then
  4.   ' Do something else.
  5. ElseIf MyName = "Earl" Then
  6.   ' Do something else again.
  7. Else
  8.   ' Don't do anything.
  9. End If
Nov 14 '07 #4
AHMEDYO
112 100+
hi all

i think the most clear different is select case only work with constanct values

u cant say

select case (x)
case y: ' wrong
end select

but u can say

if (x=y) Then
''''
end if
Nov 15 '07 #5
Killer42
8,435 Expert 8TB
i think the most clear different is select case only work with constanct values ...
Actually, I think you're mistaken about that. I'll check and get back to you. (It'll have to wait until lunch time, though.)
Nov 15 '07 #6
AHMEDYO
112 100+
Actually, I think you're mistaken about that. I'll check and get back to you. (It'll have to wait until lunch time, though.)
ok man ,,waiting your reply
Nov 16 '07 #7
AHMEDYO
112 100+
ok man ,,waiting your reply
HI...

you are right Killer42, i was confused about Visual Basic and Visual C++ , because i am ordinary use Case statement only with C++

visual basic will accept this select statement

Expand|Select|Wrap|Line Numbers
  1.     Dim i As Long, y As Long, z As Long
  2.     Select Case i
  3.  
  4.     Case y:
  5.         MsgBox "ok"
  6.     Case z:
  7.  
  8.     End Select
with visual C++ you will have error

Expand|Select|Wrap|Line Numbers
  1.     DWORD X,Y,Z;
  2.     switch (Z)
  3.     {
  4.     case X:
  5.  
  6.         break;
  7.     case Y:
  8.  
  9.         break;
  10.     }
main.cpp(16) : error C2051: case expression not constant
main.cpp(19) : error C2051: case expression not constant

Sorry Guys.
Nov 16 '07 #8
Mohan Krishna
115 100+
Hi everyone!

If I am not wrong...

Killer2 is right.
The "Switch...case" is used on one variable and whose value is in a list of constants (an Array like). But in the case of "if...else", it is quite observable that the expressions (or conditions) on different variables can be used.

Is'nt it?

ThnQ
Nov 16 '07 #9
Killer42
8,435 Expert 8TB
Sorry, haven't had a chance to check yet. I've been very busy.
Nov 16 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: root | last post by:
Hi group, Apologies in advance if this has been asked somewhere before, but I haven't managed to get anything from the Google archives - I've been getting introductory guides to C++ all day...
65
by: He Shiming | last post by:
Hi, I just wrote a function that has over 200 "cases" wrapped in a "switch" statement. I'm wondering if there are performance issues in such implementation. Do I need to optimize it some way? ...
13
by: My4thPersonality | last post by:
I am reading something about the details of C#, and I came acros the statements readonly and const. I do not understand, it seems to be the same, what's the difference? Here is the text were it...
34
by: Jeff | last post by:
For years I have been using VBA extensively for updating data to tables after processing. By this I mean if I had to do some intensive processing that resulted in data in temp tables, I would have...
6
by: nikhilbhavsar | last post by:
Hi Friends , I want to know the exact difference between collection ID and qualifier. I also want to know in detail : When we perform BIND operation on a stored procedure, what we are actually...
7
by: amit | last post by:
Hello everybody, I need your advice on this. In my javascript I'm using two anchor <A> which both are to download a pdf file. That works fine but my question is why the "this" parameter in...
4
by: arnuld | last post by:
i am unable to understand the difference between a "C++ expression" and a "C++ statement". this is what i get from C++ Primer: expression The smallest unit of computation. An expression...
0
by: Michael Rudolph | last post by:
Hi DB2 newsgroup, I have encountered a difference between our development DB2 on Windows and the test environment on AIX. If using the LOCATE scalar function with a SMALLINT as LENGTH parameter...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.