80 likes | 225 Vues
Session-19 CSIT 121 Spring 2006. Count controlled loops can be implemented with for statement This statement becomes the loop header for (lcv=initial value; lcv<=final value; lcv modification statement)
E N D
Session-19 CSIT 121 Spring 2006 • Count controlled loops can be implemented with for statement • This statement becomes the loop header • for (lcv=initial value; lcv<=final value; lcv modification statement) • For example; we have to evaluate factorial of a number. If we use a for loop, it will be quick and simple
Count Controlled Nested Loops • You can embed loops within loops (nested loops) • It may be useful when processing data in separate categories • The task may be performing repetitive operations and the task itself may be repetitive • Example: A program displays multiplication tables for numbers 1 through 10 as on the next page • A clock is incrementing seconds and after 60 seconds it increments minutes and after 60 minutes, it increments hours. Thus it is performing repetitive operations.
Lab Demo Due April 11th • Design a program that simulates a digital clock. When the program starts running, it displays the following time: • 00:00:00 • Later, the program starts changing the time just like a clock would do. For example, the next display would be: • 00:00:01
Help: Familiar Format • Instead of using Sleep() function with a changed format of the program, you may want to waste about one second with this idle loop. Plug it in to your program where you need the delay of one second: • for (int j=0; j<500000000; j++); • system("cls"); //to clear the screen
Help: Unfamiliar Format • Instead of : • #include <iostream> • #include <iomanip> • Using namespace std; • Use the following directives (Old C Style): • #include <stdio.h> • #include <windows.h> • #include <iomanip.h> • Then use the following line to sleep for one second: • Sleep(1000);
Help: Both Formats • You also need the following statements to set the format of your output: • cout.setf(ios::fixed | ios::right); • cout.fill('0'); //leading zeroes displayed
Practice Exercises • Warm up exercise 1 Page 299 • Warm up exercise 6 Page 300 • Warm up exercise 12 Page 300