What is the difference between them and when to use them where?
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: - If text = "Black" Then
-
'Code executes here...
-
Else If text = "Yellow" Then
-
'Code executes here...
-
Else If text = "Green" Then
-
'Code executes here...
-
Else If text = "Red" Then
-
'Code executes here...
-
End If
-
-
********** Now compare the CASE statement *******
-
-
Select Case text
-
Case "Black"
-
'Code executes here...
-
Case "Yellow"
-
'Code executes here...
-
Case "Green"
-
'Code executes here...
-
Case "Red"
-
'Code executes here...
-
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
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: - If text = "Black" Then
-
'Code executes here...
-
Else If text = "Yellow" Then
-
'Code executes here...
-
Else If text = "Green" Then
-
'Code executes here...
-
Else If text = "Red" Then
-
'Code executes here...
-
End If
-
-
********** Now compare the CASE statement *******
-
-
Select Case text
-
Case "Black"
-
'Code executes here...
-
Case "Yellow"
-
'Code executes here...
-
Case "Green"
-
'Code executes here...
-
Case "Red"
-
'Code executes here...
-
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...
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... - Select Case text
-
Case "Black"
-
' Code executes here...
-
Case "Yellow"
-
' Code executes here...
-
Case "Green"
-
' Code executes here...
-
Case "Red"
-
' Code executes here...
-
End Case
You could be wanting to check different situations. For instance... - If Text = "BLACK" Then
-
' Do something.
-
ElseIf AgeNextBirthday < 38 Then
-
' Do something else.
-
ElseIf MyName = "Earl" Then
-
' Do something else again.
-
Else
-
' Don't do anything.
-
End If
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
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.)
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
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 - Dim i As Long, y As Long, z As Long
-
Select Case i
-
-
Case y:
-
MsgBox "ok"
-
Case z:
-
-
End Select
with visual C++ you will have error - DWORD X,Y,Z;
-
switch (Z)
-
{
-
case X:
-
-
break;
-
case Y:
-
-
break;
-
}
main.cpp(16) : error C2051: case expression not constant
main.cpp(19) : error C2051: case expression not constant
Sorry Guys.
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
Sorry, haven't had a chance to check yet. I've been very busy.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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?
...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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
|
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...
|
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...
| |