User Tools

Site Tools


notes:c4eng:fall2024:projects:stl2

STL2

loops

A “for” loop is a top driven loop that will test the condition of a statement before the loop begins to see if it is true. It is a control flow statement that is less “messy” than other types, as while it needs more conditions in order to run, it needs less input code in order to run. An example of a “for” loop is as follows:

for (index=0;index<10;index=index+1) {

fprintf (stdout,"%\n", index);

}

The looping variable is “index”, the loop starts when index=0, will continue to loop as the index increases by a value of 1 for each loop, and will stop/end when index=10, with the value stored in “index”.

for loop

A type of control flow statement that allows you to execute a desired task a specified number of times. The general form we see is as follows:

if(initialization; condition; update)

{task we wish to execute}

Initialization: Typically used to initialize a loop control variable (we used “index” in class). Condition: This part is evaluated at the start of each successive loop. If the condition is “true,” the loop is run and the code is executed. If the condition is “not true,” then the loop is terminated. Update: Executed after each iteration, this term is used to update the loop control variable>

while loop

A type of control flow statement that will continue to run whatever code is written in the body as long as the predetermined condition is met. For example, in this class, we see the while loops with the condition (1) which simply means “true.” This means that as long as the condition is true (1) the loop will continue to run until it is interrupted by the user. The format we see in this class is

while (1)

{task we wish to repeat here}

do-while loop

bitwise operations

bitwise AND

A bitwise AND is one of the multiple different bitwise functions that can be used not only to strengthen code but also to shorten code and make the code more succinct and more efficient. A bitwise AND is represented by the “&” symbol. A bitwise AND compares the two different values that it is given in order to create an answer. The bitwise AND compares the values to see which portions of each value are matching, because a bitwise AND will only output a 1 (AKA a success) if the two values are the same. For example if you were to bitwise AND the values 1010 and 0010, the response created would be 0010 because the second place of both of the values was the only place where both values had a 1. bitwise AND's are extremely useful for stl2 in order to allow for easier comparison between the current counter (or number) value that the program is on as well as the place calculator known as mask. The comparison of number and mask allows for our code to more easily tell what place we are currently in and whether that place needs to be on or off.

logical left shift

A bitwise operation that moves all bits in an operand to the left by a specific number of positions. What does this mean? In C, we use « to represent a left shift of one place. We could also do «# to left shift that specific number of places. In practice, this would shift the ones place to the twos place, the twos place to the fours place, the fours place to the eights place, and so on so fourth. In the context of this project, when used in a loop, this operation would allow us to perform sequential operations on a specific value of multiple bits by successively shifting the value one place to the left with each loop.

notes/c4eng/fall2024/projects/stl2.txt · Last modified: 2024/10/09 15:14 by dprado