1214mondaytest Flashcards
What are the three main types of loops in java
for loop
while loop
do while loop
Indeterminate
Indeterminate loop situations are those situations where we do not know in advance how many times we may have to repeat (or iterate) the loop body code. (T!)
Type of loop favored for indeterminate situations:
while or do-while
determinate
loop situation we know ahead of time exactly how many iterations our loop body code has to iterate.
Type of loop favored for determinate situations:
Usually we’ll use either a for loop for these situations or we’ll use a counter-controlled while loop.
Which loop is a pre-test type of loop?
While loop, the loop continuation condition is check first before trying to run the loop body statements
How do we prevent an infinite loop?
add some code to the loop body that will eventually cause the loop continuation condition to become false
sentinel value
The sentinel value is a special input value that tests the condition within the while loop. When a sentinel value of zero is entered, the while loop continuation condition becomes false, and the loop terminates or ends.
Which type of loop is a post-test type?
do-while because it runs the code first and then the LCC is checked after the body code has run.
Difference between a while loop and a do while loop?
do while loop the loop body statements will always run at least once, because condition is checked after the loop body executes
Do-while loop is often used for
data validation
when is the for loop used?
when we know in advance how many times we want the loop to execute. commonly used for arrays
For(expression1; expression2; expression3)
Expression1 declares and initializes the the loop control variable or loop counter. Some texts call this the initialization action. This part of the loop header runs just once, at the start of the loop. Expression2 is the loopcontinuation condition expression. It is re-evaluated after each iteration of the loop
Expression3 is the loopcounter adjustment expression. (Some call it the action-after-each-iteration). It is the last action done in an iteration of the body code before the loop goes back to check the LCC again.
int count = 0; count < 5; count++) ; //semicolon here is error. What happens?
(Logic error)
This separates the loop header from the code in the loop body. The two parts then run independently of each other.
How do you get out of a loop if a certain condition is met?
with the break statement
What does the continue keyword do?
it will stop further execution of just the current iteration of a loop. (T!)
difference between continue and break?
continue stops only the current iteration of the loop
break is used to break out of the loop body completely, no matter how many iterations might remain
The for : each loop for(datatype: collection_Name)
Basically, it allows you to process all the data values stored in the storage structure without having to worry about how many elements there are in the structure ( this saves a few lines of code)It uses what is called an iterator (it’s like an indicator that points to each value in the data structure) to iterate through the values.It does have its usefulness, but it is actually a little less powerful than a standard for loop. In the declaration statement for(datatype: collection_Name) the datatype refers to the type of data that has been stored in the data structure that has ‘collection_Name’ as its identifier
Reading From A File Using a Scanner Object
If we want the Scanner object to read a file instead of the keyboard buffer, then we create it like this: // first we create a File object that has the path name of the file File myFile = new File(“DataFile_1.txt”); // then we create the Scanner and pass it the File object Scanner fileReader = new Scanner(myFile);
A path is:
A string argument that indicates the location of a folder (directory) or a file in your computer’s file system.Example: “C:\Temp2” is a path to a folder(directory) on drive ‘C’
Absolute path
any path string that begins with a drive letter or root prefix is known as an ABSOLUTE path
Relative path
relative to the current eclipse project directory which the java program is located
C:\MyEclipseWorkspace\FileExamplesProject\output.txt
COMMON ERROR:
putting the file you want to read in your ‘src’ source folder in your Eclipse project instead of in the project folder itself.
FileNotFoundException
File class object in Java is
Best described as an abstract representation of file and path names. (T)