1 / 21

IPC144 - LOOPS

IPC144 - LOOPS. while - do while - for. Assignment 1. Let’s talk …. While. To perform some code repeatedly Write a program to print odd numbers between 50 and 100. The program prints the number and the square of the number. k=51; while(k<100){

robin-sweet
Download Presentation

IPC144 - LOOPS

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. IPC144 - LOOPS while - do while - for

  2. Assignment 1 Let’s talk …

  3. While To perform some code repeatedly Write a program to print odd numbers between 50 and 100. The program prints the number and the square of the number. k=51; while(k<100){ printf(“%d square: %d \n”, k, k*k ); k = k + 2; }

  4. Do / while Do-While Statement is similar to while but the test is at the bottom of the loop) So the statement(s) are always performed once at least. k=51; do{ printf(“%d square: %d \n”, k, k*k ); k = k + 2; {while(k<100); Note: Semi-colon at end of while

  5. Compare While with Do /While Using do the body will be executed at least one time But with while the body can be executed zero times In this case using “do” will print one line, but using while, no lines are printed. k=1051; do{ printf(“%d square: %d \n”, k, k*k); k = k + 2; }while(k<100); ======================================== k=1051; while(k<100){ printf(“%d square: %d \n”, k, k*k, ); k = k + 2; } =========================================

  6. for The For Statement allows the programmer to specify in a single line: 1) the starting value of the loop variable (initialization) 2) the test 3) The change in value of the loop variable (often called the step). for (k=51; k<100; k=k+2){ printf(“%d square: %d \n”, k, k*k ); }

  7. for Comments on the For Statement: The test is performed before entering the body of the loop. So the body can be executed zero times (similar to while) Here is an example: j = 42; for (k=51 ; k<j ; k=k+2){ printf(“k is: %d and j is: %d . k is less than j\n”, k, j ); }

  8. for Comments on the For Statement: The order of operations is: 1) Set the loop variable to it’s starting value 2) Perform the test 3) Perform the body of the loop if the test resolves to true 4) After the body has been performed, change the value of the loop variable as specified in the third part of the For. j = 82; for (k=51 ; k<j ; k=k+2){ printf(“k is: %d and j is: %d . k is less than j\n”, k, j ); }

  9. for Comments on the For Statement: It’s unusual but the test does not have to be on the loop variable. printf(“Enter age: “); scanf(“%d”, &age); /* assume age less than 65 /* for (k=51 ; age <=65 ; k=k+1){ income = 42000 * (age / 65); printf(“Age: %d Income is: %.2lf \n “, age,income); }

  10. for Comments on the For Statement: C will not stop you from changing the value of the loop variable in the body of the loop, but it is not recommended as we are not expecting changes in the loop variable in the body. This makes the program difficult to follow. for (k=51 ; k < 65 ; k=k+1){ income = 42000 * (k/65); age = k; printf(“Age: %d Income is: %.2lf \n “, age,income); k = k + k%2; } Note: The age code is not recommended !

  11. Validate ranges with do/while Validate with do/while … do{ printf(“please enter the hourly rate: ”); scanf(“%lf”, &rate); if(rate < 15.00 || rate > 22.00) printf(“Error: pay rate is out of range!”); }while(rate < 15.00 || rate > 22.00);

  12. Validate ranges with While Validate with while … printf(“please enter the hourly rate: ”); scanf(“%lf”, &rate); while(rate < 15.00 || rate > 22.00){ printf(“Error: pay rate is out of range!”); printf(“please enter the hourly rate: ”); scanf(“%lf”, &rate); }

  13. Validating A List of Values Using AND Campers at Robin Hood receive a swimming grade of A, B,or, C. All other codes are incorrect. This is handled with an AND and != . printf(‘Enter grade: ‘); scanf( “ %c”, &grade); while(grade != ‘A’ && grade != ‘B’ && grade != ‘C’ ) { printf(“Error: Grade must be A, B, or, C !\n” printf(‘Enter grade: ‘); scanf( “ %c”, &grade); }

  14. Validating Input Matches a Value in a List: Question 6-3 Apples are graded as ‘A’, ‘J’, or, ‘X’ printf(“Enter grade: “); scanf(“ %c”, &grade); while( ) { }

  15. Prog Question 6-3-Ans Using != char categ; printf(“Enter the apples category: “); scanf(“% c”, &categ); while (categ != ‘A’ && categ != ‘J’ && categ != ‘X’){ printf(“Error: Category must be A, J, or, X!\n”); printf(“Enter the apples category: “); scanf(“%d”, &categ); }

  16. Functions • Functions can be used to break a large problem into smaller pieces. • main( ) calls functions, but a function can call other functions as well. • Main( ) will pass information to the function so that it can perform it’s part of the work.

  17. Example of a Function main() { int num, ans; printf(“Enter a number: "); scanf("%d", &num); ans = fn_triple(num); /* the input to the function is num, and the answer will be in ans */ printf(" total is %d: “ans); } double fn_triple(int number) { int answer; answer = number * 3; return answer; } /* note carefully how the function returns it’s answer to main() */

  18. Function that Triples a Value Steps: • main() gets a value for the number. • Main “calls” the function and copies the value of number into num. • The Function triples num and puts the answer into a local variable called answer. • In the return statement, the value for answer is placed into the function name. • main() then receives the value into ans.

  19. Functions In the example that follows main( ) gets the values and then calls fn_total_charge to calculate the total.

  20. Example of a Function main() { int num; double price, total; char grade = ‘A’; printf(“Enter the number of apples and the price: "); scanf("%d %lf", &num, &price); total = fn_total_charge(num, price); printf("%d apples cost $%.2lf\n",num, total); } double fn_total_charge(int number_sold, double price_each) { double total_cost, tax_rate = .13; /* local variables */ total_cost = (number_sold * price_each) * (1 + tax_rate); return total_cost; }

  21. Functions – Guidelines and Rules 1. Each small program (called a function) should handle one task (or function) 2. When coding functions, we attempt to have 55 lines or less so that the programmer can see the entire function on one printed page. 3. The main() program will control the sequence, selection, and looping involving functions. This is done by "calling the functions". 4. When main() calls a function, main will usually tell the function what values to use for certain key variables. This is known as "passing values or variable addresses". 5. The function "sees" only the passed values, variables whose address has been passed, and any locally defined variables.

More Related