Courses‎ > ‎AP Computer Science‎ > ‎Konstantinovich‎ > ‎

2018-09-21 While

posted Sep 21, 2018, 5:48 AM by Konstantinovich Samuel   [ updated Sep 21, 2018, 10:19 AM ]

Goal: While we are learning Strings... we need loops


If you need a better seat because of vision, OR you have a name you prefer to be called instead of your official first name:

please fill this out: https://goo.gl/forms/JIE8eoHyk4WQXhW93


Lab + Homework:

codingbat -> warmup2 ->

stringTimes

frontTimes

countXX

doubleX

stringBits

stringSplosion


String-2

doubleChar

countHi





in python:

while boolean:
  body
x = 0
while x < 10:
  #some more code
x = x + 1

in netlogo:

while [boolean]
[
 ;body
]
let x 0
while [ x < 10][
;some code
set x (x + 1)
]

in java:

while(boolean){
   body
}
int x = 0;
while(x < 10){ //some code here
x = x + 1;
 }
GENERIC CODE:

1. To start, evaluate the boolean when it is true go to INSIDE_LOOP and do step 2, but when it is false go to AFTER_LOOP  and you are done.

2. When you go inside the body, you execute the entire code block. After the body of the loop completes go back to boolean (go back to the step 1).


while (boolean){   

   //INSIDE_LOOP

}

//AFTER_LOOP





Print individual characters in a string:


String s = "doa what"

         //01234567


int i = 0;

while (i < s.length()){  

System.out.println(s.substring(i, i +1));

// s.charAt(i)

i = i + 1;

//OR: i += 1;

//OR: i++;

}


Note that:

a) <= would cause an exception because index 8 doesn't exist, but the length is 8.

b) length() is a method




Make a string with every other chraracter:


String s = "doa whatever";

         //012345678901

String result = "";

int i = 0;

while (i < s.length()){  

result += s.charAt(i);

i = i + 2;

}
//Now result is every even index of s, or just "dawaee"




Comments