Fundamentals of C++ Programming - 03: Control Structures (part 1) - Dustin Nguyen, PhD

Understand basic control structures in C/C++
❖ if-else statement
❖ switch statement
❖ Solve the problem using conditional executions
❖ Implement if-else, switch-case statements 
pdf 36 trang xuanthi 27/12/2022 2940
Bạn đang xem 20 trang mẫu của tài liệu "Fundamentals of C++ Programming - 03: Control Structures (part 1) - Dustin Nguyen, PhD", để tải tài liệu gốc về máy hãy click vào nút Download ở trên.

File đính kèm:

  • pdffundamentals_of_c_programming_03_control_structures_part_1_d.pdf

Nội dung text: Fundamentals of C++ Programming - 03: Control Structures (part 1) - Dustin Nguyen, PhD

  1. Outcomes ❖ Understand basic control structures in C/C++ ❖ if-else statement ❖ switch statement ❖ Solve the problem using conditional executions ❖ Implement if-else, switch-case statements 2
  2. Conditional execution
  3. Conditional execution ❖ Type bool: true/false ❖ Size: 1 byte (basic unit of storage) ❖ Be represented as integer: true = 1, false = 0 ❖ What happens when you assign a value to boolean type: ❖ False: 0 value (for integer, floating point number, character ‘\0’) ❖ True: anything else (except structures, unless a casting operator is defined) 6
  4. Conditional execution ❖ Logic operators Operator Meaning ! not && and || or 8
  5. If-else statement
  6. If-else statement ❖ Flowchart simple if statement N Y statement statement 12
  7. If-else statement ❖ Examples #include #include int main() { int main() { float a, b, c, delta; float a, b, c, delta; cout > a >> b >> c; cin >> a >> b >> c; delta = b * b - 4 * a * c; delta = b * b - 4 * a * c; if (delta < 0) { if (delta < 0) cout << “Delta value is negative” << endl; cout << “Delta value is negative” << endl; cout << "Have no real root" << endl; cout << "Have no real root" << endl; } return 0; return 0; } } 14
  8. If-else statement ❖ Flowchart full if-else statement N Y if-true if-false statement statement statement 16
  9. Nested conditionals ❖ Nested if-else statements ❖ if ( ) // first check if ( ) // second check if ( ) // third check else else else if ( ) else if ( ) else 18
  10. Switch statement
  11. Switch statement ❖ Flowchart N N N Y Y Y case 1 case 2 case 3 defaults statements statements statements statement 22
  12. Switch statement ❖ Example: #include #include int main() { int main() { // Do something // Do something int usrChoice; int usrChoice; cout > usrChoice; cin >> usrChoice; if (usrChoice 4) switch (usrChoice) { cout << “Cannot recognise your choice” << endl; case 1: else switch (usrChoice) { cout << “pick 1” << endl; case 1: cout << “action 1” <<endl; break; cout << “pick 1” << endl; case 2: cout << “action 1” <<endl; cout << “pick 2” << endl; case 2: cout << “action 2” << endl; break; cout << “pick 2” << endl; case 3: cout << “action 2” << endl; cout << “pick 3” << endl; case 3: cout << “action 3” <<endl; break; cout << “pick 3” << endl; case 4: cout << “action 3” <<endl; cout << “pick 4” << endl; default: cout << “action 4” << endl; break; cout << “pick 4” << endl; default: cout << “action 4” << endl; cout << “Cannot recognise your choice” << endl; } } return 0; return 0; } } 24
  13. Enumerated type ❖ Define a list of possible values of a type ❖ enum { , , }; ❖ enum [ ] { } ; ❖ Example: ❖ enum Color {Red = -1, Orange = 2, Yellow = 8, Green = 3, Blue, Violet}; Color c = Blue; out << “Blue color has value: ” << c << endl; 26
  14. If-else vs. switch ❖ switch statement can be represented by a sequence of if-else statements ❖ if-else statement is more general ❖ All control structures can be represented by if-else statements and goto statements (with the support from labels). 28
  15. Conditional operator ❖ Safety: unless you understand how your code works (including compiler mechanism), write safe code. ❖ Example: ❖ char outChar = (a == ‘c’)? ‘C’: ‘c’; ❖ float diff = (x > y)? (x - y): (y - x); ❖ float r = ((a ^ b) & 0x0010)? (x - (a == b? 1.5: -1.5)): (y - x); 30
  16. Preprocessor directives ❖ Conditional Pre-processor directives: ❖ #define, #undef, #ifdef, #ifndef, #else, #elif, #endif ❖ E.g.: ❖ int foo(float a, double b) { #ifdef __MSC_VER return a * 3.14159 - sqrt(b * a); #else return a * 3.14159 + b * b; #endif } 32
  17. Preprocessor directives ❖ Power of macros and preprocessor directive ❖ One definition fit all ❖ Flexible, portable ❖ Open source community 34
  18. Quiz & homework ❖ Use if-else statement to write the program that asks user to input three real values. Compare those values and tell user, which number is the maximum, minimum, and median. ❖ Write a program to solve the quadratic equation. Tell user all possible roots, including complex roots. 36