LOOPS AS A CONCEPT WHAT ARE LOOPS Loops

  • Slides: 20
Download presentation
LOOPS AS A CONCEPT

LOOPS AS A CONCEPT

WHAT ARE LOOPS? Loops are repetitions of lines of code You are telling the

WHAT ARE LOOPS? Loops are repetitions of lines of code You are telling the program to do something until certain condition is reached Imagine having to execute the same set of codes 100 times. If you don’t know how to use loops, you would have to copy paste the code 100 times. Not very effective, hmm?

TYPES OF LOOPS While loop Do while loop Foreach loop For loop Continue and

TYPES OF LOOPS While loop Do while loop Foreach loop For loop Continue and break keywords

WHILE LOOP A loop which executes as long as certain condition evaluates to true

WHILE LOOP A loop which executes as long as certain condition evaluates to true Tip that might help you remember : A while(true) loop means an infinite loop , this will mean that when a loop’s condition reaches false, it stops A type of top-test loop One of the simpler loops Typically used when the number of executions is not known When size is involved, always try to use for loop to simplify code

SYNTAX FOR WHILE LOOP while(condition){ //statements to execute here } condition can be anything

SYNTAX FOR WHILE LOOP while(condition){ //statements to execute here } condition can be anything that would represent a boolean value Tip : when thinking of condition to put, think about what would you put in a if statement or a boolean variable

WHILE STATEMENT Example: Find the Fibonacci number of a positive number. A Fibonacci number

WHILE STATEMENT Example: Find the Fibonacci number of a positive number. A Fibonacci number is a member of the Fibonacci series, which includes all members that are the sum of the previous two numbers in the series beginning with 1 & 1, i. e. 1, 1, 2, 3, 5, 8, 13, 21, 34 …. . 6

SYNTAX FOR DO WHILE LOOP do { //statements to execute here }while(condition); Same type

SYNTAX FOR DO WHILE LOOP do { //statements to execute here }while(condition); Same type of condition as while loop but have to consider what has been done before the bottom of the loop

DO WHILE LOOP Similar to a while loop, will execute at least one time

DO WHILE LOOP Similar to a while loop, will execute at least one time Bottom-test loop Typically not used unless the code in a while loop situation is being repeated(Next Slide)

EXAMPLE OF DO WHILE LOOP

EXAMPLE OF DO WHILE LOOP

WHILE AND DO WHILE LOOPS While and do while loops both require the loop

WHILE AND DO WHILE LOOPS While and do while loops both require the loop to modify the condition at some point of time Otherwise, if condition is not able to be modified, loop goes on forever

SYNTAX FOR LOOP for(initial; condition; loop){ //statement } initial - any form of initializer

SYNTAX FOR LOOP for(initial; condition; loop){ //statement } initial - any form of initializer to set the value (situation in which the loop should start in) eg. Start from 0 ? Start from some number? etc. condition – how to stop? loop – statements to be done when continuing in loop(usually increment or decrement of the intializer variable)

FOR LOOP A loop which executes as long as a certain condition evaluates to

FOR LOOP A loop which executes as long as a certain condition evaluates to true Generally used when a counter variable is involved For example, you will use a for loop for the following situations : going through an array, executing some block of code a fixed number of times

SYNTAX FOREACH LOOP foreach(type temp. Var in collection){ //statement } type – type of

SYNTAX FOREACH LOOP foreach(type temp. Var in collection){ //statement } type – type of temp. Var (should match the same type of elements as collection) temp. Var – the variable that will represent an item in the collection each time the loop runs collection – any set of items that allows iteration

FOREACH LOOP Similar to for loop but there is no need for a counter

FOREACH LOOP Similar to for loop but there is no need for a counter A foreach loop is commonly used when iterating through an array or collection of objects. Operations can be done to the variable inside the loop The scope of the variable for the foreach loop is only limited to within the loop

FOREACH STATEMENT Example: label 1 16

FOREACH STATEMENT Example: label 1 16

BREAK STATEMENT You can use a break statement to escape out of a loop

BREAK STATEMENT You can use a break statement to escape out of a loop immediately. Example: What is the value of counter? 17

CONTINUE STATEMENT The continue statement exits the current iteration and jumps to the loop

CONTINUE STATEMENT The continue statement exits the current iteration and jumps to the loop condition. Basically, it “skips” the rest of the current iteration and goes to the next iteration while (condition) { {code block A} continue; {code block B} } code block B not executed 18

CONTINUE STATEMENT Example: What’s the value of sum? 19

CONTINUE STATEMENT Example: What’s the value of sum? 19

RESOURCES This slides can be downloaded at http: //www. thatcodingplace. wordpress. com/

RESOURCES This slides can be downloaded at http: //www. thatcodingplace. wordpress. com/