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

Slides:



Advertisements
Similar presentations
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Advertisements

Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Do/while Structure (L15) * do/while structure * break Statement * continue Statement * Loop Programming Techniques - Interactive input within a loop -
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Program Control Dilshad M. Shahid New York
Control Structures in C++ while, do/while, for switch, break, continue.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Structured Program Development in C
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
1 Lecture 4 for loops and switch statements Essentials of Counter-Controlled Repetition Counter-controlled repetition requires  Name of control.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.
1 CSCE 1030 Computer Science 1 Control Statements in Java.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
C++ Programming Lecture 6 Control Structure II (Repetition) By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
 2003 Prentice Hall, Inc. All rights reserved. 1 Control Structures Outline -Introduction -Algorithms -Pseudocode -Control Structures -if Selection Structure.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
1 Lecture 3 Control Structures else/if and while.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Computer Programming Control Structure
Introduction to Computers and Programming Lecture 7:
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2551 Dr. S. Kozaitis Fall Chapter 5 - Control Statements: Part 2 Outline 5.3 for Repetition.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Introduction to Computer Programming
Chapter 4 – C Program Control
Control Structures Sequential execution Transfer of control
while Repetition Structure
Chapter 4 C Program Control Part II
Control Statements: Part 2
JavaScript: Control Statements I
Week 4 – Repetition Structures / Loops
Ch 7: JavaScript Control Statements I.
Chapter 2.2 Control Structures (Iteration)
Control Statements Kingdom of Saudi Arabia
JavaScript: Control Statements I
Programming Fundamentals Lecture #6 Program Control
MSIS 655 Advanced Business Applications Programming
Structured Program
3 Control Statements:.
2.6 The if/else Selection Structure
EPSII 59:006 Spring 2004.
Presentation transcript:

Control Structures Week 3

2.1 Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil e and switch statements 2

while Repetition Structure 3

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 Example int product = 2; while ( product <= 1000 ) product = 2 * product; 4

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

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

8 1 // Fig. 2.7: fig02_07.cpp 2 // Class average program with counter-controlled repetition. 3 #include 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 // initialization phase 18 total = 0; // initialize total 19 gradeCounter = 1; // initialize loop counter 20

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 } // termination phase 30 average = total / 10; // integer division // display result 33 cout << "Class average is " << average << endl; return 0; // indicate program ended successfully } // 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.

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 1 2. Initial value Loop-continuation condition 4 4. Increment

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

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

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

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 switch statement, causes immediate exit from that statement Program execution continues with the next statement

break statement exiting a for statement.

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.

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 both TRUE before we choose a certain path of execution if ( gender == 1 && age >= 65 ) seniorFemales++;

&& (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 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;

|| (logical OR) operator truth table

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