1 / 8

Session-19 CSIT 121 Spring 2006

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)

tamira
Télécharger la présentation

Session-19 CSIT 121 Spring 2006

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. Multiplication Tables

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

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

  6. 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);

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

  8. Practice Exercises • Warm up exercise 1 Page 299 • Warm up exercise 6 Page 300 • Warm up exercise 12 Page 300

More Related