13 LoopsFunctionString

13 LoopsFunctionString - Some loops never run! Some loops...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Some loops never run! Some loops never run! What will be displayed by the following program? int num = 100; while(num < 10) { println(num); println(num); num++; num++; } 1 Some loops never run! Some loops never run! What will be displayed by the following program? NOTHING! but with a small change. . . int num = 100; while(num < 10) { println(num); println(num); num++; num++; } 2 Some loops never run! Some loops never run! What will be displayed by the following program? int num = 100; do { println(num); println(num); num++; num++; }while(num < 10); Now it displays 100! 3 do-while loops do-while Because the code for the control expression is at the bottom, a do-while loop always runs at least once int num = 100; do { println(num); println(num); num++; num++; }while(num < 10); 4 while vs. do-while vs. A while may never run A do-while will always run at least once, do-while will always run at least once, even if the control expression is false Otherwise, they do the same thing 5 toUpperCase() & toLowerCase() toUpperCase() returns a new String that is all upper or lower case does NOT change the original String 6 A function that "removes" 'a's A function that "removes" removeAs("apple") → "pple" removeAs("apple") removeAs("banana") → "bnn" removeAs("banana") removeAs("aaaaaaa") → "" removeAs("aaaaaaa") removeAs("bcdef") → "bcdef" removeAs("bcdef") String removeAs(String str) String { ??? ??? } 7 A function that "removes" 'a's A function that "removes" String removeAs(String str) { String noAs = ""; String ??? ??? return noAs; return } 8 A function that "removes" 'a's A function that "removes" String removeAs(String str) { String noAs = ""; String for(int i = 0; i < str.length(); i++) for(int { ??? ??? } return noAs; return } 9 A function that "removes" 'a's A function that "removes" String removeAs(String str) { String noAs = ""; String for(int i = 0; i < str.length(); i++) for(int { if(str.charAt(i)=='a') if(str.charAt(i)=='a') { ??? ??? } } return noAs; return 10 } A function that "removes" 'a's A function that "removes" String removeAs(String str) { String noAs = ""; String for(int i = 0; i < str.length(); i++) for(int { if(str.charAt(i)!='a') if(str.charAt(i) { ??? ??? } } return noAs; return 11 } A function that "removes" 'a's A function that "removes" String removeAs(String str) { String noAs = ""; String for(int i = 0; i < str.length(); i++) for(int { if(str.charAt(i)!='a') if(str.charAt(i) { } } noAs = noAs ??? ??? } return noAs; return 12 A function that "removes" 'a's A function that "removes" String removeAs(String str) { String noAs = ""; String for(int i = 0; i < str.length(); i++) for(int { if(str.charAt(i)!='a') if(str.charAt(i)!='a') { noAs = noAs + str.charAt(i); noAs } } return noAs; return } 13 What the whole program would look like What the whole program would look like void setup() { println(removeAs("apple") ); println(removeAs("banana") ); println(removeAs("aaaaaaa") ); println(removeAs("bcdef") ); } String removeAs(String str) { String noAs = ""; String for(int i = 0; i < str.length(); i++) for(int { if(str.charAt(i)!='a') if(str.charAt(i)!='a') { noAs = noAs + str.charAt(i); noAs } } return noAs; return } 14 find the output find the output void setup() { println(mystery("yellow")); println(mystery("yellow")); String test = "willy nilly"; String println(mystery(test)); println(mystery(test)); println(mystery(test.toUpperCase())); println(mystery(test.toUpperCase())); println(test); println(test); } String mystery(String str) { String s = ""; String for(int i = 0; i < str.length(); i=i+2) for(int { if(str.charAt(i) != 'y') if(str.charAt(i) { s = s + str.charAt(i); str.charAt(i); } } return s; return } 15 ...
View Full Document

This note was uploaded on 01/13/2012 for the course CS 12 taught by Professor Ma during the Spring '08 term at UC Davis.

Ask a homework question - tutors are online