20 likes | 230 Views
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:
E N D
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
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