1 / 2

For Loops

For Loops. For loops are the preferred loop among programmers, although in certain situations, other loops are more appropriate. For loops: initialization, condition, and increment/decrement are all taken care of with one line of code Syntax:

lotta
Download Presentation

For Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. For Loops • For loops are the preferred loop among programmers, although in certain situations, other loops are more appropriate. • For loops: initialization, condition, and increment/decrement are all taken care of with one line of code • Syntax: for (initialization; condition; increment/decrement) { // loop body } • Demo: ForLoopDemo 1 2 3

  2. Assignments • (ForLoops) Use a for loop for each task: • Display the numbers 55 to 61 • Display even numbers from 40 to 50 • Display backwards numbers from 2 to -9 • (first, test the previous loops…) Then, deliberately create an infinite for loop. • (ForCounter) • Have the user type a sentence. • Error check – with a while loop: must be at least 10 characters, at most 25, AND must contain at least 2 words • Next, Using a for loop, display each character of the sentence (including spaces) on a separate line. • Display the number of times that letters from your own first nameare in the sentence • Display how many times the letter e is in the sentence • Display whether this # is divisible by 3 • Display the name of the bassist for the band Rush

More Related