Questions? Email sfo.lax6776@gmail.com

Join our weChat group or follow us on Facebook

©2019 by Legion of Learners

sfolax6776
Jun 27

5.4 Can we exit a loop early if something happens?

0 comments

Edited: 6 days ago

A. Introduction

There are occasions that we might need to terminate a loop prematurely. There two statements for this purpose. Please note they apply not only for for-loops, but also for while-loops and do-loops.

B. Watch (Slides)

C. Try: to print out all prime numbers that are less than 100, do you need to use break or continue statements? Can your program achieve the same goal without using them?

 

New Posts
  • sfolax6776
    7 days ago

    A. Introduction The third and last type of flow is all about repetitions: how to repeat some actions exact N times? or how to repeat some actions until certain status is achieved? There are actually three types of statements: for loops, while loops and do loops. B. Watch( Slides ) C. Try: Print out a multiplication table from 1 x 1 = 1 .... to 9 x 9 = 81. (Hint: you need a for loop inside a for loop)
  • sfolax6776
    Nov 1

    A. Introduction You were asked to use for-loops to print out a multiplication table, for-loops and nested for-loops are widely used for many applications. Here are some examples. B. Watch( Slides ) C. Try to print out the upper half or the lower half of a multiplication table like this:
  • sfolax6776
    Oct 26

    A. Introduction do loops are essentially same at while loops except that they will run at least once. All three types of loops can be converted into each other... Which one to choose? B. Watch( slides ) C. Try this: Write a do loop that reads integers and computes their sum. Stop when reading a zero or the same value twice in a row. For example, if the input is 1 2 3 4 4, then the sum is 14 and the loop stops.