Lecture10-w04-LIN-09252019-ForLoop-2Complement.pdf - Computing I COMP.1010 Fall 2019 Lecture 11 for loop Two’s complement By Sirong Lin PhD University

# Lecture10-w04-LIN-09252019-ForLoop-2Complement.pdf -...

• 37

This preview shows page 1 - 9 out of 37 pages.

Computing I By: Sirong Lin, PhD University of Massachusetts Lowell COMP.1010, Fall 2019 Lecture 11: for loop, Two’s complement
Work Ready Life Ready World Ready Computing I, F2019, By Dr. Sirong Lin Upcoming Due Dates 2 Due Date Daily 08 09-25-2019 (W), 9:00pm Week 04 Readings MWF, 9:00am Week 04 Practice 09-29-2019 (Su), 9:00pm Daily 09 09-27-2019 (F), 9:00pm Daily 10 09-30-2019 (M), 9:00pm Two’s complement HW 09-30-2019 (M), before class Daily 01-04 Resubmission 09-30-2019 (M), 9:00pm HW1 10-02-2019 (W), in class
Work Ready Life Ready World Ready Computing I, F2019, By Dr. Sirong Lin Outline for loop Tow’s Complement 3
Work Ready Life Ready World Ready Computing I, F2019, By Dr. Sirong Lin Loop while loop for loop do-while loop 4
Work Ready Life Ready World Ready Computing I, F2019, By Dr. Sirong Lin for Statement — Syntax 5 for ( initialization of expression ; loop repetition condition ; update expression ) { Loop body (executable statements) } 3 sections separated by two semi-colons (1) initialization of expression (e.g., loop_counter = 0 ) (2) loop repetition condition (e.g., loop_counter < 6) (3) update expression (loop_counter ++) reserve these sections for loop usage only 1 2 3
Work Ready Life Ready World Ready Computing I, F2019, By Dr. Sirong Lin for Statement — Syntax (Cont’d) for ( initialization of expression ; loop repetition condition ; update expression ) { Loop body (executable statements) } 6 initialization expression is executed loop repetition condition is tested. If it is true, the loop body is executed, and the update expression is executed. Then the loop repetition condition is retested. loop body is repeated as long as the loop repetition condition is true; when this condition is found to be false, the for loop is exited.
Work Ready Life Ready World Ready Computing I, F2019, By Dr. Sirong Lin for Statement —Syntax (Cont’d) for ( initialization of expression ; loop repetition condition ; update expression ) { Loop body (executable statements) } 7 for ( int loop_counter = 0 ; //initialization loop_counter < 6 ; //repetition condition loop_counter += 1) //update { printf ( "%d\n" , loop_counter); } Output: 0 1 2 3 4 5
Work Ready Life Ready World Ready Computing I, F2019, By Dr. Sirong Lin for Statement — Example Exercise: modify the function drawMultipleHouses with a for loop 8 void drawMultipleHouses( int numOfHouses) { int loop_counter = 1; while (loop_counter <= numOfHouses) { drawHouse (); loop_counter = loop_counter + 1; } //end of while } void drawMultipleHouses_for( int numOfHouses) { int loop_counter; for (loop_counter = 1; loop_counter <= numOfHouses; loop_counter++) { drawHouse (); }//end of for }

#### You've reached the end of your free preview.

Want to read all 37 pages?

• Fall '15

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern