From Gates to Apps: An Introduction to Computer Science by Edward Amoroso & Matthew Amoroso

From Gates to Apps: An Introduction to Computer Science by Edward Amoroso & Matthew Amoroso

Author:Edward Amoroso & Matthew Amoroso
Language: eng
Format: azw3
Published: 2017-07-13T07:00:00+00:00


Example: Use a for-loop to write a small snippet of code to zero out an integer array A with length ten.

Answer: The code snippet is as follows:

for index = 1 to 10 do

A[index] = 0

od

A common way to use for-loops is to examine strings. If you recall, strings are groups of individual characters that make up meaningful words and phrases to humans, but not to computers. Using for-loops to examine each individual character of a string can give the string meaning to the computer.

For example, a website that requires certain criteria for passwords, (at least one letter, symbol, number, etc.) may utilize for-loops to do this. Like our previous examples of for-loops, this code would execute commands as many times as a certain index tells it to. In the above code, this index is an array with length 10, but in a password analyzing for-loop the index would be the actual password, or string.

Another loop construct is called a while-loop. This works in a similar manner, but rather than counting out an index in advance, the while-loop tests some condition at the end of the loop to determine whether to continue, or repeat another round of the loop. Here’s an example:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.