Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Basics (retired) Control Flow While and Do-While Loop

Brahim Farhani
Brahim Farhani
1,534 Points

I do NOT UNDERSTAND THIS! HELP

Please help i dont no understand the term Condition and Loop, swell as while loop and do??????!??!?!?!

4 Answers

Blair Rorani
Blair Rorani
6,658 Points

A condition is another word for a 'state' that something or someone is in. For example, what is the condition of your stomach? Empty or full? (The condition of your house, the economy, your iPhone, etc).

If the condition of your stomach is empty, then you should eat something.

A loop is something that repeats (i.e. a cycle). So using the stomach example, you want to keep eating, until the condition of your stomach is full.

Condition and loop are just concepts we use to describe what we mean to a computer so it follows our instructions the way we intend.

Does that help?

Brahim Farhani
Brahim Farhani
1,534 Points

Really good Explanation !! Thank You Can you also explain the why he writes those lines of codes what to they mean the (<) and .count :)

Brahim Farhani
Brahim Farhani
1,534 Points

like the part where it says: println(todo[index]) index++ the index++ is what confuses me the most!!!!

Blair Rorani
Blair Rorani
6,658 Points

index++ is a quick way of writing "increase the value of index by 1". index-- therefore means "decrease the value of index by 1"

.count is a property. An example of a property is "Blair.gender" which would return the value "male" because I'm a male. Other properties that a person might have are Blair.height or Brahim.favouriteFood etc.

.count is a property of an array (a group of items). If my array was called "seasons" and I used the count property like so "seasons.count" then it would return "4" winter, spring, summer, autumn.

I think the < you're referring to is less than. So if seasons.count (which we know is four) is < (less than) 4, then we know the year isn't finished yet (because there are 4 seasons in a year). Not sure if that example is helpful or not :)

Make sense?

Brahim Farhani
Brahim Farhani
1,534 Points

yes it does blair oh and blair are u good with swift?

Stone Preston
Stone Preston
42,016 Points

Loops are used to execute some code over and over again.

while loops run some code while a certain condition is true. do while loops run an action at LEAST once, and then continue to loop if a certain condtion is true.

most loops have a condition, and a body. The condition is tested before each execution of the loop and if its true the loop executes, if its false the loop breaks from execution and does not loop again. Do while loops are an exception since the condition is tested after the loop runs, guaranteeing it will run at least once

you can use a while loop to do something as long as a condition is met

var score = 0
// the line below is the condition
while score < 10 {

     // these lines inside the { } are the loop body
     println("The score is less than 10)
     score++
}

the above loop will continue to print out "the score is less than 10" for as long as the score is less than 10. since it starts at 0 and we increment it every time, the loop will stop running once the score hits 10.

a do while loop is used when you need the loop to run at least once.

// it will print at least one time, even if x is not equal to 10
do {
  // this is the loop body
  println(x)

// the line below is the condition
} while (x == 10)

the while loop is much more common than the do while loop in my experience. but even then you dont really use a while loop that much. the for in loop is probably the one that gets used most

codebully
codebully
2,306 Points

Single equal sign (=) is an assignment. for example, here is a constant; all dogs bark.

let dogsBark = true

Two equal signs (==) however evaluate a condition. Meaning if it's true that dogs bark, we will print a line that says so.

if dogsBark == true {
println("Dogs are barking.")
}
//== two equal signs means "is equal to"
// ie x==y means is x equal to y if its true it will return a boolean true and false otherwise
//example

var x = 10
var y = 20

if x==y {
// perform some function here 
}
Brahim Farhani
Brahim Farhani
1,534 Points

Please this does no help is there anyway I can contact you for help, please :)