Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-11-2020, 01:04 PM   #1
Senior Member
Registered: Aug 2005
Location: Brazil
Distribution: Debian Stretch
Posts: 1,024

Rep: Reputation: 82
How to run something only once from within a loop?

I need to run a loop many times, possibly hundreds. Sometime along that, one certain value may change. The loop keeps running. How do I make sure that value won't be changed again?

For example:
x = 90;
y = 5;
if x > 100 then y = y * 3;

(now y = 15 and should never change again although x may keep increasing)

Old 08-11-2020, 01:08 PM   #2
LQ Guru
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 15,097

Rep: Reputation: 4972Reputation: 4972Reputation: 4972Reputation: 4972Reputation: 4972Reputation: 4972Reputation: 4972Reputation: 4972Reputation: 4972Reputation: 4972Reputation: 4972
you might want to use a variable named flag:
if flag is on (or true or enabled or whatever you like) you will modify y and set flag to off/false/disabled.
Next time before you start the loop you need to set the flag again.
1 members found this post helpful.
Old 08-11-2020, 01:24 PM   #3
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,907
Blog Entries: 13

Rep: Reputation: 4040Reputation: 4040Reputation: 4040Reputation: 4040Reputation: 4040Reputation: 4040Reputation: 4040Reputation: 4040Reputation: 4040Reputation: 4040Reputation: 4040
Yes, set a test condition.
if ((y != 15) && (x > 100)) {
    y *= 3;
At least insofar as the details asked. For other circumstances, you can adjust the tests, etc.

Or you could also have it such that if x == 100 or x == 101 you take action to multiply y at that time, but it all depends on whether or not x will always be at a specific value, or instead if also it can hit that value repeatedly.

As pan64 is saying, just using a flag:
int done = 0;
loop start

if ((x > 100) && (done == 0) {
    y *= 3;
    done = 1;
Perhaps more details, unless these rudimentary thoughts are enough to solve it for you.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
question: 'onclick' within 'onmouseover' within 'form' within 'table' - how is it possible? rblampain Programming 4 04-25-2017 08:49 PM
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM
Strange behavior: string within scope return 0 if not within a while loop. RHLinuxGUY Programming 2 08-05-2006 11:05 PM
Knoppix booted once, and only once... abowling Debian 7 02-29-2004 09:31 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:33 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration