Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.

Similar presentations


Presentation on theme: "CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim."— Presentation transcript:

1 CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim from ECE 103 material by prof. Phillip Wong @ PSU

2 Syllabus Individual Statements Statement Bocks Examples List of C Operators

3 2 Individual C++ Statements A statement is a valid combination of keywords and expressions that forms a complete action Expressions consists of operands and combining operators; list of operators below Statements are executed in sequence to accomplish a specified task Individual statements are terminated by semicolon ; An additional semicolon by itself creates a null statement; null statements generate no code

4 3 A statement block (AKA a compound statement) is a sequence of 0 or more statements grouped together { // opening brace, marks the start of the block } // ending brace, marks the end of the block Syntax: { statement; : } There is no need for a semicolon after the ending brace.

5 4 Variables declared outside a block can be accessed from inside the block, unless overridden; they are said to be global to the inner scope Variables can be declared inside a block:  These variables have local scope  Inside the block, a local variable takes precedence over a variable having the same name from outside the block; i.e. a search for names progresses from inner toward outer scopes

6 5 #include /* All variables in same scope */ int main( void ) { // main int x, y; x = 2; y = 3 * x; printf( "A: %d %d\n", x, y ); x = 4; y = 3 * x; printf( "B: %d %d\n", x, y ); printf( "C: %d %d\n", x, y ); return 0; } //end main Output: A: 2 6 B: 4 12 C: 4 12 #include /* Scope depends on block */ int main (void) { // main int x, y; x = 2; y = 3 * x; printf( "A: %d %d\n", x, y ); { // a new block int x; x = 4; y = 3 * x; printf( "B: %d %d\n", x, y ); } //end of block printf( "C: %d %d\n", x, y ); return 0; } //end main Output: A: 2 6 B: 4 12 C: 2 12

7 6 #include /* Scope depends on block */ int main( void ) { // main int x = 1, y = 10; printf( "A: %d %d\n", x, y ); { // new block, level 2 int x = 3; printf("B: %d %d\n", x, y); x = 5; y = 20; printf("C: %d %d\n", x, y); { // new nested block, level 3 int x = 7; printf( "D: %d %d\n", x, y ); y = 30; printf( "E: %d %d\n", x, y ); } //end boxck level 3 printf( "F: %d %d\n", x, y ); } //end block level 2 printf( "G: %d %d\n", x, y ); return 0; } //end main A variable's scope determines its visibility or accessibility from other parts of a program.

8 7 #include /* Scope depends on block */ int main( void ) { // main int x = 1, y = 10; printf( "A: %d %d\n", x, y ); { // new block, level 2 int x = 3; printf("B: %d %d\n", x, y); x = 5; y = 20; printf("C: %d %d\n", x, y); { // new nested block, level 3 int x = 7; printf( "D: %d %d\n", x, y ); y = 30; printf( "E: %d %d\n", x, y ); } //end block level 3 printf( "F: %d %d\n", x, y ); } //end block level 2 printf( "G: %d %d\n", x, y ); return 0; } //end main A: 1 10B: 3 10C: 5 20F: 5 30E: 7 30D: 7 20G: 1 30

9 8 Table 1: C Operator Precedence Table in Decreasing Order LDescriptionOperatorAssociativityLDescriptionOperatorAssociativity 1 Function call ( ) left-to-right 5 Bitwise left shift << left-to-right Array subscript [ ] Bitwise right shift >> Struct member. 6 Less than < left-to-right Struct dereference -> Greater than > Increment (post) expr++ LT or equal to <= Decrement (post) expr-- GT or equal to >= 2 Indirection * right-to-left 7 Equal to == left-to-right Reference (addr) & Not equal to != Unary plus + 8Bitwise AND & left-to-right Unary minus - 9Bitwise XOR ^ left-to-right Logical negation ! 10Bitwise OR | left-to-right Bitwise NOT ~ 11Logical AND && left-to-right Increment (pre) ++expr 12Logical OR || left-to-right Decrement (pre) --expr 13Conditional ? : right-to-left Cast ( type ) 14Assignment = += -= *= /= %= >>= <<= &= ^= |= right-to-left Size in bytes sizeof 3 Multiplication * left-to-right 15Comma, left-to-right Division / Highest precedence is Level 1. Lowest precedence is Level 15. Modulo % 4 Addition + left-to-right Subtraction -


Download ppt "CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim."

Similar presentations


Ads by Google