CMPSC 201 Chapter Notes - Chapter 5: While Loop, Data Validation, Semicolon

54 views4 pages
School
Course
Increment - value increases by 1
decrement- value decreases by 1
Prefix mode (++2)
2 is first incremented. The output will be 3
Postfix mode (2++)
2 is first sent as an output then it is incremented
x=10
if (x++ >10)
statement wont execute because it is compared first
but;
x=10
if (++x >10)
it is incremented first. 11 is than compared with 10
A loop is a part of a program that repeats
3 loops: while, do-while, for
while loop:
has 2 parts-
1. expression that is tested for true or false value
2. statement that is repeated as long as the expression is true
Iteration: each repetition of a loop
loop control variable: something that controls the number of times something is iterated
infinite loop:
1. When there is no statement that stops repetition
2. When a semicolon is placed after a while statement. It is considered to be a null statement
3. Forgetting braces
4. Using = instead of ==
it can be used for input validation. If user enters wrong input, the program is carried out till the
user enters a correct number
counter: variables such as num++ that keeps track of how many times iteration is carried out
do-while loop:
posttest loop
must be terminated with a semicolon (while (expression);)
user controlled loops because it’s unto user how many times it should be iterated
starts with do ends with while so statement is executed even if it is false
for loop:
conditional loop: executes as long as a particular condition exists
count controlled loop: executes a fixed number of times
count- controlled loop must possess 3 elements:
1. Must initialize a counter variable to a starting value
2. Must compare counter variable by comparing it to max value. If it reaches max value it
terminates
3. Must update counter variable after each iteration. Usually done by incrementing
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows page 1 of the document.
Unlock all 4 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Increment - value increases by 1 decrement- value decreases by 1. 2 is rst sent as an output then it is incremented x=10 if (x++ >10) statement wont execute because it is compared rst but; x=10 if (++x >10) it is incremented rst. A loop is a part of a program that repeats. 3 loops: while, do-while, for while loop: has 2 parts: expression that is tested for true or false value, statement that is repeated as long as the expression is true. Iteration: each repetition of a loop loop control variable: something that controls the number of times something is iterated in nite loop: when there is no statement that stops repetition, when a semicolon is placed after a while statement. It is considered to be a null statement: forgetting braces, using = instead of == it can be used for input validation. If it reaches max value it terminates: must update counter variable after each iteration.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents