




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Material Type: Notes; Class: C++ Computer Science I; Subject: Computer Science; University: Oakton Community College; Term: Fall 2009;
Typology: Study notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!
Page 279 6.2 Selection: The if Statement 285 The if Statement (General Form) 286 Understanding the Multibranch if 286 Pitfall: The Dangling-else Problem 287 Pitfall: Confusing = and == 289 6.3 Selection: The switch Statement 292 Example: Temperature Conversions 292 Object-Centered Design 293 Form of the switch Statement 297 The break Statement 298 Drop-Through Behavior 298 Example: Converting Numeric Codes to Names 299 Object-Centered Design 300 Cases With no Action 302 Choosing the Proper Selection Statement 303 6.5 Selection: Conditional Expressions 312 The Conditional Expression 313 Part of the Picture: Boolean Logic and Digital Design 315 Early Work 315 Digital Circuits 317 Circuit Design: A Binary Half-adder 317 Part of the Picture: Computer Architecture 320 Additionally<6.6> Mutators and Input Methods
Q. What is std? A. std is a namespace for C++ standard libraries Q. What is a namespace? A. A namespace is a set of related names (variables, constants, functions, classes, etc). Inside some namespace A the names can be used as is, for example, variable x. Outside the namespace A it is necessary either to use qualified names, for example, A::x, or include using statement. Q. What is using namespace std? A. After this statement it is possible to refer to C++ standard library names without namespace qualifier, for example, just cout instead of std::cout.
Q. How to look what is inside std? A. There is no easy way. You can look in the C++ Standard. You also can check inside include files. For example,
Now /* a.hpp (header file) / // the same namespace Kamilla { int a ( int x); // function a declaration; } /*************end of a.hpp file***********/ Now, b.cpp file will look as /b.cpp file*/ #include
if (boolean_expression1) statement else if (boolean_expression2) statement else if (boolean_expression3) statement else if (boolean_expression4) statement else statement the same as if (boolean_expression1) statement else if (boolean_expression2) statement else if (boolean_expression3) statement else if (boolean_expression4) statement else statement Dangling –Else Problem:solution: In a nested if statement, an else is matched with the nearest preceding unmatched if. To escape ambiguity enclose statement in curly braces { } (block) Confusing = and == = is assignment operator == is boolean operator comparison 6.3 The Switch Statement Form: switch (expression) { case_list1 : statement_list1; case_list2 : statement_list2; ………………… case_listn : statement_listn; default : statement_listn+1; }
char conversion; int result; cin >> conversion; switch ( conversion ) { case ‘A’ : case ‘a’ : result =5; cout << result<< endl; case ‘B’ : result=6; cout << result<<endl; default : result =0; cout << result<<endl; } Output if conversion ==’A’or ‘a’ Is 5 6 0 Output if conversion ==’B’ Is 6 0 Output if conversion another char Is 0 6.5 Selection: Conditional Expressions Form: condition? expression1 : expression where condition is a Boolean expression; and expression1 and expression2 are type-compatible expressions. Behavior : condition is evaluated. If the value of condition is true (i.e., nonzero), Then the value expression1 is returned as the result. If the value of condition is false (i.e., zero), Then the value expression2 is returned as the result. In conditional expression, only one of expression1 and expression2 is evaluated Example1: reciprocal = ( (x==0)? 0 : 1/x ); // is safe because if x is 0 1/x will not be evaluated
Example2: Function int largeOf (int value1, int value2) { if (value1 > value2) return value1; else return value2; } A conditional expression provides a simpler alternative: int largeOf (int value1, int value2) { return ( ( value1 > value2)? value1 : value2): }