3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (5) 10 September.

Slides:



Advertisements
Similar presentations
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Advertisements

While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Computer Science 1620 Loops.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Chapter 4 Program Control Statements
True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions: && (and),
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
CPS120 Introduction to Computer Science Iteration (Looping)
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
CONTROLLING PROGRAM FLOW
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
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.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
1 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX do { Statement } while.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 7 Additional Control Structures. Chapter 7 Topics l Switch Statement for Multi-Way Branching l Do-While Statement for Looping l For Statement.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
Chapter 05 (Part III) Control Statements: Part II.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Fundamental Programming Fundamental Programming for Loops.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Control Structures RepetitionorIterationorLooping Part I.
CPS120 Introduction to Computer Science Iteration (Looping)
1 CS161 Introduction to Computer Science Topic #8.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Chapter five exercises. a. false; b. true; c. false; d. true; e. true; f. true; g. true; h. false.
Computer Programming -1-
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Review 1.
Chapter 3 Control Statements
REPETITION CONTROL STRUCTURE
生查子 ~ 歐陽修 去年元夜時,花市燈如晝, 月上柳梢頭,人約黃昏後; 今年元夜時,月與燈依舊, 不見去年人,淚濕春衫袖。
Engineering Problem Solving with C++, Etter/Ingber
Chapter 2.2 Control Structures (Iteration)
Programming Fundamentals
לולאות קרן כליף.
Introduction to Functions
Chapter 8 Repetition Statements
Compound Assignment Operators in C++
Additional Control Structures
Chapter 7 Additional Control Structures
Chapter 2.2 Control Structures (Iteration)
Computing Fundamentals
Control Structures Part 2
Chapter 4: Control Structures I (Selection)
Control Structures Part 1
do/while Selection Structure
CS150 Introduction to Computer Science 1
Presentation transcript:

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (5) 10 September 2008

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C Program Flow Control

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 3 if (cond) // if the cond { doA(); // is true, } // then do doA() else // else do doB() { doB(); } if... else Control of Program Flow Normal program execution is performed from top-to-down and statement-by-statement. It is often that the program modifies the program flow depending on some conditions set by the programmer or user. C++ provides many approaches to control program flow. switch (expression) { case value1: doA(); break; case value2:doB(); break; } switch... case... break

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 4 #include using namespace std; int main() { int firstNum, secondNum = 10; cout << "Please enter: \n"; cin >> firstNum; cout << "\n\n"; if (firstNum >= secondNum) {if ((firstNum% secondNum) == 0) { if (firstNum == secondNum) cout << "They are the same!\n"; else cout << "They are evenly divisible!\n"; } else cout << "They are not evenly divisible!\n"; } else cout << "Hey! The second no. is larger!\n"; return 0; } Nested if... else

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 5 Using braces with if … else #include using namespace std; int main() { int x; cout 100: "; cin >> x; cout << "\n"; if (x >= 10) if (x > 100) cout << "More than 100, thanks!\n"; else// not the else intended! cout << "Less than 10, thanks!\n"; return 0; } ? ? What’s wrong with this program? How do you solve the problem? What’s wrong with this program? How do you solve the problem? { }

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 6 switch … case … break #include using namespace std; int main() { unsigned short int number; cout << "Enter a number between 1 and 5: "; cin >> number; switch (number) {case 0:cout << "Too small, sorry!"; break; case 3:cout << "Excellent!\n";//falls through case 2:cout << "Masterful!\n";//falls through case 1:cout << "Incredible!\n"; break; default:cout << "Too large!\n"; break; } cout << "\n\n"; return 0; } Only accept number or expression that returns a number See the differences between break and without break Do the default if all tests fail.

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 7 Implement switch with if … else if (num == 0) cout << "Too small, sorry!"; else { if (num == 3 || num == 2 || num == 1) {cout << "Incredible!\n"; if (num == 3 || num == 2) { cout << "Masterful!\n"; if (num == 3) cout << "Excellent!\n"; } else cout << "Too large!\n"; } switch … case statement can be implemented by the if … else statement but with more complicated structure. However, switch … case can only be applied to simple numerical tests.

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 8 Looping Many programming problems are solved by repeatedly acting on the same data. The method for achieving repeated execution is by looping. C++ provides many approaches to implement looping if … goto // old way, strongly NOT recommended while loop// use when the testing parameters are // complicated do-while loop // use when the repeated part is expected // to be executed at least once for loop// use when the testing parameters are // simple

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 9 while Loops #include using namespace std; int main() { int counter = 0;//initialize counter while (counter<5) { counter++;//top of the loop cout << "counter: " << counter << "\n"; } cout << "Complete. Counter: " << counter << ".\n"; return 0; } while loops do the looping until the testing condition fails. Test condition ( tested variable must change inside the loop ) Repeat this part until counter >= 5 5

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 10 More complicated while Loops while loops can be used with a more complicated testing condition. #include using namespace std; int main() { unsigned long small, large; unsigned short const MAXSMALL = ; cin>>small; cin>>large; while (small 0 && small < MAXSMALL) { if (small % 5000 == 0) cout << ".\n"; // write a dot every 5000 small++; large -= 2; } return 0; } 3 tests for each loop Testing parameters are updated in every loop

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 11 do … while while loops do the test first and then the loop. Sometimes we would like to have the loop to be done at least once. In this case, do … while can be used. #include using namespace std; int main() {int counter; cout << "How many hellos? "; cin >> counter; do {cout << "Hello\n"; counter--; }while (counter > 0); cout << "Counter is: " << counter << endl; return 0; } Hello will be shown at least once even if user enters 0 for counter

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 12 for loops In many loops, we initialize a testing parameter, modify the value of the parameter and test the parameter. It can be done in a single line by a for loop. #include using namespace std; int main() { int counter; for (counter=0; counter<5; counter++) cout << "Looping! "; //counter then increment cout << "\nCounter: " << counter << ".\n"; return 0; } Initialize Modify Test 5

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 13 Different varieties of for loops #include using namespace std; int main() { for (int i=0, j=0; i<3; i++, j++) cout << "i: " << i << " j: " << j << endl; return 0; } Multiple initialization and increments #include using namespace std; int main() { int counter = 0; for (; counter < 5;) {counter++; cout<<"Looping! "; } cout << "\nCounter: " << counter << ".\n"; return 0; } Null initialization and increments

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 14 Different varieties of for loops #include using namespace std; int main() { int counter = 0;// initialization int max; cout << "How many hellos?"; cin >> max; for (;;) // a for loop that doesn't end { if (counter < max)// test { cout << "Hello!\n"; counter++;// increment } else break; //end for loop } return 0; } Empty for loop

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 15 continue and break Keywords continue and break allow one to change the program flow during looping. Should be used with caution since it will make the program hard to understand owing to the sudden change of direction. #include using namespace std; int main() { int counter; for (counter=0; counter<5; counter++) {cout << "Looping! counter is " << counter <<"\n"; if ((counter%2) == 1) //odd number continue; cout << "Counter is an even number.\n"; } return 0; } Execution of continue will skip the following part of the loop but not leaving the loop

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 16 Nested for loops #include using namespace std; int main() {int rows, columns; char theChar; cout << "How many rows? "; cin >> rows; cout << "How many columns? "; cin >> columns; cout << "What characters? "; cin >> theChar; for (int i=0; i<rows; i++) {for (int j=0; j<columns; j++) cout << theChar; cout << "\n"; } return 0; } Nested for loop Will be executed ( rows  columns ) times More explanation on next page

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 17 Value of i, j for (int i=0; i<rows; i++) {for (int j=0; j<columns; j++) cout << theChar; cout << "\n"; } Output on the screen: Assumption: rows = 2 columns = 3 theChar = x Assumption: rows = 2 columns = 3 theChar = x i = 0 j = ? i = 0 j = 0 x i = 0 j = 1 x i = 0 j = 2 x i = 0 j = 3 i = 1 j = 3 i = 1 j = 0 i = 1 j = 1 i = 1 j = 3 i = 1 j = 2 i = 1 j = 0 x i = 1 j = 1 x i = 1 j = 2 x  i = 0 j = 3  i = 1 j = 3 i = 2 j = 3

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 18 If a variable is defined in the initialization part of the for loop, the variable will no longer exist on leaving the for loop. It is not an error for Visual Studio.NET 2003.

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 19 Exercise 3.4 For the program on p. 16, a.Build the project and note the result. b.Try to rewrite the program using nested while loops instead of the nested for loops. Which program is more complicated?