In the following code :
boolean condition=false;
do{
condition=expression;
if(condition)
{
// subprogram 2
}
// subprogram 1
}while(condition1);
I want to execute subprogram 2 only once (when "condition" changes from false to true, it will remain true afterwards) while the do-while loop keeps advancing.
Is there a way to do it ?
bool ifStatementRan = false;
do{
condition=expression;
if(condition && !ifStatementRan )
{
ifStatementRan = true;
// subprogram 2
}
// subprogram 1
}while(condition1);
You will probably want to use a more descriptive variable name than "ifStatementRan" to describe what the if statement is doing.
You can use a done flag for subprogram 2.
boolean condition=false;
boolean subProgram2Done = false;
do{
condition=expression;
if(condition && subProgram2Done == false )
{
// subprogram 2
subProgram2Done = true;
}
// subprogram 1
}while(condition);
boolean JustFirstTime=true;
int x = 0;
while(x < 10){
if(JustFirstTime)
{
// subprogram 2
JustFirstTime= false;
}
// subprogram 1
x++;
};
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.