INTRO PROGRAMMING PRINCIPLES
INTRO PROGRAMMING PRINCIPLES CSCI 1301
Popular in Course
Popular in ComputerScienence
This 5 page Class Notes was uploaded by Freida Schaefer DDS on Saturday October 3, 2015. The Class Notes belongs to CSCI 1301 at Armstrong Atlantic State University taught by Y. Liang in Fall. Since its upload, it has received 36 views. For similar materials see /class/217868/csci-1301-armstrong-atlantic-state-university in ComputerScienence at Armstrong Atlantic State University.
Reviews for INTRO PROGRAMMING PRINCIPLES
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 10/03/15
Chapter 4 Loops 1 count lt lOO always true at Point A count lt lOO always false at Point C count lt lOO is sometimes true or sometimes false at Point B 2 It would be wrong if it is initialized to a value between 9 and 100 because it could be the number you attempt to guess 3 a In nite number of times b The loop body is executed nine times The printout is 2 4 6 8 on separate lines c The loop body is executed nine times The printout is 3 5 7 9 on separate lines 4 The difference between a do whi l e loop and a wni l e loop is the order of evaluating the continuation condition and executing the loop body In a while loop the continuation condition is checked and then iftrue the loop body is executed In a do whi l e loop the loop body is executed for the first time before the continuation condition is evaluated int sum 039 int number 211 gtgt number39 E i Sum number39 211 gtgt number39 while number 1 0 5 Same When the i and i are used in isolation their effects are same 6 The three parts in a for loop control are as follows The first part initializes the control variable The second part is a Boolean expression that determines whether the loop will repeat The third part is the adjustment statement which adjusts the control variable 7 The loop keeps doing something indefinitely 8 No The scope ofthe variable is inside the loop maX is 5 number 0 sum is 14 count is 4 maX is 5 number 0 Yes The advantages of f or loops are simplicity and readability Compilers can produce more ef cient code for the f or loop than for the corresponding whi 1 e loop while loop long sum 0 int i 0 while i lt 1000 1 sum i do whileloop long sum 0 int i 0 do i While i lt 1000 sum i No Try nl 3 and n2 3 The keyword break is used to eXit the current loop The program in A will terminate The output is Balance is 1 The keyword continue causes the rest ofthe loop body to be skipped for the current iteration The whi 1 e loop will not terminate in B for int i 1 sum lt 10000 i sum sum i 17 If a continue statement is executed inside a for loop the rest of the iteration is skipped then the actionaftereachiteration is performed and the loopcontinuation condition is checked If a continue statement is executed inside a while loop the rest of the iteration is skipped then the loopcontinuationcondition is checked Here is the x int i 0 While i lt 4 i continue sum i i 18 1nclude lt1ostreamgt using namespace std39 int main i int sum 039 int number 039 while number lt 20 ampamp Sum lt 100 i number39 sum number39 4 cout ltlt quotThe number 15 quot ltlt number ltlt endl39 cout ltlt quotThe Sum 15 quot ltlt Sum ltlt endl39 return 039 i 1nclude lt1ostreamgt using namespace std39 int main i int sum 039 int number 039 whlle number lt 20 number 1f number 1 10 ampamp number 1 11 Sum number39 1 cout ltlt quotThe Sum 15 quot ltlt Sum39 return 039 1 19 a Line 1 The semicolon at the end of the for loop heading should be removed Line 4 the semicolon at the end of the if statement should be removed Line 5 Missing a semicolon for the rst println statement Line 9 The semicolon at the end of the while heading should be removed Line 17 Missing a semicolon at the end ofthe dowhile loop b The loop will be executed only one time because the test condition for the while clause is true 20 Tip for tracing programs Draw a table to see how variables change in the program Consider a for example i j output 1 0 0 1 1 2 0 0 2 1 1 2 2 3 0 0 3 1 1 3 2 2 3 3 4 0 0 4 1 1 4 2 2 4 3 3 4 4 A 0010120123 13 2L C D 2 3 2 1XXX2XXX4XXX8XXX16XXX 1XXX2XXX4XXX8XXX 1XXX2XXX4XXX IXXXZXXX IXXX lG 163G lGBGSG lGBGSG7G lGBGSG7G9G x is 2147483648 The reason When a variable is assigned a value that is too large in size to be stored 22 it causes overflow 2l47483647 l is actually 214M83648 A n times 13 n times C n 5 times D The ceiling of n 53 times
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'