# For Loops With Strings

**Channel:** MIT OpenCourseWare
**Source:** https://www.youtube.com/watch?v=4WtaFLayz_w
**Transcript page:** https://www.withtranscript.ai/video/4WtaFLayz_w

## Chapters

- 0:00 — Chapter 1 (0:00)

## Transcript

**[0:00]:** the following content is provided under a Creative Commons license your support will help MIT open courseware continue to offer highquality educational resources for free to make a donation or view additional materials from hundreds of MIT courses visit MIT opencourseware at

**[0:22]:** ocw.mit.edu we have two strings S1 S2 first we're going to check if the length of S1 is equal to length two anyone really really quickly can you tell me what is this the length of S1 and S2 how many characters are in

**[0:45]:** there 10 right because we're including spaces so 1 2 3 4 5 6 7 8 9 10 so they are equal first of all so we're going to go inside this if statement then we're going to go for every character in S1 and then we're going to have a nested Loop for every character in S2 we're going to check if the two characters are the same and if they are we're going to print this common letter statement and then we're going to break okay great we have we have some good competition here so let's see the outer for Loop is first going to go through the letter M okay so this is so first it's going to look at M right right for chart in S1 the first character in S1 is an M then we're entering inside this other for Loop for charart 2 in S2 so I'm going to say for this m I'm going to look at I which is the first character in S2 if the character one is equal

**[1:51]:** to character two they're not don't do anything keep going to the next character in S2 okay so that you don't you don't do anything there the next character in S2 is a space m does not equal a space so you don't do anything uh R does not the next character is an R don't do anything the next character is U don't do anything then an L don't do anything then an e don't do anything space and then suddenly we get to an m so we're still in the outer for loop we're still looking at M right and we're comparing M with every other letter inside the S2 and suddenly we reach here where we're comparing M with an m and we say yes they are equal so we're going to print out something make that into better check we're going to print common letter and then we're going to break this break says do not

**[2:48]:** keep looking at the remaining letters in S2 okay so once we've found one that fits that that once we found one that matches stop right here break and print PR out the thing and then go on to looking at I which is the next letter in S1 and you do the same thing compare I with I we have a match perfect right off the bat so you print out another common letter and then you break which means don't keep looking at the remaining letters and then you look at since you broke out of the inner loop you go to the outer loop the next letter is a t and so on and so on so essentially you're comparing every letter out here in S1 with every single letter in S2 until you find one that matches as soon as you do you exit so if you if you actually run this program it's going to

**[3:45]:** print out seven times I think we can check

**[3:54]:** it okay one two three four five six seven times

**[4:03]:** nice majority of the people are right if you got this wrong please go back and try to trace through your program so that means step by step try to figure out what values of variables are just to make sure that you're on the right track and you understand exactly what's happening okay
