Upcoming SlideShare
×

# While loops

1,504 views

Published on

1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
1,504
On SlideShare
0
From Embeds
0
Number of Embeds
1,092
Actions
Shares
0
6
0
Likes
1
Embeds 0
No embeds

No notes for slide

### While loops

1. 1. CS111 Lab while loops Instructor: Michael Gordon
2. 2. Repeating instructions  To repeat a set of instructions, we can use a while loop, which checks a condition (like an if statement) and continues as long as the condition is true.  while (condition to check){ instructions; }
3. 3. A word of advice  If the while loop checks a certain variable, make sure that:  A) The variable has a value before the loop starts.  B) That variable can change inside the loop.  x = 1;  while (x<5){“output”} will never end  while (x<5) {x++; “output”} will stop once x is 5.
4. 4. Incrementing  Incrementing (+1) and decrementing (-1) are useful tools for iterating in loops.  int x = 0;  x++; //means x = x+1; called post-increment  ++x; // means x = x+1; called pre-increment  x+=1; // means x = x+1;  Decrementing:  --x;  x--;