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

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

Computer Science 1620 Loops.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Loops – While, Do, For Repetition Statements Introduction to Arrays
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
C Programming Lecture 12. The Compound Statement b A compound statement is a series of declarations and statements surrounded by braces. b A compound.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
1 Flowchart notation and loops Implementation of loops in C –while loops –do-while loops –for loops Auxiliary Statements used inside the loops –break –continue.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Chapter 05 (Part III) Control Statements: Part II.
Repetition and Iteration ANSI-C. Repetition We need a control instruction to allows us to execute an statement or a set of statements as many times as.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
COMP Loop Statements Yi Hong May 21, 2015.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Chapter 4 – C Program Control
CSE 220 – C Programming Loops.
Chapter 6: Loops.
REPETITION CONTROL STRUCTURE
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Chapter 7 - A closer look at Arrays
Chapter 4 - Program Control
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
CS1010 Programming Methodology
JavaScript: Control Statements I
Loops Chapter 6 Copyright © 2008 W. W. Norton & Company.
Lecture 07 More Repetition Richard Gesick.
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Lecture 4B More Repetition Richard Gesick
Chapter 13 Control Structures
Looping.
Arrays, For loop While loop Do while loop
Chapter 13 Control Structures
- Additional C Statements
Outline Altering flow of control Boolean expressions
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
A First Book of ANSI C Fourth Edition
Chapter 6 Decision Making and Looping
Chapter 4 - Program Control
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Chapter 2.1 Repetition.
Program Control Topics While loop For loop Switch statement
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Chapter 4 - Program Control
Based on slides created by Bjarne Stroustrup & Tony Gaddis
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Chap 7. Advanced Control Statements in Java
Flow of Control.
CSC215 Lecture Control Flow.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Chapter 13 Control Structures
Presentation transcript:

Chapter 5 - Repetition while, do-while, and for loops revisited infinite loops comma operator break statement continue statement 1

the WHILE loop The 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 statement The test is performed first - if it is false the body is NOT executed even once.

the DO-WHILE loop The 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.

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

Infinite loops We 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++) {

The COMMA operator The 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.

The COMMA operator - continued a=(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);

The BREAK statement Already 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 }

The BREAK statement - continued in 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 */

The CONTINUE statement The 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).

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 */ }

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

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

Program 5.1 - specification start declare variables prompt for input read in an array of numbers, stop if EOF print numbers in original order sort the numbers from high to low print sorted numbers at end of each inner loop stop 5

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) }

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; }

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; }

Program 5.2 Write 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.

Voltage Dividers Have many uses: For measuring large voltages via a calibrated divider For simplifying some circuit calculations As filters, either high-pass, low-pass, band-pass or band-notch The circuit for program 5.2 is an example of a high-pass filter

High-pass filter

Program 5.2 - specification start declare variables, initialize constants prompt for resistance, inductance, etc. read resistance, inductance, etc. start at lowest frequency compute divider ratio and print increment frequency logarithmically if frequency less than maximum frequency, repeat last two steps stop 5

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) {

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");

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;