Reputation: 529
I have a logical problem. I will provide pseudo code since the real code isn't all that readable. I want to only enter one of the instructions, and if none of the instructions are reachable I want the function to break out from the loop.
I'm pretty new to programming so any help is appreciated.
edit: In every iteration I want to only reach one of the conditions. Since there is a loop going on for a longer time I want to be able to reach the same / other instructions for every iteration
while(true){
if(condition){ // Instruction 1
do stuff
}else{
do stuff
}
if(condition){ // Instruction 2
do stuff
}else{
do stuff
}
if(condition){ // Instruction 3
do stuff
}else{
do stuff
}
if(condition){ // Instruction 4
do stuff
}else{
do stuff
}
if(none condition){
break;
}
}
Upvotes: 1
Views: 14287
Reputation: 16508
Alternatively you can check if it is possible to use switch Statements instead of if-else. Example
public static void main(String argv[]) {
String [] conditions = {"condition_1","condition_2","condition_3","condition_4","xyz"};
printConditionType(conditions);
}
public static void printConditionType(String [] conditions){
int i = 0;
while (i<conditions.length){
switch (conditions[i]) {
case "condition_1":
System.out.println("#1");
break;
case "condition_2":
System.out.println("#2");
break;
case "condition_3":
System.out.println("#3");
break;
case "condition_4":
System.out.println("#4");
break;
default:
System.out.println("Invalid condition:" + conditions[i]);
}
i++;
}
}
Upvotes: 0
Reputation: 44250
I don't think anyone's got this spot on so far so I'll throw in my understanding of what you're asking.
while(true) // keep looping
{
boolean instructionExecuted = false; // we haven't done an instruction this iteration
// Instruction 1
if(condition) {
instructionExecuted = true;
//do stuff
} else {
//do stuff
}
// Instruction 2
if(condition && !instructionExecuted) { // If we've not done an instruction,
// and we can do instruction #2
instructionExecuted = true;
//do stuff
} else if (!instructionExecuted) { // only do this if we haven't already
// done an instruction
//do stuff
}
// Instruction 3
if(condition && !instructionExecuted) { // If we've not done an instruction,
instructionExecuted = true; // and we can do instruction #3
//do stuff
} else if (!instructionExecuted) { // only do this if we haven't already
// done an instruction
//do stuff
}
//etc.
if(none condition)
{
break;
}
}
Upvotes: 2
Reputation: 9
boolean reachable = false;
while(true){
if(condition){ // Instruction 1
do stuff
reachable = true
}else{
do stuff
}
if(condition){ // Instruction 2
do stuff
reachable = true
}else{
do stuff
}
if(condition){ // Instruction 3
do stuff
reachable = true
}else{
do stuff
}
if(condition){ // Instruction 4
do stuff
reachable = true
}else{
do stuff
}
if(!reachable){
break;
}
}
Upvotes: 0
Reputation: 28703
well, there are multiple ways to accomplish what you want:
doNotBreak
and set true in if/else blocks execution of which means the loop should not break.if(condition1 && !condition2 && ..) break;
continue
.Upvotes: 0
Reputation: 192
You're making this harder than it has to be. All you need to do is have an if statement with multiple else if statements after it.
Example:
if (condition) {
doStuff();
} else if (condition) {
doStuff();
} else if (condition) {
...
} else {
break;
}
Upvotes: 0
Reputation: 5
Adding a bool trigger might be what you are looking for. if you enter any if statements turn the trigger to true.
Upvotes: 0