LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


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

Rep: Reputation: 110Reputation: 110
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)

TIA
 
Old 08-11-2020, 01:08 PM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,042

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
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
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
Yes, set a test condition.
Code:
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:
Code:
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.
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:57 AM.

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