Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review 1.

Similar presentations


Presentation on theme: "Review 1."— Presentation transcript:

1 Review 1

2 Structure of a C++ program
include necessary header files every C++ program should have a main function as the entrance! the body of the function enclosed by “{ }” How many stuff can we put under the main function??

3 Is there any issue of the following C++ code?
void main() { cout << “No, I can be compiled and run! ; }

4 Is there any issue of the following C++ code?
#include <iostream> using namespace std; missing a header file void main() { cout << “No, I can be compiled and run! ; } Is a header file always needed ? Is the header file always be #include <iostream> ?

5 Does the following code need a header file?
void main() { } Is a header file always needed ? Is the header file always be #include <iostream> ?

6 Does the following code need a header file?
void main() { } Is a header file always needed ? Yes and no! An empty main() does not need a header. But all meaningful programs do not have empty body! Is the header file always be #include <iostream> ? No. depending on what you need from the C++ library

7 Is there any issue of the following C++ code?
#include <iostream> using namespace std; void main() { cout << “No, I can be compiled and run! ; }

8 Is there any issue of the following C++ code?
#include <iostream> using namespace std; void main() { cout << “No, I can be compiled and run!” ; } the string should be put in “ ”!! How to accept the user input from keyboard? Can the user input other information than just numbers?

9 Is there any issue of the following C++ code?
#include <iostream> using namespace std; void main() { cout << “No, I can be compiled and run!” ; } the string should be put in “ ”!! How to accept the user input from keyboard? cin >> [variable]; Can the user input other information than just numbers? Yes!

10 Is there any issue of the following C++ code?
#include <iostream> #include <string> using namespace std; int main() { string name; cout << “Please input your name! \n” ; cin >> name; cout << “Hello, “ << name << endl; return 0; } the string should be put in “ ”!! How to accept the user input from keyboard? cin >> [variable]; Can the user input other information than just numbers? Yes!

11 if-else Statement Syntax
Formal syntax: if (<boolean_expression>) <yes_statement> else <no_statement> Note each alternative is only ONE statement! To have multiple statements execute in either branch  use compound statement

12 Multiway if-else Not new, just different indenting
Avoids "excessive" indenting Syntax: Copyright © 2016 Pearson Inc. All rights reserved.

13 What will be the output of this program?
#include <iostream> using namespace std; int main() { int x = 0; cout << "Please enter x: \n"; if (x == 1) { cout << "x is 1"; } else if (x == 2) { cout << "x is 2"; else { cout << "value of x unknown";

14 How about now? #include <iostream> using namespace std; int main() { int x = 0; cout << "Please enter x: \n"; if (x == 1) { cout << "x is 1"; }

15 How about now? #include <iostream> using namespace std; int main() { int x = 0; cout << "Please enter x: \n"; cin >> x; if (x == 1) { cout << "x is 1"; } else if (x == 2) { cout << "x is 2"; else { cout << "value of x unknown";

16 Other examples…

17 switch Statement Syntax
The controlling expression must be integral! This includes char. Copyright © 2016 Pearson Inc. All rights reserved.

18 The switch Statement in Action
Copyright © 2016 Pearson Inc. All rights reserved.

19 Can you rewrite the following code using the switch statement?
#include <iostream> using namespace std; int main() { int x = 0; cout << "Please enter x: \n"; cin >> x; if (x == -1) { cout << "x cannot be negative!"; } else if (x == 0) cout << "Draw red."; else if (x == 1) cout << "Draw blue."; else if (x == 2) { cout << "Draw green"; else { cout << "value of x unknown";

20 #include <iostream>
using namespace std; int main() { int x = 0; cout << "Please enter x: \n"; cin >> x; switch(x) case -1: cout << "x cannot be negative!"; break; case 0: cout << "Draw red."; case 1: cout << "Draw blue."; case 2: cout << "Draw green."; default: cout << "value of x unknown"; }

21 What will be the output of the following code?
#include <iostream> using namespace std; int main() { int x = 0; cout << "Please enter x: \n"; switch(x) case 0: cout << "Draw red."; case -1: cout << "x cannot be negative!"; break; case 1: cout << "Draw blue."; case 2: cout << "Draw green."; default: cout << "value of x unknown"; }

22 What will be the output of the following code?
#include <iostream> using namespace std; int main() { int x = 2; cout << "Please enter x: \n"; switch(x) case 0: case 1: cout << "Draw blue."; break; case 2: cout << "Draw green."; default: cout << "value of x unknown"; }

23 switch Menu Example Switch stmt "perfect" for menus: switch (response) { case 1: // Execute menu option 1 break; case 2: // Execute menu option 2 break; case 3: // Execute menu option 3 break; default: cout << "Please enter valid response."; }

24 Loops Why do we need loops?

25 Loops 3 Types of loops in C++ while do-while for Most flexible
No "restrictions" do-while Least flexible Always executes loop body at least once for Natural "counting" loop

26 for Loop Syntax for (Init_Action; Bool_Exp; Update_Action)
Body_Statement Like if-else, Body_Statement can be a block statement Much more typical Good for the situations that the exact or maximum number of iterations is known!!! Example: compute the sum of numbers 1,2, 3,…, 100

27 #include <iostream>
using namespace std; int main() { int x = 0; int i; for (i=1; i<=100; i++) x += i; } cout << "the sum of 1 to 100 is " << x << endl;

28 while Loops Syntax

29 while Loop Example Consider: count = 0; // Initialization while (count < 3) // Loop Condition { cout << "Hi "; // Loop Body count++; // Update expression } Loop body executes how many times?

30 do-while Loop Syntax

31 do-while Loop Example count = 0; // Initialization do { cout << "Hi "; // Loop Body count++; // Update expression } while (count < 3); // Loop Condition Loop body executes how many times? do-while loops always execute body at least once!

32 #include <iostream>
using namespace std; int main() { int x = 0; int i; for (i=1; i<=100; i++) x += i; } cout << "the sum of 1 to 100 is " << x << endl; Can you implement the above for loop example using while or do-while mechanism?

33 The break and continue Statements
Flow of Control Recall how loops provide "graceful" and clear flow of control in and out In RARE instances, can alter natural flow break; Forces loop to exit immediately. continue; Skips rest of loop body These statements violate natural flow Only used when absolutely necessary!

34 Nested Loops Recall: ANY valid C++ statements can be inside body of loop This includes additional loop statements! Called "nested loops" Requires careful indenting: for (outer=0; outer<5; outer++) for (inner=7; inner>2; inner--) cout << outer << inner; Notice no { } since each body is one statement Good style dictates we use { } anyway

35 Arrays Why do we need arrays?

36 Declaring Arrays Declare the array  allocates memory int score[5];
Declares array of 5 integers named "score" Similar to declaring five variables: int score[0], score[1], score[2], score[3], score[4] Individual parts called many things: Indexed or subscripted variables "Elements" of the array Value in brackets called index or subscript Numbered from 0 to size - 1

37 Accessing Arrays Access using index/subscript
cout << score[3]; Note two uses of brackets: In declaration, specifies SIZE of array Anywhere else, specifies a subscript Size, subscript need not be literal int score[MAX_SCORES]; score[n+1] = 99; If n is 2, identical to: score[3]

38 Array Usage Powerful storage mechanism Can issue command like:
"Do this to ith indexed variable" where i is computed by program "Display all elements of array score" "Fill elements of array score from user input" "Find highest value in array score" "Find lowest value in array score"


Download ppt "Review 1."

Similar presentations


Ads by Google