/
Chapter 5 - Repetition while, do-while, and for loops revisited Chapter 5 - Repetition while, do-while, and for loops revisited

Chapter 5 - Repetition while, do-while, and for loops revisited - PowerPoint Presentation

murphy
murphy . @murphy
Follow
79 views
Uploaded On 2023-06-21

Chapter 5 - Repetition while, do-while, and for loops revisited - PPT Presentation

infinite loops comma operator break statement continue statement the WHILE loop The general form of the while loop is while expression statement if the expression ID: 1001271

num freq printf loop freq num loop printf statement count program temp break continue frequency scanf amp high enter

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Chapter 5 - Repetition while, do-while, ..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

1. Chapter 5 - Repetitionwhile, do-while, and for loops revisitedinfinite loopscomma operatorbreak statement continue statement

2. the WHILE loopThe general form of the while loop is while (expression) statement; if the expression is true, the program executes the body of the loop - in this case the single statementThe test is performed first - if it is false the body is NOT executed even once.

3. the DO-WHILE loopThe general form of the do-while loop is do { statement; } while (expression); We will always use the braces in this class, though they are not required for a single statement.The test is made at the end of the loop, so the loop is always executed at least once.

4. the FOR loopThe general form of the for loop is for (expression1;expression2;expression3) statement; the for loop is more compact, but equivalent to: expression1; while (expression2) { statement; expression3; }

5. Infinite loopsWe will see examples of why infinite loops can be useful soon. Here’s how to create them:while (1) { ...do { .... } while (1);for (i=0; ;i++) {

6. The COMMA operatorThe comma operator is used to combine a number of expressions into one.General form: expression1, expression2,...,expressionm;expression1 is evaluated first, expressionm is done last (left-to-right)Result of the whole expression is the result and type of rightmost expression.

7. The COMMA operator - continueda=(b=3, c=b+4); assigns 3 to b and 7 to c and a Possibly the most common use of the comma operator is in for expressions:for(n=exp_x=term=1;n<100;term*=x/n++,exp_x+=term);for(sum=0.0, i=0; i<MONTHS;sum+=atoi(gets(buff)),i++) printf(“Month %d\n”,i);

8. The BREAK statementAlready discussed in Chapter 4 in conjunction with the switch statement.Break can also be used to terminate a loop:for (sum=0, count=0;;count++){ /* infinite loop */ printf(“Enter a number: “); if (scanf(“%d”,&num)==EOF) break; sum+= num}

9. The BREAK statement - continuedin a nested loop, break only terminates the inner most loop - NOT all loops: while (i<5) { while (j<3) { if (i==5) break; /* jump out of inner most loop */ ... } ... /* execution begins here after break */ }

10. The CONTINUE statementThe continue statement immediately forces the statements after the continue statement and up to the end of the loop to be skipped and a new iteration of the loop to begin.Unlike the break statement: the loop is not terminated.the continue statement serves no useful purpose in a switch statement (unless it is imbedded in a loop).

11. The CONTINUE statement - ctd.The general form in a for loop is:for (expression1;expression2;expression3) { ... if (expr) continue; ... /* these statements skipped if expr is true */}

12. To continue or not to continue ...do { if (x==y) { statement1; continue; } statement2; statement3;} while (...);do { if (x==y) statement1; else { statement2; statement3; }} while (...);

13. Program 5.1 - the “bubble” sortread in an array of numbers and sort them from high to low

14. Program 5.1 - specificationstartdeclare variablesprompt for inputread in an array of numbers, stop if EOFprint numbers in original ordersort the numbers from high to lowprint sorted numbers at end of each inner loopstop

15. Program 5.1 - part 1/* perform a bubble sort of an integer array from high to low*/#include <stdio.h>int main(int argc, char *argv[]){ int num[100], i, j, temp, count; // input all of the integers that we are going to sort for(count=0;count<100;count++) { printf("Enter an integer # %i (0 to exit): ",count+1); if (scanf("%d",&num[count])==EOF) break; if (num[count]<=0) break; }

16. Program 5.1 - part 2 // print array before we start to sort printf("\n\nThe unsorted array is:\n"); for(i=0;i<count;i++) printf("%c %d",i%10?' ':'\n',num[i]);// sort for(j=0;j<count-1;j++) { for(i=j+1;i<count;i++) if(num[i]>num[j]) { temp=num[i]; num[i]=num[j]; num[j]=temp; } }

17. Program 5.1 - part 3 // finished sort high to low; print results printf("\n\nThe SORTED array is:\n"); for(i=0;i<count;i++) printf("%c %d",i%10?' ':'\n',num[i]); printf("\n\n"); return 0;}

18. Program 5.2Write a code to read in the value of resistance, inductance, and a range and number of frequencies and calculate the voltage divider ratio as a function of frequency.

19. Voltage DividersHave many uses:For measuring large voltages via a calibrated dividerFor simplifying some circuit calculationsAs filters, either high-pass, low-pass, band-pass or band-notchThe circuit for program 5.2 is an example of a high-pass filter

20. High-pass filter

21. Program 5.2 - specificationstartdeclare variables, initialize constantsprompt for resistance, inductance, etc.read resistance, inductance, etc.start at lowest frequencycompute divider ratio and printincrement frequency logarithmicallyif frequency less than maximum frequency, repeat last two stepsstop

22. Program 5.2 - part 1// Frequency response//// Written by W. Lawson//// last modified 30 Oct 2014//#include <stdio.h>#include <math.h>#define PI 3.141592654///* A program to evaluate the voltage divider rule for an RL circuit as a function of frequency with lineqr and nonlinear increments *///int main(void){

23. Program 5.2 - part 2 int i=0, num_freq; double r, l, freq, temp, freq_min, freq_max, vrat, freqlin, vratlin; printf("enter resistance (ohms)"), scanf("%lf",&r); printf("enter inductance (mH)"), scanf("%lf",&l); printf("enter minimum frequency (kHz)"), scanf("%lf",&freq_min); printf("enter maximum frequency (kHz)"), scanf("%lf",&freq_max); printf("enter number of frequency points"), scanf("%d",&num_freq); printf("\nFrequency (kHz) \tVoltage Ratio\n\n"); printf("\n\n\n Nonlinear Spacing \t\t Linear spacing\n\n");

24. Program 5.2 - part 3 while (i<num_freq) {// nonlinear spacing freq=freq_min*pow(freq_max/freq_min,(float)i/(num_freq-1)); temp=2.*PI*freq; vrat=temp/sqrt(r*r+temp*temp);// linear spacing freqlin=freq_min+(freq_max-freq_min)*i/(num_freq-1); temp=2.*PI*freqlin; vratlin=temp/sqrt(r*r+temp*temp);// print results printf(" %f \t %f \t\t %f \t %f\n",freq,vrat,freqlin,vratlin); i++; } return 0;}