COMP 248 L 4

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What are the three things you need to pay attention to when making a loop ?

1. that you condition is reachable 2. the value that you are evaluating needs to change throughout the loop 3. the variable you are using needs to be defined initially

WHat are the three diffrent loops ?

1. while loop 2. do while loop 3. for loop

What is the difference between the exit statement and the break ?

A break Will end a loop or switch statement, but will not end the program --> it's not ending a program its just ending the execution of a particular statement The exit statement i.e. the System.exit(0); will terminate the program at that point THAT MEANS the statament AFTWR the System.exit(0); statements WILL NOT BE EXECUTED !

What is the difference between the do-while loop and the while loop ?

A do while loop is similar to a while loop BUT the condition in a do while loop is evaluated after the body of the loop is executed. Therefore, the body of a do while loop will execute at least once.

What is another name for a for loop?

Aka a counter contorl loop

What is an array ?

An ordered list of elements of the same type An array is of size n from 0 to n-1 The elements of an array can be primitive type of an object referene

What is the difference in application of the while loop, do-while loop and the for-loop?

Any loop can be rewritten using any of the other loops HOWEVER, You want to use a while loop or a do while loop when you don't know how many times you want to execute the loop body. For example if your loop is using user input like the example where we were validating user input so in these EVENT controlled loops or USER controlled loops it is recommended to use a while or a do while loop determining when to use a while loop and a do while loop —> remember ther difference between the two The while loop, minimum times it will loop is ZERO A do while loop will execute a minimum of 1 TIMES So in the case where you're going to have user input you want to use a do-while loop because if you want to validate the user in put , you need that user input at least once a for-loop is also known as a counter control loop i.e. you are incrementing your value, or decrementing your value or modifying your value that is being tested in the condition When you look at a for loop, you willl know how many times that loop is going to be executed i.e. you will know the number of iterations of that loop Nothing says you can't use a while loop for a counter control oop (for-loop) but the above is the idea for what these loops were designed for

What is important to remember about nested loops ?

Every iteration of the outer loop will go through all the iterations of the inner loop before going back to the next iteration of the outer loop !

What is a loop?

Is a repetition statement that allows us to execute a statement several times.

What if I want to initialise an array to value other than zero ?

Need to make an intialiser list

What is the minimum amount of times that a do while loop will execute ?

Once! Remember the do while will go through the first statement always the while, if the condition is not met initially, it will skip the while and it will never be executed

If you want more than one statement to be executed in a while loop what do you need to do ?

Put in between {}

What is a continue statement do ?

Stop executing the body of that loop, assume that you've finsihed the current interation and do what you normally would do when you finsih the current iteration which in the attached case is to go back and test the Condition

What do you need to remember about a for loop?

The incrementation in a for loop is done BEFORE the condition is tested

The size of an array needs to be a what ?

The size of an integer , you can;t have half a room

What is an interesting fact about the initialisation of arrays?

They get initialised to 0 of its type !

Again, what is the difference between a while and a do while statement ?

While loop will evaluate the condition first and if the condition is met will move on to the statement (Pre test loop) a do while loop will look at the statement first and then look at the ocndition after (Post Test loop)

What is a break statement?

Will exit the inner most loop without evaluating the condition and will go to the next staement outside the loop

What is the continue statement?

Will interrupt the current iteration of the inner most loop will force a new evaluation of the condition for a possible new iteration

What is interesting about while loops and for loops

You can write an equivalent while loop as a for loop

What is important to note about the do while statements ?

after the condition in the do while statement there is a semi colon that is not present in the regular while statement this is how the system differentiates them

Even though we can ommit portions of a for loop, what do we always need to include

the 2 semi colones !!

What is the purpose of the break and the continue statements?

these statement are used to bypass the normal flow of control loops.

in a for loop can we initialize multiple expression ?

yes we can we need to sperate the variables with a coma dont forget that we need a semi colone for all of the 4 parts of the for loop

What is the minimum amount of times a while loop will be executed ?

zero if the starting limit is above the while statement see slide 12 L4


संबंधित स्टडी सेट्स

Unit 9 Respiratory Prep U Questions

View Set

Chapter 22: Descent with Modification: A Darwinian View of Life

View Set

STS - Chapter 1 (Historical Antecedents in the Course of Science and Technology)

View Set

MKT/421T: Marketing Wk 4 - Practice: Ch. 13, Building the Price Foundation [due Day 5]

View Set

Income Measurement & Asset Valuation Exam 1

View Set

Unit 1a Homework Transistor Amplifier Theory

View Set

WSU Life Span Development: Final

View Set