Assume the string has at least three characters 1 2 3

This preview shows page 4 - 5 out of 5 pages.

Assume the String has at least three characters. 1 2 3 4 5 6 7 8 String word = "Blabbermouth"; int posLast = word.length() - 1; char letter ; for (int i = posLast; i > posLast - 3; i--) { letter = word.charAt(i); System.out.print( (char) letter ); } System.out.println(); htu 23 for loop – scope of variables Any variable declared in the initialization section of a for loop only exists inside the for loop – and cannot be referred to outside of the loop 1 2 3 4 5 6 7 8 9 String word1 = "Brouhaha"; String word2 = ""; char letter ; for ( int i = 0; i < word1.length(); i ++) { letter = word1.charAt( i ); word2 = word2 + letter + letter; } System.out.println("word2: " + word2); System.out.println("i is position: " + i ); 24 Loops – scope of variables Remember scope: any variable exists from the moment it is created to the end of the block in which it is created. 1 2 3 4 5 6 7 8 9 10 String word1 = "flibbertigibbet"; for ( int i = 0; i < word1.length()/2; i ++) { char letter = word1.charAt( i ); } System.out.println("letter: " + letter ); int i = 0; while ( i < 5) { int num = i + 2 * 513 / 24 – 2; } System.out.println("num: " + num );
Image of page 4

Subscribe to view the full document.

CompSci 101 Lecture 12 5 25 Ex04 Using a for loop complete the printBackwards() method which prints the letters of the String parameter in reverse order. private void printBackwards(String word) { int posLast = … } pernickety ytekcinrep valhalla allahlav Hubbub bubbuH 26 Ex05 Using a for loop complete the printStars() method which prints a row of stars ("*"). The number of stars is given by the parameter. private void printStars(int numberOfStars) { } printStars(3) *** printStars(21) ********************* printStars(9) ********* 27 Ex06 Using a for loop complete the printNumVowels() method which prints the number of vowels in the parameter String. private void printNumVowels(String word) { final String VOWELS = "aeiouAEIOU"; int numVowels = 0; char letter; System.out.println("Number of vowels " + numVowels); } rumpus Number of vowels 2 Squabash Number of vowels 3 Flippant Number of vowels 2 hymn Number of vowels 0 MISDEMEANOUR Number of vowels 6 28 while loops vs for loops Any while loop can be written as a for loop: int i = 0; while ( i < 10 ) { //statements to be repeated i++; } for ( int i = 0 ; i < 10 ; i++ ) { //statements to be repeated } Use a for loop when you need a simple counter to move from one value to another through the loop.
Image of page 5
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern