1 / 37

Introduction to Algorithms and programming-II

Introduction to Algorithms and programming-II. Chapter 2. Control Structures II Department of Computer Science Academic Year: 2017-2018 Semester: Two Dr . Maytham Mustafa Hammood. C++ Programming: From Problem Analysis to Program Design, Fourth Edition

ncarpenter
Download Presentation

Introduction to Algorithms and programming-II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Algorithms and programming-II Chapter 2 Control Structures II Department of Computer Science Academic Year: 2017-2018 Semester: Two Dr. Maytham Mustafa Hammood

  2. C++ Programming: From Problem Analysis to Program Design, Fourth Edition • Chapter 5: Control Structures II (Repetition) C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  3. Objectives In this chapter you will: • Learn about repetition (looping) control structures • Explore how to construct and use count-controlled, sentinel-controlled, flag-controlled, and EOF-controlled repetition structures • Examine break and continue statements • Discover how to form and use nested control structures C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  4. Why Is Repetition Needed? • Repetition allows you to efficiently use variables • Can input, add, and average multiple numbers using a limited number of variables For example, to add five numbers: • Declare a variable for each number, input the numbers and add the variables together • Create a loop that reads a number into a variable and adds it to a variable that contains the sum of the numbers C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  5. The while Loop • The general form of the while statement is: while(expression) statement • while is a reserved word • Statement can be simple or compound • Expression acts as a decision maker and is usually a logical expression • Statement is called the body of the loop • The parentheses are part of the syntax C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  6. The while Looping (continued) • Infinite loop: continues to execute endlessly, can be avoided by including statements in the loop body that assure exit condition will eventually be false C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  7. The while Looping (continued) C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  8. Designing while Loops C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  9. Case 1: Counter-Controlled while Loops • If you know exactly how many pieces of data need to be read, the while loop becomes a counter-controlled loop C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  10. Example: write a program that prompts the user to input 5 integer items then find summation and a average for those items. #include <iostream> using namespace std; const int limit=5; //variable to store the number of items int main() { int number; //variable to store the number int sum; //variable to store the sum int counter; //loop control variable sum = 0; counter = 0; while (counter < limit) { cout << “ Enter number for processing:“; cin >> number; sum = sum + number; counter++; } C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  11. Example of Counter-Controlled while (continued) cout << “The sum of the " << limit << " numbers = " << sum << endl; if (counter !=0 ) cout << “The average = “ << sum / counter << endl; else cout << “No input." << endl; return 0; } C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  12. Case 2: Sentinel-Controlled while Loops • Sentinel variable is tested in the condition and loop ends when sentinel is encountered C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  13. Example: write a program that prompts the user to input set of integer items ending with item that is equal to -999 then find summation and a average for those items. #include <iostream> using namespace std; const int SENTINEL = -999; //variable to store the number of items int main() { int number; //variable to store the number int sum = 0; //variable to store the sum int count = 0; //variable to store the total numbers read cout << “ Enter numbers ending with “ << SENTINEL << endl; cin >> number; while (number != SENTINEL) { sum = sum + number; count++; cin >> number; } C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  14. Example of Sentinel-Controlled while (continued) cout << “The sum of the " << count << " numbers is " << sum <<endl; if (count != 0) cout << “The average = “ << sum / count << endl; else cout << “No input." << endl; return 0; } C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  15. Case 3: Flag-Controlled while Loops • A flag-controlled while loop uses a bool variable to control the loop • The flag-controlled while loop takes the form: C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  16. EOF- Controlled while Loops • Use an EOF (End Of File)-controlled while loop • The logical value returned by cin can determine if the program has ended input • The syntax is: cin >> variable; while (cin) { . cin >> variable; . } C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  17. Example: write a program that prompts the user to input set of integer items ending with item that is negative then find summation positive items. #include <iostream> using namespace std; int main() { int num, sum; bool isNegative; sum = 0; isNegative = false; cout << "Enter numbers: "; cin >> num; C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  18. Example of Flag-Controlled while (continued) while (cin && !isNegative) { if (num < 0) //if num is negative, terminate the loop after this iteration { cout << "Negative number found in the data." << endl; isNegative = true; } else { sum = sum + num; cin >> num; } } cout << endl; cout << "The sum is: " << sum << endl; return 0; } C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  19. More on Expressions in while Statements • The expression in a while statement can be complex For example: while ((noOfGuesses < 5) && (!isGuessed)) { … } C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  20. do…while Looping (Repetition) Structure • General form of a do...while: • The statement executes first, and then the expression is evaluated • To avoid an infinite loop, body must contain a statement that makes the expression false • The statement can be simple or compound • Loop always iterates at least once C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  21. do…while Looping (Repetition) Structure (continued) C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  22. do…while Looping (Repetition) Structure (continued) C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  23. Divisibility Test by 3 and 9 C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  24. The for Loop • The general form of the for statement is: for (initial statement; loop condition; update statement) statement; • The initial statement, loop condition, and update statementare called for loop control statements C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  25. C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  26. The for Loop (continued) • The for loop executes as follows: • initial statement is the first to be executed and is executed only once • loop condition is evaluated • If loop condition evaluates to true • Execute for loop statement • Execute update statement • Repeat previous step until the loop condition evaluates to false • The for loop executes indefinitely if the loop condition is always true C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  27. The for Loop (continued) • Fractional values can be used for loop control variables • A semicolon at the end of the for statement is a semantic error • In this case, the action of the for loop is empty C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  28. The for Loop (continued) • In a for statement, all three statements (initial statement, loop condition, and update statement) can be omitted • The following is a legal for loop: for(;;) cout<<"Hello"<<endl; C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  29. for Looping (Repetition) Structure (continued) C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  30. Nested Control Structures • To create the following pattern: * ** *** **** ***** • We can use the following code: for (i = 1; i <= 5 ; i++) { for (j = 1; j <= i; j++) cout << "*"; cout << endl; } C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  31. Nested Control Structures (continued) • What is the result if we replace the first for statement with the following? for (i = 5; i >= 1; i--) • Answer: ***** **** *** ** * C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  32. Choosing the Right Looping Structure • All three loops have their place in C++ • If you know or can determine in advance the number of repetitions needed, the for loop is the correct choice • If you do not know and cannot determine in advance the number of repetitions needed, and it could be zero, use a while loop • If you do not know and cannot determine in advance the number of repetitions needed, and it is at least one, use a do...while loop C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  33. break & continue Statements • break and continue alter the flow of control • The break statement is used for two purposes: • To exit early from a loop • To skip the remainder of the switch structure • After the break statement executes, the program continues with the first statement after the structure C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  34. Example: #include <iostream>using namespace std;int main(){ int num, sum; sum = 0; cout << "Enter numbers: "; cin >> num; while (cin) { if (num < 0) //if number is negative, terminate the loop { cout << "Negtive number found in the data." << endl; break; } sum = sum + num; cin >> num; } cout << endl; cout << "The sum is: " << sum << endl; return 0; } C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  35. break & continue Statements (continued) • continue is used in while, for, and do-while structures • When executed in a loop • It skips remaining statements and proceeds with the next iteration of the loop C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  36. Example:#include <iostream>using namespace std;int main(){ int num, sum; sum = 0; cout << "Enter numbers: "; cin >> num; while (cin) { if (num < 0) { cout << "Negtive number found in the data." << endl; cin >> num; continue; } sum = sum + num; cin >> num; } cout << endl; cout << "The sum is: " << sum << endl; return 0; } C++ Programming: From Problem Analysis to Program Design, Fourth Edition

More Related