310 likes | 443 Vues
This review material, guided by Instructor Tina Tian, covers fundamental C++ concepts including input/output functions, data types, basic operators, control structures, and loops. Key topics such as the use of standard libraries, variable declaration, arithmetic operations, escape sequences, and boolean types are discussed, alongside practical examples like conditional statements and loops for user input validation. Ideal for students revisiting core C++ programming principles or preparing for more advanced topics in their courses.
E N D
CMPT 238 Data StructuresC++ Review Instructor: Tina Tian
Include Directives • #include <iostream> • Preprocessor directive • iostream is a library containing definitions of the input and output function • Linker • Appears at the start of the program, begin with # • using namespace std; • Tells the compiler to use names in iostreamin a “standard” way
int main() • { //beginning of the main function • .... //statements • return 0; • } //end of the program
The output function cout • c(C++ language)out(output) • c(C++ language)in(input) • cout<<whatever data you want to output; • cout<<“Welcome to CMPT102!”; • << represents the flow of the data.
Input Function cin • cout: output to screen • cin: input from keyboard • cout<<whatever you want to output; • cin>>variable_name; • cin>>variable1>>variable2; • Orcin>>variable1; • cin>>variable2;
Escape sequences • Start a new line • \n (need to go inside of the quotes) • endl • Escape sequence • \n new line • \t tab • \” “ • \\ \
Comments • // This is a comment. • /* • This is a comment. • This is another comment. • */
Combine Declaration and Assignment • int number = 3; • double rate = 0.07, balance = 0;
http://software.intel.com/en-us/articles/size-of-long-integer-type-on-different-architecture-and-oshttp://software.intel.com/en-us/articles/size-of-long-integer-type-on-different-architecture-and-os
The char Type • Short for character • Any single character from the keyboard • a letter, a digit, or punctuation mark • intnumber; char letter; • intnumber = 5;char letter = ‘a’; • char letter = ‘A’;
The Class string #include <string> charletter; string message; charletter = ‘a’; string message = “hello”; • Concatenation • string firstName = “Chuck”; • string lastName = “Norris”; • string name = firstName + lastName; //string name = firstName + “ “ + lastName;
The bool Type • Boolean • Two values • True • False • bool flag; • boolisPrime= false;
Constants • double pi = 3.14159; • const double PI = 3.14159; • constdouble CELSIUS_TO_FAHRENHEIT = 33.8; • Old version of C++ compilers: • #define PI 3.14159
Arithmetic Operators • +, -, *, / • int / int -> int • double / int-> double • 10/3, 5/2, 11/3 • (a+b)*c • (a-(b+c))*d
Modulus % • 17 % 5 = 2 • 23 % 2 = ? • 20 % 3 = ? • How to determine if a number is even/odd?
Multiwayif-else syntax • if (condition_1) • Action_1; • else if (condition_2) • Action_2; • ... • else if (condition_N) • Action_N; • else • Action_For_All_Other_Cases;
Comparison Operators • > greater than • < less than • >= greater than or equal to • <= less than or equal to • == equal to • != not equal to • a = 2; • if (a == 2)
The And Operator • If score is greater than 0and less than 100... • (score > 0) && (score < 100) • is true only if both are true
The Or Operator • (score1 >= 60) || (score2 >= 60) • is true if one or both are true
Syntax of the while Statement • while(Boolean_Expression) • { • Statement_1; // body • Statement_2; // iteration • ... • } • while(Boolean_Expression) • Statement;
Using the while Loop for Input Validation cout<<“Guess a number between 0 and 10: "; cin>>number; while((number <0) || (number > 10)) { cout<<"Invalid Entry!“;cout<<"Guess a number between 0 and 10: "; cin>>number; } //Start playing the game
Increment and Decrement Operators • Usually are used with int variables • count = count + 1; • count++; • count = count – 1; • count--;
For Loop Dissection • The for loop uses the same components as the while loop in a more compact form • for (int n = 1; n <= 10; n++) Initialization Action Update Action Boolean Expression
for(int n = 1; n < = 10; n = n+2) • for(int n = 0 ; n > -100 ; n = n-7) • for(int n = 0 ; n < 100 ; n = n*2) • for(double x = pow(y,3.0); x > 2.0; x = sqrt(x) )
Nested Loops • for (inti = 1; i <= 5; i++)// outer loop • { • for (int j = 1; j<=5; j++)// inner loop • { • cout<<i<<“ “<<j<<endl; • } • }
The break Statement • The break statement can be used to exit a loop. • Write a program to ask the user to enter 10 positive numbers and output their sum. • Terminate the loop when the user enters a negative value.
The continueStatement • Can use continue to go to end of loop and prepare for next repetition
Exercise: Pattern Displays • Write a C++ program that uses a loop to display the pattern below. • ++++++++++ • +++++++++ • ++++++++ • +++++++ • ++++++ • +++++ • ++++ • +++ • ++ • +
Next class • C++ Review • Functions • Arrays • Pointers • OOP
Homework • Review your old 102 assignments • Practice C++