Midterm Review
First Midterm Exam Friday, Oct 6th Closed book But.. you may bring a cheat sheet! One side of an A4-sized paper Partial access to computers
Part II (paper or computer based) Part I (paper based) Multiple choices Write the output of a program Part II (paper or computer based) Programming Visual Studio only You shouldn’t use the network
Variables a place to hold a number or data of other types Variable names Start with a letter or the underscore Remaining can be letters, numbers or underscore x x1 3X _abc %change data-1 PROG.CPP Big_Bonus C++ is case-sensitive rate RATE Rate
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; Or cin>>variable1; cin>>variable2;
http://software. intel http://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 int number; char letter; int number = 5; char letter = ‘a’; char letter = ‘A’;
The Class string #include <string> Concatenation char letter; string message; char letter = ‘a’; string message = “hello”; Concatenation string firstName = “Chuck”; string lastName = “Norris”; string name = firstName + lastName; //string name = firstName + “ “ + lastName;
getline() To read the whole line of input into a string string name; getline(cin, name);
The bool Type Boolean Two values bool flag; bool isPrime = false; True
Arithmetic Operators +, -, *, / int / int -> int double / int -> double 10/3, 5/2, 11/3
Modulus % 17 % 5 = 2 23 % 2 = ? 20 % 3 = ? How to determine if a number is even/odd?
Syntax for if-else if (condition) Yes_Statement else No_Statement Chooses between two alternative actions
The And Operator If score is greater than 0 and 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
The Not Operator !(score > 60) is equivalent to score <= 60
Nested Statements
Multiway if-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;
The Conditional Operator Can use to create short if/else statements Format: expr ? expr : expr; x<0 ? y=10 : z=20; First Expression: Expression to be tested 2nd Expression: Executes if first expression is true 3rd Expression: Executes if the first expression is false
Type Casting static_cast<Type>(Value) double -> int static_cast<int>(9.2) static_cast<int>(number)
switch Statement Syntax switch (Controlling_Expression) { case result_1: Statement_Sequence_1; break; case result_2: ... case result_n: Statement_Sequence_n; default: Default_Statement_Sequence; }
Syntax of the while Statement while (Boolean_Expression) { Statement_1; // body Statement_2; // iteration ... } Statement;
The do-while Statement { Statement_1; Statement_2; ... } while (Boolean_Expression); Statement; while (Boolean_Expression);
Announcements Homework 3 is out No class on Tuesday, Oct 10 Due on Friday, Oct 13 No class on Tuesday, Oct 10 Follows a Monday schedule