While loop flow diagram
Loop constructs |
---|
Do while loop |
While loop |
For loop |
Foreach loop |
Infinite loop |
Control flow |
In most computer programming languages, a while loop is a control flowstatement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement.
- 3Demonstrating while loops
- 3.7C or C++
Overview[edit]
The while construct consists of a block of code and a condition/expression.[1] The condition/expression is evaluated, and if the condition/expression is true,[1] the code within the block is executed. This repeats until the condition/expression becomes false. Because the while loop checks the condition/expression before the block is executed, the control structure is often also known as a pre-test loop. Compare this with the do while loop, which tests the condition/expression after the loop has executed.
For example, in the C programming language (as well as Java, C#,[2]Objective-C, and C++, which use the same syntax in this case), the code fragment
first checks whether x is less than 5, which it is, so then the {loop body} is entered, where the printf function is run and x is incremented by 1. After completing all the statements in the loop body, the condition, (x < 5), is checked again, and the loop is executed again, this process repeating until the variable x has the value 5.
Note that it is possible, and in some cases desirable, for the condition to always evaluate to true, creating an infinite loop. When such a loop is created intentionally, there is usually another control structure (such as a break statement) that controls termination of the loop.For example:
Equivalent constructs[edit]
In the C programming language,
is equivalent to
or
or
or
Buku online free. Those last two are not recommended because the use of 'goto' statements makes it hard for a programmer to understand the flow of control, and is generally regarded as a last resort.
Also, in C and its descendants, a while loop is a for loop with no initialization or counting expressions, i.e.,
In addition, it has been shown that a loop with an inner for loop performs more computations per unit time than a loop without it or one with an inner while loop. This means the nested while loop will finish slower, given the same number of computations to perform, compared to the nested for loop.[3]
Demonstrating while loops[edit]
These while loops will calculate the factorial of the number 5:
ActionScript 3[edit]
Ada[edit]
The Wikibook Ada_Programming has a page on the topic of: Control |
AutoHotkey[edit]
Microsoft Small Basic[edit]
Visual Basic[edit]
Bourne (Unix) shell[edit]
C or C++[edit]
Script syntax[edit]
Tag syntax[edit]
Fortran[edit]
Java, C#, D[edit]
The code for the loop is the same for Java, C# and D:
JavaScript[edit]
Lua[edit]
MATLAB[edit]
Mathematica[edit]
Oberon, Oberon-2 (programming language), Oberon-07, or Component Pascal[edit]
Maya Embedded Language[edit]
Pascal[edit]
Perl[edit]
While loops are frequently used for reading data line by line (as defined by the
$/
line separator) from open filehandles:PHP[edit]
PL/I[edit]
Python[edit]
Non-terminating while loop:
Racket[edit]
In Racket, as in other Scheme implementations, a named-let is a popular way to implement loops:
Using a macro system, implementing a while loop is a trivial exercise (commonly used to introduce macros):
But note that an imperative programming style is often discouraged in Racket (as in Scheme).
Ruby[edit]
Smalltalk[edit]
Contrary to other languages, in Smalltalk a while loop is not a language construct but defined in the class
BlockClosure
as a method with one parameter, the body as a closure, using self as the condition.Smalltalk also has a corresponding whileFalse: method.
Swift[edit]
Tcl[edit]
VEX[edit]
Windows PowerShell[edit]
While programming language[edit]
The While programming language[4] is a simple programming language constructed from assignments, sequential composition, conditionals and while statements, used in the theoretical analysis of imperative programming language semantics.[5][6]
See also[edit]
- LOOP (programming language) – a programming language with the property that the functions it can compute are exactly the primitive recursive functions
References[edit]
- ^ ab'The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)'. Dosc.oracle.com. Retrieved 2016-10-21.
- ^'while (C# reference)'. Msdn.microsoft.com. Retrieved 2016-10-21.
- ^Inner loop program construct: A faster way for program execution
- ^'Chapter 3 : The While programming language'(PDF). Profs.sci.univr.it. Retrieved 2016-10-21.
- ^Flemming Nielson; Hanne R. Nielson; Chris Hankin (1999). Principles of Program Analysis. Springer. ISBN978-3-540-65410-0. Retrieved 29 May 2013.
- ^Illingworth, Valerie (11 December 1997). Dictionary of Computing. Oxford Paperback Reference (4th ed.). Oxford University Press. ISBN9780192800466.
Retrieved from 'https://en.wikipedia.org/w/index.php?title=While_loop&oldid=900241094'
Froot Loopsunknown
Guy 1: hey, let's go get some fruit loops
Guy 2: it's FROOT loops you asshat
Guy 2: it's FROOT loops you asshat
Get a Froot Loops mug for your Uncle Bob.
FROOTLOOPSunknown
either of 2 definitions.
1-pills u take and don't know what they are
2-coricidin pills
1-pills u take and don't know what they are
2-coricidin pills
1-she went to that party and took some frootloops, and now she's all fucked up
2-i just stole some frootloops from the drug store.
2-i just stole some frootloops from the drug store.
Get a FROOTLOOPS mug for your dad Paul.
Froot Loopunknown
Derived from the slang term froot, usually meaning someone who is gay.
by fsc May 31, 2005
Get a Froot Loop mug for your girlfriend Nathalie.
froot loopunknown
You are acomplete froot loop
Get a froot loop mug for your sister-in-law Zora.
Froot Loopunknown
Sometimes means gay.
Main definition is when a person is an outlander to this world and does not belong here
©2010
Main definition is when a person is an outlander to this world and does not belong here
©2010
Man 1: Hey, did you see that idiotdown there?
Man 2: Wow, what a Froot Loop
Man 2: Wow, what a Froot Loop
Get a Froot Loop mug for your father Callisto.
Froot Loopunknown
NOT 'Fruit Loop', the correct spelling is Froot Loop. Froot Loops are sugary, highly-toxic and so-called nutritious cereal. They are not healthy at all seeing that they contain a lot of sugar, but, well, they are delicious. They were created by Kellogg's and the character in the box is Toucan Sam
'Mom, I told you I hate Cornflakes! I want Froot Loops!
Get a Froot Loop mug for your father-in-law Callisto.
Frootloopunknown
A person that is a wimp and runs away from a fight.
by STOOOOOOOOOOOOOOOpiddd March 13, 2015
Get a Frootloop mug for your mate Helena.
Trending RN - July 07, 2019
- 1. speed bumping
- 2. SPEED BUMP
- 3. Egyptian PT
- 4. 69420
- 5. kelly oubre
- 6. Joergen I.
- 7. k10
- 8. frist
- 9. bumpf
- 10. Fish-mouth Syndrome
- 11. green flag
- 12. off your face
- 13. not like this
- 14. Joergen
- 15. pink clouding
- 16. gumdrop
- 17. 617
- 18. Pavloved
- 19. Hot girls summer
- 20. gmfu
- 21. hammie
- 22. AWP
- 23. bulljive
- 24. Dwight Howard
- 25. Sheenis
- 26. catch the vapors
- 27. cup check
- 28. Culver's
- 29. Nutsock
- 30. 69 and 420
Loop command: against the results of another command.
FOR /F processing of a command consists of reading the output from the command one line at a time and then breaking the line up into individual items of data or 'tokens'. The DO command is then executed with the parameter(s) set to the token(s) found.
The FOR command is the answer to innumerable questions where you want to take the output of some command, store it in a variable (%%G) then do something with the result.
The FOR command is the answer to innumerable questions where you want to take the output of some command, store it in a variable (%%G) then do something with the result.
For example the PING command returns serveral lines including one like:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
To select that one line of output, you can search for the text 'loss' (which is always present), then use the Tokens parameter to select the number of lost packets, here this is 0 but it will vary each time you run the command.
set _ping_cmd=ping -n 5 127.0.0.1
FOR /f 'tokens=4 delims=(=' %%G IN ('%_ping_cmd% ^|find 'loss') DO echo Result is [%%G]
The tricky part is always splitting up the line of interest into the right tokens, in this case I'm splitting on the characters '=' and '('
these two characters split the line into 5 chunks of text and we pull out the fourth one with 'tokens=4'
these two characters split the line into 5 chunks of text and we pull out the fourth one with 'tokens=4'
By default, /F breaks up the command output at each blank space, and any blank lines are skipped.
You can override this default parsing behavior by specifying the 'options' parameter. The options must be contained within 'quotes'
You can override this default parsing behavior by specifying the 'options' parameter. The options must be contained within 'quotes'
It is possible to use the syntax on this page to parse a text file with TYPE('Type somefile.txt') but you will get much better performance using FOR /F File contents(somefile.txt) as that will save loading TYPE.EXE.
The precedence/priority of FOR command options is: usebackq > skip > delims > eol > tokens
usebackq
This option is useful when dealing with a command that already contains one or more straight quotes.
The backquote character ` is just below the ESC key on most keyboards. See the FOR /F page for other effects of usebackq.
The backquote character ` is just below the ESC key on most keyboards. See the FOR /F page for other effects of usebackq.
Usebackq can be abbreviated to useback (undocumented.)
Skip
SKIP will skip processing a number of lines from the beginning of the file.
SKIP includes empty lines, but after the SKIP is complete, FOR /F ignores (does not iterate) empty lines.
SKIP includes empty lines, but after the SKIP is complete, FOR /F ignores (does not iterate) empty lines.
eol
The default end-of-line character is a semicolon ';' when the FOR command reads a text file (or even a character string), any line that STARTS with the eol character will be ignored. In other words it is treated as a comment.
Use eol=X to change the eol character to X.
Use eol=X to change the eol character to X.
Often you will want to turn this feature off so that every line of your data file is processed, in theory 'eol=' should turn this feature off, but in practice this fails to work correctly - it will set eol to whatever the next character is, often the quote or space character. One workaround is to set eol to some unusual character that you don’t expect to ever encounter in the data file e.g. 'eol=€' or 'eol=¬'. Another method is to escape every delimiter For /f tokens^=*^ delims^=^ eol^= %%a in (file.txt) do.. (see forum for a discussion of this)
Delims
More than one delimiter can be specified so a string like 'abcd+efg+hijk;lmno;pqr' can be broken up using 'delims=;+'.
You can use any character as a delimiter, but they are case sensitive.
If you don’t specify delims it will default to 'delims=<tab><space>'
n.b. some text editors will enter the TAB character as a series of spaces, specifying more than one delimiter has been known to cause problems with some data sets.
If you don’t specify delims it will default to 'delims=<tab><space>'
n.b. some text editors will enter the TAB character as a series of spaces, specifying more than one delimiter has been known to cause problems with some data sets.
Tokens
tokens=2,4,6 will cause the second, fourth and sixth items on each line to be processed
tokens=2-6 will cause the second, third, fourth, fifth and sixth items on each line to be processed
tokens=* will cause all items on each line to be processed
tokens=3* will process the third token and the 4th + all subsequent items, this can also be written as tokens=3,*
Each token specified will cause a corresponding parameter letter to be allocated. The letters used for tokens are case sensitive.
If the last character in the tokens= string is an asterisk, then additional parameters are allocated for all the remaining text on the line.
tokens=2-6 will cause the second, third, fourth, fifth and sixth items on each line to be processed
tokens=* will cause all items on each line to be processed
tokens=3* will process the third token and the 4th + all subsequent items, this can also be written as tokens=3,*
Each token specified will cause a corresponding parameter letter to be allocated. The letters used for tokens are case sensitive.
If the last character in the tokens= string is an asterisk, then additional parameters are allocated for all the remaining text on the line.
The following ASCII characters can be used as FOR tokens:
ASCII 63 - 93 inclusive, 31 tokens: ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ ]
ASCII 95-123 inclusive, 29 tokens: _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z {
(there are a few other characters that can be used, but require escaping)
ASCII 63 - 93 inclusive, 31 tokens: ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ ]
ASCII 95-123 inclusive, 29 tokens: _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z {
(there are a few other characters that can be used, but require escaping)
A single FOR /F command can never parse more than 31 tokens, to use more requires a workaround with multiple FOR commands.
The numbers specified in tokens= are automatically sorted, so for example tokens=5,7,1-3 and tokens=1,2,3,5,7 both produce the same result.
Matching the same token more than once (tokens=1,2,1) can give unpredictable results. Token variables can of course be used multiple times: Echo %%G %%H %%G
FOR tokens variables (or parameter names) are global, so in complex scripts which call one FOR statement from within another FOR statement you can refer to both sets of parameters.
FOR tokens variables (or parameter names) are global, so in complex scripts which call one FOR statement from within another FOR statement you can refer to both sets of parameters.
command
This can be almost any internal or external command.
A common pattern is to set a variable = some token output to make it available for later processing/display. When doing this be sure to read the section Using variables within a FOR loop on the main FOR page.
Also consider the case where the command fails, the FOR command will trap the error, but the DO clause is never reached - this means the variable will not be changed. This can cause subtle bugs. When using the FOR command in a batch file always use SETLOCAL at the start to localise all variables. Alternatively you can explicitly clear the variable(s) to remove any existing value, just before running the FOR loop.
command_to_process
The command_to_process can be almost any internal or external command, but there are a few commands that have no effect when are called through FOR /F - SHIFT, SETLOCAL, ENDLOCAL, CALL:Subroutine.
To be clear these can be used as part of command, but not command_to_process
To be clear these can be used as part of command, but not command_to_process
Examples:
To ECHO from the command line, the name of every environment variable.
To ECHO from the command line, the name of every environment variable.
The same command with usebackq:
To put the Windows Version into an environment variable
List all the text files in a folder:
The 'tokens=*' has been added to match all parts of any long filenames returned by the DIR command.
List all the text files in a folder, including the full path:
We use DIR /S (even if the folder has no subfolders) to make DIR return the full path to each file.
In the example above the long filename has to be surrounded in 'quotes' and those quotes have to be escaped using ^
Although the above is a trivial example, being able to set a variable equal to each long filename in turn allows much more complex processing to be done.
In the example above the long filename has to be surrounded in 'quotes' and those quotes have to be escaped using ^
Although the above is a trivial example, being able to set a variable equal to each long filename in turn allows much more complex processing to be done.
More examples can be found on the Syntax / Batch Files pages and the other FOR pages below.
FOR is an internal command.
“History never repeats itself, Mankind always does” - Voltaire
Related:
FOR - Summary of FOR Loop commands
FOR - Loop through a set of files in one folder
FOR /R - Loop through files (recurse subfolders)
FOR /D - Loop through several folders
FOR /L - Loop through a range of numbers
FOR /F - Loop through items in a text file
SETLOCAL - Control the visibility of variables inside a FOR loop
FORFILES - Batch process multiple files
GOTO - Direct a batch program to jump to a labelled line
IF - Conditionally perform a command
Powershell: ForEach-Object - Loop for each object in the pipeline
Equivalent bash command (Linux): for - Expand words, and execute commands
Related:
FOR - Summary of FOR Loop commands
FOR - Loop through a set of files in one folder
FOR /R - Loop through files (recurse subfolders)
FOR /D - Loop through several folders
FOR /L - Loop through a range of numbers
FOR /F - Loop through items in a text file
SETLOCAL - Control the visibility of variables inside a FOR loop
FORFILES - Batch process multiple files
GOTO - Direct a batch program to jump to a labelled line
IF - Conditionally perform a command
Powershell: ForEach-Object - Loop for each object in the pipeline
Equivalent bash command (Linux): for - Expand words, and execute commands
Copyright © SS64.com 1999-2019
Some rights reserved
Some rights reserved
I think I'm missing something because I can't seem to find what this means.
Example: for /D %%A in (*) do '7za.exe' u -t7z -m9=LZMA2 '%%A.7z' '%%A'
That line was supposed to use a command line version of 7zip to compress individual folders, but I'm stumped as to what %%A means in this context.
Jim Kieger
Jim KiegerJim Kieger
3 Answers
The
for
command needs a placeholder so you can pass along variables for use later in the query, we are telling it use the placeholder %A
, the reason the code you saw uses %%A
is because inside a batch file (which I assume is where you found this) the %
has a special meaning, so you must do it twice %%
so it gets turned in to a single %
to be passed to the for
commandTo actually break apart what the command is doing, there is two parts to the command:
What this part says is for every folder in the current folder execute the following command replacing
%%A
with the name of the currently processing folder.What this part says is execute the command
'7za.exe' u -t7z -m9=LZMA2 '%%A.7z' '%%A'
and replace the two %%A
's with the current record we are processing.Fruit Loop Clifton Park
DavidPostill♦112k2727 gold badges247247 silver badges282282 bronze badges
Scott ChamberlainScott Chamberlain28.3k55 gold badges8484 silver badges102102 bronze badges
It's a variable.
That particular example uses the directory option of a FOR loop, iterating through the directories and assigning them to %%A.
What Does Flops Mean In Chat
That's also not a command-line example, but a batch file example. In batch files, you need to use %%A, while on the command-line, you'd just use %A.
ernieernie5,65322 gold badges2121 silver badges2727 bronze badges
In your scenario, the %%A is a placeholder for what the 'for' loop is iterating over (which the /D indicates directories). So each iteration of the loop, %%A is one of the directories.
You'll see %% instead of % in batch code. You'll see % instead of %% used in your command prompt.
So know that if you copy over a batch file code into a command prompt and run it with %% being used, it will error, and vice versa.
MechaflashMechaflash71644 gold badges1010 silver badges2323 bronze badges