You are on page 1of 3

# Alip, Yahmani M.

BSCS-1

## Chapter 5: Making Decisions

Usually, you can compare only values that are of the same type. You can ask every programming question by using one of only three types of comparison operators in a Boolean expression. For any values that are of the same type, you can decide whether the two values are equal, the first value is greater than the second value or the first value is less than the second value. Each programming language is supports its own set of logical comparison operators, or comparison symbols, that express these Boolean tests. One of the basic structures of structured programming is called the selection structure (sometimes called a decision structure). A single-alternative or unary selection is action required for only one outcome of the question while on the other hand, a Boolean expression is one that represents only one of two states, usually expressed as true or false. Every decision you make in a computer program involves evaluating a Boolean expression. Often, you need more than one selection structure to determine whether an action should take place. A compound, or AND decision requires a nested decision or a nested if. That is a decision inside of another decision. When you nest decisions because the resulting action requires that two conditions be true, you must decide which of the two decisions to make first. Logically, either selection in an AND decision can come first. However, when there are two selections, you often can improve your programs performance by making an appropriate choice as to which selection to make first. When you must satisfy two or more criteria to initiate an event in a program, you must make sure that the second decision is made entirely within the first decision. Beginning programmers often make another type of error when they must make two comparisons on the same field while using a logical AND operator. Sometimes, you want to take action when one or the other of two conditions is true. It is called an OR decision because either one condition must be met or some other condition must be met in order for an event to take place. You can write a program that creates a report containing all employees who have either the medical or dental insurance by using the main Loop (). Business programs often need to make selections based on a variable falling within a range of values. Most languages allow you to use the algebraic signs for greater than > and for less than < to make corresponding comparisons. Additionally, COBOL, which is very similar to English, allows you to spell out the comparisons in expressions. Any logical situation can be expressed using just three types of comparisons: equal, greater than, and less than. Comparing two amounts to decide if they are not equal to each other is the most confusing of all the comparisons. Using not equal to in decisions involves thinking in double negatives, which makes you prone to include logical errors in your program.

Alip, Yahmani M.

BSCS-1

Chapter 6: Looping

There are three steps that occur in every loop. First, you must initialize a variable that will control the loop. Second, you must compare the variable to some value that controls whether the loop continues or stops. Lastly, you must compare the variable to some value that controls whether the loop continues or stops. Thus, you alter rep by adding 1 to it. On each pass through the loop, the value in the rep variable determines whether the loop will continue. Therefore, variables like rep are known as loop control variables. To stop a loop, you compare the loop control value to a sentinel value. The statements that execute within a loop are known as the loop body. Looping is the structure that repeats actions while some condition continues, within a computer program. You can write one set of instructions that operates on multiple separate sets of data. The main loop is a typical loop. Within it, you write one set of instructions that executes repeatedly while records continue to be read from an input file. Loops also appear in sub routines. A counter is any numeric variable you use to count the number of times an event has occurred. Adding 1 to a variable is called incrementing the variable. Rather than incrementing a loop control variable until it passes some sentinel value, sometimes it is more convenient to reduce a loop control variable on every cycle through a loop. Decreasing a variable by one is called decrementing the variable. A different sort of error occurs if you remove the statement that adds 1 to the label Counter from the program. You can use the For statement, or for loop, with definite loopsthose for which you know how many times the loop will repeat. When you use either a while or a for loop, the body of the loop may never execute. When you want to ensure that a loops body executes at least one time, you can use a do until loop. In a do until loop, the loop control variable is evaluated after the loop body executes instead of before. Therefore, the body always executes at least one time. Programmers must be careful to use the correct comparison in the statement that controls a loop. Because you cant determine ahead of time how many records there might be and, therefore, how many times the loop might execute, the mainline loop in the label-making program is called an indeterminate or indefinite loop. With some loops, you know exactly how many times they will execute. This kind of loop, in which you definitely know the repetition factor, is a definite loop. Every high-level computer programming language contains a while statement that you can use to code any loop, including indefinite loops (like the mainline loop) and definite loops (like the label-printing loop).

Alip, Yahmani M.

BSCS-1

Chapter 7: Arrays

Arrays make code easier to read. Declaring an array requires the type, the name, and the number of elements. If all elements in an array will have the same initial value, use a loop. Providing values for an array is called populating the array. Uninitialized array: does not receive values for the elements until run time. Initialized array: values for the elements are hard-coded by the programmer. An array can be thought of as a table. Array: a series or list of variables in memory that have the same name and type but different subscripts (or indexes). Subscript (or index): a number that indicates the position of an item within the array. Element: one item in the array; has a common name and a unique subscript, and stores a value. Size of the array: the number of elements in the array. An arrays elements are stored contiguously in memory. Using an array can greatly reduce the amount of code required. Subscript can be represented by a constant or a variable. Using a variable for the subscript allows the use of a loop to cycle through all elements in an array. A subscript can have an illegal name, must be numeric with no decimal places, and must be initialized to 0, must be incremented by 1 each time the logic passes through a loop. When using a loop to search an array, exit the loop if the item is found in an array element. Early Exit: leaving a loop when a match is found. Instead of hard-coding the array size, use a named constant or variable. Advantage: only the variable or constant needs to be changed to change the array size. Named constant identifiers should be written in all caps. Range match: finding a value within a contiguous range of values. Hard-coded values are inconvenient only the programmer can change them. Initializing an array by reading values from a file makes the program more flexible. Use a loop to search through an array, element by element. The loop control variable can be used as the array subscript variable. Flag: a variable used to indicate if an event has occurred. Start with flag = No; set it to Yes if you find the desired value in an array element. Parallel arrays: two arrays in which each element in one array is associated with the element in the same relative position in the other array.