tailieunhanh - Lecture Introduction to computer and programming - Lecture No 10

The contents of this chapter include all of the following: Functions in C, a function with a parameter, example: area of a ring, implementing functions in C, run-time stack, getting it all to work, tying it all together, problem solving using functions,. | CSC103: Introduction to Computer and Programming Lecture No 10 Previous lecture Example programs nested if-else nested conditional operator Loops while loop Today’s lecture outline for loop break statement continue statement do while loop for loop The most popular looping instruction The for loop allows us to specify three things in a single line Initialization: Setting a loop counter to an initial value Condition: Testing loop counter to check whether it reaches the desired number of repetitions Increment / decrement part: Increase or decrease the value of loop counter each time when the statement within the loop are executed General form of for loop for ( ; ; ) { do this; and this; and this; } initialization condition Incr/ decr Incr/ decr stands for increment/ decrement Sequence of execution of for loop for ( ; ; ) { do this; and this; and this; } initialization condition Incr/ decr 1 2 3 4 5 6 7 8 Cont. It is important to note that the initialization, testing and incrementation part of a for loop can be replaced by any valid expression. Ways for writing for loop Cont. Cont. Nested for loop /* Demonstration of nested loops */ main( ) { int r, c, sum ; for ( r = 1 ; r <= 3 ; r++ ) /* outer loop */ { for ( c = 1 ; c <= 2 ; c++ ) /* inner loop */ { sum = r + c ; printf ( "r = %d c = %d sum = %d\n", r, c, sum ) ; } } } Go to program Example program 1 Write a program to find the factorial value of any number entered through the keyboard. Write a program Example program 2 Write a program that displays the table of x up to y. The value of x and y is input by the user. . x = 4 and y = 5 4 x 1 = 4 4 x 2 = 8 4 x 3 = 12 4 x 4 = 16 4 x 5 = 20 Write a program The break Statement There are situations where we want to jump out of a loop instantly, without waiting to get back to the conditional test. The keyword break allows us to do this When break is encountered inside any loop, control automatically passes to the first statement after the loop. A break is usually . | CSC103: Introduction to Computer and Programming Lecture No 10 Previous lecture Example programs nested if-else nested conditional operator Loops while loop Today’s lecture outline for loop break statement continue statement do while loop for loop The most popular looping instruction The for loop allows us to specify three things in a single line Initialization: Setting a loop counter to an initial value Condition: Testing loop counter to check whether it reaches the desired number of repetitions Increment / decrement part: Increase or decrease the value of loop counter each time when the statement within the loop are executed General form of for loop for ( ; ; ) { do this; and this; and this; } initialization condition Incr/ decr Incr/ decr stands for increment/ decrement Sequence of execution of for loop for ( ; ; ) { do this; and this; and this; } initialization condition Incr/ decr 1 2 3 4 5 6 7 8 Cont. It is important to note that the initialization, testing and incrementation .

TỪ KHÓA LIÊN QUAN