31
Control Structures Week 3

Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

Embed Size (px)

Citation preview

Page 1: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

Control Structures

Week 3

Page 2: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

2.1 Introduction

- Representation of the theory and principles of structured programming.

Demonstration of for, while,do…while and switch statements

2

Page 3: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

while Repetition Structure

3

Page 4: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

while Repetition Structure

• Repetition structure– Action repeated while some condition remains true– Psuedocode

while there are more money in my pocket Buy coca cola

– while loop repeated until condition becomes false

• Exampleint product = 2;

while ( product <= 1000 )

product = 2 * product;4

 

Page 5: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

The while Repetition Structure

• Flowchart of while loop

5

product <= 1000 product = 2 * producttrue

false

Page 6: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

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

Page 7: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

Formulating Algorithms (Counter-Controlled Repetition)

• Pseudocode for example:Set total to zeroSet grade counter to oneWhile grade counter is less than or equal to ten

Input the next gradeAdd the grade into the totalAdd one to the grade counter

Set the class average to the total divided by tenPrint the class average

• Next: C++ code for this example7

Page 8: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

8

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

Page 9: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

9

• 21 // processing phase• 22 while ( gradeCounter <= 10 ) { // loop 10 times• 23 cout << "Enter grade: "; // prompt for input• 24 cin >> grade; // read grade from user• 25 total = total + grade; // add grade to total• 26 gradeCounter = gradeCounter + 1; // increment counter• 27 }• 28 • 29 // termination phase• 30 average = total / 10; // integer division• 31 • 32 // display result• 33 cout << "Class average is " << average << endl; • 34 • 35 return 0; // indicate program ended successfully• 36 • 37 } // end function main

Enter grade: 98

Enter grade: 76

Enter grade: 71

Enter grade: 87

Enter grade: 83

Enter grade: 90

Enter grade: 57

Enter grade: 79

Enter grade: 82

Enter grade: 94

Class average is 81

The counter gets incremented each time the loop executes. Eventually, the counter causes the loop to end.

Page 10: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

Essentials of Counter-Controlled Repetition

• Counter-controlled repetition requires1. the name2. initial value3. loop – continuation4. increment/decrement

Page 11: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

Counter-controlled repetition example

1. 1. NamesNames

1

2. 2. Initial valueInitial value

2

3

3. 3. Loop-continuationLoop-continuationconditioncondition

4

4. 4. IncrementIncrement

Page 12: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

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.

Page 13: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

for Repetition Statement example

Page 14: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

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

Page 15: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

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 )

Page 16: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

Summing integers with the for statement.

Page 17: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

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

Page 18: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

do...while repetition statement

Page 19: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

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

Page 20: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

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

Page 21: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

break statement exiting a for statement.

Page 22: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

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 loop-continuation test evaluates immediately after the continue statement executes. In the for statement, the increment expression executes, then the loop-continuation test evaluates.

Page 23: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil
Page 24: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

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

Page 25: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

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 ) seniorFemales++;

Page 26: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

&& (logical AND) operator truth table

- The table shows all four possible combinations of false and true

Page 27: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

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 ( ( semesterAverage >= 90 ) || ( finalExam >= 90 ) ) cout << "Student grade is A" << endl;

Page 28: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

|| (logical OR) operator truth table

Page 29: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

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

Page 30: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

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

Page 31: Control Structures Week 3. 2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil

THANK YOU FOR YOUR ATTENTION