Android: Count number of cases in switch statement, know when last case
I am busy working on an app with multiple switch statement that each have a different number of cases.
switch(variable){
case: 0
//do something
case: 1
//do something
case: 3
//do something
I can iterate through the cases by using variable = variable +1. That works fine. My problem is being able to tell when the last case in the switch statement has been reached so that I can perform a different action.
How do I know when the count is at the last case? What is the code for that?
I appreciate your time.
android switch-statement counting
|
show 1 more comment
I am busy working on an app with multiple switch statement that each have a different number of cases.
switch(variable){
case: 0
//do something
case: 1
//do something
case: 3
//do something
I can iterate through the cases by using variable = variable +1. That works fine. My problem is being able to tell when the last case in the switch statement has been reached so that I can perform a different action.
How do I know when the count is at the last case? What is the code for that?
I appreciate your time.
android switch-statement counting
2
What do you mean with "last case"?
– tm13
Nov 23 '18 at 5:05
1
Can't you just add your action on the last case?
– josh_gom3z
Nov 23 '18 at 5:05
@tm13 when i say last case i mean, using the example above, case: 3. it could be another number in another switch statement.
– Stark
Nov 23 '18 at 5:12
@josh_gom3z in the last case a button will show that will offer the user the option to do something else. I could add it to the last case but I would have to go through each switch statement and there are a lot so I am trying to find a way to count the cases in a given switch statement in order to be able to tell when the last case for a particular switch statement has been reached.
– Stark
Nov 23 '18 at 5:15
1
@Stark could you share your code so that we have clear understanding about your problem? Actually, you can use default case
– tm13
Nov 23 '18 at 5:31
|
show 1 more comment
I am busy working on an app with multiple switch statement that each have a different number of cases.
switch(variable){
case: 0
//do something
case: 1
//do something
case: 3
//do something
I can iterate through the cases by using variable = variable +1. That works fine. My problem is being able to tell when the last case in the switch statement has been reached so that I can perform a different action.
How do I know when the count is at the last case? What is the code for that?
I appreciate your time.
android switch-statement counting
I am busy working on an app with multiple switch statement that each have a different number of cases.
switch(variable){
case: 0
//do something
case: 1
//do something
case: 3
//do something
I can iterate through the cases by using variable = variable +1. That works fine. My problem is being able to tell when the last case in the switch statement has been reached so that I can perform a different action.
How do I know when the count is at the last case? What is the code for that?
I appreciate your time.
android switch-statement counting
android switch-statement counting
asked Nov 23 '18 at 5:01
StarkStark
409
409
2
What do you mean with "last case"?
– tm13
Nov 23 '18 at 5:05
1
Can't you just add your action on the last case?
– josh_gom3z
Nov 23 '18 at 5:05
@tm13 when i say last case i mean, using the example above, case: 3. it could be another number in another switch statement.
– Stark
Nov 23 '18 at 5:12
@josh_gom3z in the last case a button will show that will offer the user the option to do something else. I could add it to the last case but I would have to go through each switch statement and there are a lot so I am trying to find a way to count the cases in a given switch statement in order to be able to tell when the last case for a particular switch statement has been reached.
– Stark
Nov 23 '18 at 5:15
1
@Stark could you share your code so that we have clear understanding about your problem? Actually, you can use default case
– tm13
Nov 23 '18 at 5:31
|
show 1 more comment
2
What do you mean with "last case"?
– tm13
Nov 23 '18 at 5:05
1
Can't you just add your action on the last case?
– josh_gom3z
Nov 23 '18 at 5:05
@tm13 when i say last case i mean, using the example above, case: 3. it could be another number in another switch statement.
– Stark
Nov 23 '18 at 5:12
@josh_gom3z in the last case a button will show that will offer the user the option to do something else. I could add it to the last case but I would have to go through each switch statement and there are a lot so I am trying to find a way to count the cases in a given switch statement in order to be able to tell when the last case for a particular switch statement has been reached.
– Stark
Nov 23 '18 at 5:15
1
@Stark could you share your code so that we have clear understanding about your problem? Actually, you can use default case
– tm13
Nov 23 '18 at 5:31
2
2
What do you mean with "last case"?
– tm13
Nov 23 '18 at 5:05
What do you mean with "last case"?
– tm13
Nov 23 '18 at 5:05
1
1
Can't you just add your action on the last case?
– josh_gom3z
Nov 23 '18 at 5:05
Can't you just add your action on the last case?
– josh_gom3z
Nov 23 '18 at 5:05
@tm13 when i say last case i mean, using the example above, case: 3. it could be another number in another switch statement.
– Stark
Nov 23 '18 at 5:12
@tm13 when i say last case i mean, using the example above, case: 3. it could be another number in another switch statement.
– Stark
Nov 23 '18 at 5:12
@josh_gom3z in the last case a button will show that will offer the user the option to do something else. I could add it to the last case but I would have to go through each switch statement and there are a lot so I am trying to find a way to count the cases in a given switch statement in order to be able to tell when the last case for a particular switch statement has been reached.
– Stark
Nov 23 '18 at 5:15
@josh_gom3z in the last case a button will show that will offer the user the option to do something else. I could add it to the last case but I would have to go through each switch statement and there are a lot so I am trying to find a way to count the cases in a given switch statement in order to be able to tell when the last case for a particular switch statement has been reached.
– Stark
Nov 23 '18 at 5:15
1
1
@Stark could you share your code so that we have clear understanding about your problem? Actually, you can use default case
– tm13
Nov 23 '18 at 5:31
@Stark could you share your code so that we have clear understanding about your problem? Actually, you can use default case
– tm13
Nov 23 '18 at 5:31
|
show 1 more comment
2 Answers
2
active
oldest
votes
You can use default
case for your problem.
switch(variable){
case: 0
//do something
break;
case: 1
//do something
break;
case: 3
//do something
break;
default:
// Here you can run your different action
break;
}
You have to also define the max value for your variable so that when your variable value reaches to that max value it stops incrementing the variable value.
Thanks Rahul. I am trying to get the code itself to know when the number 3 has been reached without me having to manually type into the default case of each switch statement.
– Stark
Nov 23 '18 at 5:48
add a comment |
I think you are working inside a method or cycle
In that case you can restart the variable when it reaches the last value inside your switch
.
.
.
switch(variable){
case 0:
//do something
case 1:
//do something
case 2: //this is the last
.
.
.
//do something
.
.
.
variable = 0;
}//End of switch
.
.
.
[EDIT]
You can restart the variable in the last case of your switch.
Thanks Mijael. I am trying to get the code itself to know when the number 3 has been reached without me having to type if(variable ==3).
– Stark
Nov 23 '18 at 5:47
1
@Stark, see the edit please.
– Mijael Viricochea
Nov 23 '18 at 23:12
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440878%2fandroid-count-number-of-cases-in-switch-statement-know-when-last-case%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use default
case for your problem.
switch(variable){
case: 0
//do something
break;
case: 1
//do something
break;
case: 3
//do something
break;
default:
// Here you can run your different action
break;
}
You have to also define the max value for your variable so that when your variable value reaches to that max value it stops incrementing the variable value.
Thanks Rahul. I am trying to get the code itself to know when the number 3 has been reached without me having to manually type into the default case of each switch statement.
– Stark
Nov 23 '18 at 5:48
add a comment |
You can use default
case for your problem.
switch(variable){
case: 0
//do something
break;
case: 1
//do something
break;
case: 3
//do something
break;
default:
// Here you can run your different action
break;
}
You have to also define the max value for your variable so that when your variable value reaches to that max value it stops incrementing the variable value.
Thanks Rahul. I am trying to get the code itself to know when the number 3 has been reached without me having to manually type into the default case of each switch statement.
– Stark
Nov 23 '18 at 5:48
add a comment |
You can use default
case for your problem.
switch(variable){
case: 0
//do something
break;
case: 1
//do something
break;
case: 3
//do something
break;
default:
// Here you can run your different action
break;
}
You have to also define the max value for your variable so that when your variable value reaches to that max value it stops incrementing the variable value.
You can use default
case for your problem.
switch(variable){
case: 0
//do something
break;
case: 1
//do something
break;
case: 3
//do something
break;
default:
// Here you can run your different action
break;
}
You have to also define the max value for your variable so that when your variable value reaches to that max value it stops incrementing the variable value.
answered Nov 23 '18 at 5:31
Rahul SharmaRahul Sharma
2,2691619
2,2691619
Thanks Rahul. I am trying to get the code itself to know when the number 3 has been reached without me having to manually type into the default case of each switch statement.
– Stark
Nov 23 '18 at 5:48
add a comment |
Thanks Rahul. I am trying to get the code itself to know when the number 3 has been reached without me having to manually type into the default case of each switch statement.
– Stark
Nov 23 '18 at 5:48
Thanks Rahul. I am trying to get the code itself to know when the number 3 has been reached without me having to manually type into the default case of each switch statement.
– Stark
Nov 23 '18 at 5:48
Thanks Rahul. I am trying to get the code itself to know when the number 3 has been reached without me having to manually type into the default case of each switch statement.
– Stark
Nov 23 '18 at 5:48
add a comment |
I think you are working inside a method or cycle
In that case you can restart the variable when it reaches the last value inside your switch
.
.
.
switch(variable){
case 0:
//do something
case 1:
//do something
case 2: //this is the last
.
.
.
//do something
.
.
.
variable = 0;
}//End of switch
.
.
.
[EDIT]
You can restart the variable in the last case of your switch.
Thanks Mijael. I am trying to get the code itself to know when the number 3 has been reached without me having to type if(variable ==3).
– Stark
Nov 23 '18 at 5:47
1
@Stark, see the edit please.
– Mijael Viricochea
Nov 23 '18 at 23:12
add a comment |
I think you are working inside a method or cycle
In that case you can restart the variable when it reaches the last value inside your switch
.
.
.
switch(variable){
case 0:
//do something
case 1:
//do something
case 2: //this is the last
.
.
.
//do something
.
.
.
variable = 0;
}//End of switch
.
.
.
[EDIT]
You can restart the variable in the last case of your switch.
Thanks Mijael. I am trying to get the code itself to know when the number 3 has been reached without me having to type if(variable ==3).
– Stark
Nov 23 '18 at 5:47
1
@Stark, see the edit please.
– Mijael Viricochea
Nov 23 '18 at 23:12
add a comment |
I think you are working inside a method or cycle
In that case you can restart the variable when it reaches the last value inside your switch
.
.
.
switch(variable){
case 0:
//do something
case 1:
//do something
case 2: //this is the last
.
.
.
//do something
.
.
.
variable = 0;
}//End of switch
.
.
.
[EDIT]
You can restart the variable in the last case of your switch.
I think you are working inside a method or cycle
In that case you can restart the variable when it reaches the last value inside your switch
.
.
.
switch(variable){
case 0:
//do something
case 1:
//do something
case 2: //this is the last
.
.
.
//do something
.
.
.
variable = 0;
}//End of switch
.
.
.
[EDIT]
You can restart the variable in the last case of your switch.
edited Nov 23 '18 at 23:10
answered Nov 23 '18 at 5:26
Mijael ViricocheaMijael Viricochea
5317
5317
Thanks Mijael. I am trying to get the code itself to know when the number 3 has been reached without me having to type if(variable ==3).
– Stark
Nov 23 '18 at 5:47
1
@Stark, see the edit please.
– Mijael Viricochea
Nov 23 '18 at 23:12
add a comment |
Thanks Mijael. I am trying to get the code itself to know when the number 3 has been reached without me having to type if(variable ==3).
– Stark
Nov 23 '18 at 5:47
1
@Stark, see the edit please.
– Mijael Viricochea
Nov 23 '18 at 23:12
Thanks Mijael. I am trying to get the code itself to know when the number 3 has been reached without me having to type if(variable ==3).
– Stark
Nov 23 '18 at 5:47
Thanks Mijael. I am trying to get the code itself to know when the number 3 has been reached without me having to type if(variable ==3).
– Stark
Nov 23 '18 at 5:47
1
1
@Stark, see the edit please.
– Mijael Viricochea
Nov 23 '18 at 23:12
@Stark, see the edit please.
– Mijael Viricochea
Nov 23 '18 at 23:12
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440878%2fandroid-count-number-of-cases-in-switch-statement-know-when-last-case%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
What do you mean with "last case"?
– tm13
Nov 23 '18 at 5:05
1
Can't you just add your action on the last case?
– josh_gom3z
Nov 23 '18 at 5:05
@tm13 when i say last case i mean, using the example above, case: 3. it could be another number in another switch statement.
– Stark
Nov 23 '18 at 5:12
@josh_gom3z in the last case a button will show that will offer the user the option to do something else. I could add it to the last case but I would have to go through each switch statement and there are a lot so I am trying to find a way to count the cases in a given switch statement in order to be able to tell when the last case for a particular switch statement has been reached.
– Stark
Nov 23 '18 at 5:15
1
@Stark could you share your code so that we have clear understanding about your problem? Actually, you can use default case
– tm13
Nov 23 '18 at 5:31