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 -...

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
Image of page 1
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
Image of page 2
Work Ready Life Ready World Ready Computing I, F2019, By Dr. Sirong Lin Outline for loop Tow’s Complement 3
Image of page 3
Work Ready Life Ready World Ready Computing I, F2019, By Dr. Sirong Lin Loop while loop for loop do-while loop 4
Image of page 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
Image of page 5
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.
Image of page 6
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
Image of page 7
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 }
Image of page 8
Image of page 9

You've reached the end of your free preview.

Want to read all 37 pages?

  • Fall '15

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors