I want to check every entry inside a table with an if statement and a for loop and do sth only once if it's true. eg:
int[][] table = new int[5][5]
int number = 4;
for(int i=0;i<table.length;i++)
{
if(table[i][0] != number)
{
do something;
}
}
So it checks for the first column if an entry equals number and if it does not it executes the "do something". But I only want to execute "do something" when the entire column does not equal number only once and not for each entry.
Solved it by using a counter that always increases when
if(table[i][0] != number)
is true and if the counter equals 0 I execute the "do something".
you can simply break a loop.
loop_name: for(int i=0;i<table.length;i++) {
if(table[i][0] != number)
{
do something;
break loop_name;
}
}
and after first time that condition become true, 'do something' and break the loop;
try this
int[][] table = new int[5][5]
int number = 4;
// use this flag to signal the condition violation
boolean flag = false;
for(int i=0;i<table.length;i++)
{
if(table[i][0] == number)
{
flag == true;
break;
}
}
if(!flag) doSomething();
You could also try
do {do_stuff; table[i][0] = number;} while (table[i][0] != number);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.