Скачать презентацию Control Structures Week 3 2 1 Introduction Скачать презентацию Control Structures Week 3 2 1 Introduction

47721fd3659a3a01f3a82921fcffd1db.ppt

  • Количество слайдов: 31

Control Structures Week 3 Control Structures Week 3

2. 1 Introduction - Representation of theory and principles of structured programming. Demonstration of 2. 1 Introduction - Representation of theory and principles of structured programming. Demonstration of for, while, do…while and switch statements 2

while Repetition Structure 3 while Repetition Structure 3

while Repetition Structure • Repetition structure – Action repeated while some condition remains true while Repetition Structure • Repetition structure – Action repeated while some condition remains true – Psuedocode while there are money in my pocket Buy coca cola – while loop repeated until condition becomes false • Example int product = 2; while ( product <= 1000 ) product = 2 * product; 4

The while Repetition Structure • Flowchart of while loop true product <= 1000 product The while Repetition Structure • Flowchart of while loop true product <= 1000 product = 2 * product false 5

Formulating Algorithms (Counter. Controlled Repetition) • Counter-controlled repetition – Loop repeated until counter reaches Formulating Algorithms (Counter. Controlled Repetition) • Counter-controlled repetition – Loop repeated until counter reaches certain value • Definite repetition – Number of repetitions known • Example A class of ten students took a quiz. The grades (integers in the range 0 to 100) for this quiz are available to you. Determine the class average on the quiz. 6

Formulating Algorithms (Counter. Controlled Repetition) • Pseudocode for example: Set total to zero Set Formulating Algorithms (Counter. Controlled Repetition) • Pseudocode for example: Set total to zero Set grade counter to one While grade counter is less than or equal to ten Input the next grade Add the grade into the total Add one to the grade counter Set the class average to the total divided by ten Print the class average • Next: C++ code for this example 7

 • • • • • 1 2 3 4 5 6 7 8 • • • • • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 // Fig. 2. 7: fig 02_07. cpp // Class average program with counter-controlled repetition. #include using std: : cout; using std: : cin; using std: : endl; // function main begins program execution int main() { int total; // sum of grades input by user int grade. Counter; // number of grade to be entered next int grade; // grade value int average; // average of grades // initialization phase total = 0; // initialize total grade. Counter = 1; // initialize loop counter 8

 • • • • • 21 22 23 24 25 26 27 28 • • • • • 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 // processing phase while ( grade. Counter <= 10 ) { // loop 10 times cout << "Enter grade: "; // prompt for input cin >> grade; // read grade from user total = total + grade; // add grade to total grade. Counter = grade. Counter + 1; // increment counter } // termination phase average = total / 10; // integer division The counter gets incremented each time the loop executes. Eventually, the counter causes the loop to end. // display result cout << "Class average is " << average << endl; return 0; // indicate program ended successfully } // end function main Enter Enter Enter Class grade: 98 grade: 76 grade: 71 grade: 87 grade: 83 grade: 90 grade: 57 grade: 79 grade: 82 grade: 94 average is 81 9

Essentials of Counter-Controlled Repetition • Counter-controlled repetition requires 1. the name 2. initial value Essentials of Counter-Controlled Repetition • Counter-controlled repetition requires 1. the name 2. initial value 3. loop – continuation 4. increment/decrement

Counter-controlled repetition example 1. Names 2. Initial value 3. Loop-continuation condition 4. Increment 2 Counter-controlled repetition example 1. Names 2. Initial value 3. Loop-continuation condition 4. Increment 2 1 3 4

for Repetition Statement • The while statement can be used to implement any counter-controlled for Repetition Statement • The while statement can be used to implement any counter-controlled loop. • for repetition statement specifies the counter -controlled repetition details in a single line of code.

for Repetition Statement example for Repetition Statement example

Examples Using the for Statement • Vary the control variable from 1 to 100 Examples Using the for Statement • Vary the control variable from 1 to 100 in increments of 1. • for ( int i = 1; i <= 100; i++ ) • Vary the control variable from 100 down to 1 in increments of -1 (that is, decrements of 1). • for ( int i = 100; i >= 1; i-- )

Examples Using the for Statement • Vary the control variable from 7 to 77 Examples Using the for Statement • Vary the control variable from 7 to 77 in steps of 7. • for ( int i = 7; i <= 77; i += 7 ) • Vary the control variable from 20 down to 2 in steps of -2. • for ( int i = 20; i >= 2; i -= 2 )

Summing integers with the for statement. Summing integers with the for statement.

do. . . while Repetition Statement • Similar to the while statement • Tests do. . . while Repetition Statement • Similar to the while statement • Tests the loop-continuation condition after the loop body executes, therefore, the loop body always executes at least once • do { statement } • while ( condition );

do. . . while repetition statement do. . . while repetition statement

break and continue Statements • C++ provides statements break and continue to alter the break and continue Statements • C++ provides statements break and continue to alter the flow of control • This section discusses how to use break in a repetition statement

break Statement • when executed in a while, for, do. . . while or break Statement • when executed in a while, for, do. . . while or switch statement, causes immediate exit from that statement • Program execution continues with the next statement

break statement exiting a for statement. break statement exiting a for statement.

continue Statement • when executed in a while, for or do. . . while continue Statement • when executed in a while, for or do. . . while statement, skips the remaining statements in the body of that statement and proceeds with the next iteration of the loop • In while and do. . . while statements, the loopcontinuation test evaluates immediately after the continue statement executes. In the for statement, the increment expression executes, then the loop-continuation test evaluates.

Logical Operators • logical operators are used to form more complex conditions by combining Logical Operators • logical operators are used to form more complex conditions by combining simple conditions. • The logical operators are: - && (logical AND) - || (logical OR) - ! (logical NOT, also called logical negation).

Logical AND (&&) Operator • When we wish to ensure that two conditions are Logical AND (&&) Operator • When we wish to ensure that two conditions are both TRUE before we choose a certain path of execution • if ( gender == 1 && age >= 65 ) senior. Females++;

&& (logical AND) operator truth table - The table shows all four possible combinations && (logical AND) operator truth table - The table shows all four possible combinations of false and true

Logical OR (||) Operator • When we wish to ensure at some point in Logical OR (||) Operator • When we wish to ensure at some point in a program that either or both of two conditions are TRUE before we choose a certain path of execution • if ( ( semester. Average >= 90 ) || ( final. Exam >= 90 ) ) cout << "Student grade is A" << endl;

|| (logical OR) operator truth table || (logical OR) operator truth table

Logical Negation (!) Operator • the ! (logical NOT, also called logical negation) operator Logical Negation (!) Operator • the ! (logical NOT, also called logical negation) operator enable a programmer to "reverse" the meaning of a condition the preceding if statement also can be written as follows ! (logical negation) operator truth table

Confusing Equality (==) and Assignment (=) Operators • Confusing do not cause syntax errors Confusing Equality (==) and Assignment (=) Operators • Confusing do not cause syntax errors • Using operator == for assignment and using operator = for equality are logic errors. suppose we intend to write but we accidentally write

THANK YOU FOR YOUR ATTENTION THANK YOU FOR YOUR ATTENTION