CECS 478 Lecture Notes - Lecture 9: Infinite Loop, While Loop, For Loop

37 views2 pages

Document Summary

Three types of loops: for, while, and do-while. While: syntax double balance = 10000; 1. Condition to keep the loop going goal: at least 20000 (>20000) balance = balance + balance*rate; 3. If you have no update statement (3), you will have an infinite loop. Ex: write a program that adds all the digits of an integer (234 = 2 + 3 + 4 = 9) 23/10 2, 2%10 2 num: digit = num%10 num = num/10. Int num = 1729; int sum = 0; while (num > 0) int digit = num%10; sum = sum + digit; num = num/10; Keeps going as long as the number is greater than 0. Sum is updated as the table goes down (there is only one sum at a time). Previous sum + current digit = new sum. Ex: write a program that prints the first 1000 positive integers. While loop: int i = 1; while (i <= 1000)

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