Lecture 9 Flashcards
(4 cards)
Syntax:
~ while (boolean_expression) {
/* statement(s) will execute until boolean expression is false /
}
~ the keyword is while
the while keyword starts the while loop control statement
the condition follows the while keyword, like while (condition)
the condition is wrapped in parenthesis (condition)
the block of code is wrapped in curly braces, like { /block of code*/ }
While Loop:
- What is the while loop control statement?
- Why is the while loop control statement important?
- How to use the while loop control statement?
- ~The while control statement is a group of code that tells the program to execute some code repeatedly based on a condition
- ~it allows a program to repeat without having to predetermine how many times the program needs to be repeated
that means: it allows your program to perform repetitive tasks for an unknown number of times - ~group of code that includes:
a condition
a block of code that is executed whenever the condition is true
after the block of code has finished executing, the condition is re-evaluated and the process is repeated until the condition is false
Counter:
~ Be careful with the counter:
be careful not to make an infinite loop
if that happens:
usually you can just hit CTRL+C, if you were working on a real terminal
since Cloud9 is in a web browser, you would sometimes need to close the terminal tab
if closing the terminal tab doesn’t fix the infinite loop, then exit Cloud9 and re-enter
be sure to update the code so avoid running another infinite loop again
counter = 1;
while (counter <= 3)
if (counter == 3)
be sure the offset is correct to get the results you expect
Things to note:
~ there’s a condition to test whether things are true or false
• a condition is an expression that simplifies to true or false
~ there’s one block of code
• the block of code is executed whenever the condition is true
after the block of code has finished executing, the condition is re-evaluated and the process is repeated until the condition is false
~ there’s no false block of code
• the block of code is skipped when the condition is false
~ control statements can be nested
•nesting control statements too deeply will make the code hard to read
•if you need to nest deeply, then consider using functions to abstract away some of the logic; we’ll discuss functions and abstraction in a later lecture