class_notes6

Class_notes6 - Engineering 3 Today more for loops sample problem variable assignment for loops dowhile loops break and continue Readings Chaps 1 4

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Engineering 3 Today: •m o r e f o r l o o p s sample problem variable assignment •f o r l o o p s do…while loops break and continue Readings: Chaps 1 - 4 Midterm, C Programming: August 29 th , in class
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Format when using for loops: for ( initialization ; LoopContinuationTest ; increment ){ statement ( s ) ; } Initialization, LoopContinuationTest, and Increment – Initialization and Increment can be comma-separated lists (LoopContinuationTest cannot be of this format) – Arithmetic expressions may be used in all 3 –E x am p l e : for (i = 0, j = 0 ; j+i <= 10 ; j--, i += 2 ){ printf( “%3d”, i+j ); }
Background image of page 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 /* using the for statement */ #include <stdio.h> int main() { int final, sum, num; printf( "\nThis program sums up the first n integers." ); printf( "\nEnter a value for n: " ); scanf( "%d", &final ); /* read integer */ for ( num = 1, sum = 0 ; num <= final ; num++ ) { sum = sum + num; } printf( "\nThe sum of the first %d integers is %d.\n\n", final, sum ); return 0; /* indicate that program ended successfully */ } /* end function main */
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Can you modify the example on the previous page so that it sums all of the odd integers between 0 and n ?
Background image of page 4
What does the following program output?
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/06/2010 for the course ENGR 3 taught by Professor Ben-yaakov during the Summer '08 term at UCSB.

Page1 / 17

Class_notes6 - Engineering 3 Today more for loops sample problem variable assignment for loops dowhile loops break and continue Readings Chaps 1 4

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online