Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Syllabus - C++ Computer Science I | CSC 155, Study notes of Computer Science

Material Type: Notes; Class: C++ Computer Science I; Subject: Computer Science; University: Oakton Community College; Term: Fall 2009;

Typology: Study notes

Pre 2010

Uploaded on 08/04/2009

koofers-user-t5y
koofers-user-t5y 🇺🇸

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C++ CSC155 11/28/2020 Page 1 of 8
C++ Lecture 8 (week 8)
Chapter 6 Selection
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
About namespace
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.
pf3
pf4
pf5
pf8

Partial preview of the text

Download Syllabus - C++ Computer Science I | CSC 155 and more Study notes Computer Science in PDF only on Docsity!

C++ Lecture 8 (week 8)

Chapter 6 Selection

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

About namespace

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, header is probably in /usr/include/g++/string file. Let us consider not just standard library std but some other library, for example Kamilla /* library Kamilla / namespace Kamilla { some C++ code } Example1: If we would like to use std namespace names we can write using namespace std; and than use functions from std If we do not write using namespace std , we have to write std::cin>> x>> endl ; std:: function name every time when we use a function from the std library. Example 2: 2 files / a.cpp contains library Kamilla, that contains function a definition / namespace Kamilla { int a ( int x) { // function a definition return x2; } } /* b.cpp has function B that uses function a from library Kamilla */ #include /************************************/ namespace Kamilla { int a ( int x); // function a declaration; } /*************************************/ void b (int y) { std::cout << Kamilla::a(y); } Example 3:

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 #include “a.hpp” using namespace std; using namespace Kamilla; void b(int y) { cout << a(y); //instead of std::cout << Kamilla::a(y); } 6.2 if Statement if (boolean_expression) statement Or if (boolean_expression) statement else statement where if and else is keywords; and statement1, statement2 are C++ statements(either simple or compound). Purpose: If the boolean_expression is true, then statement1 is executed and statement2 is bypasses, if present. If the boolean_expression is false, then statement1 is bypassed and statement2 is executed, if present. In either case, execution continues with next statement in the program. If statement1 and/or statement2 another if-else statement == multibranch form Series of nested if statement

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): }