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 Syllabus Individual Statements Statement Bocks Examples List of C Operators 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 2 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; statement; : } There is no need for a semicolon after the ending brace. 3 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 4 #include <stdio.h> #include <stdio.h> /* All variables in same scope */ int main( void ) { // main int x, y; /* Scope depends on block */ int main (void) { // main int x, y; x = 2; y = 3 * x; printf( "A: %d %d\n", 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 ); { // a new block int x; x = 4; y = 3 * x; printf( "B: %d %d\n", x, y printf( "C: %d %d\n", x, y ); return 0; } //end main ); } //end of block printf( "C: %d %d\n", x, y ); return 0; } //end main Output: Output: A: 2 6 B: 4 12 C: 4 12 A: 2 6 B: 4 12 C: 2 12 5 #include <stdio.h> /* 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 A variable's scope determines its visibility or accessibility from other parts of a program. ); } //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 6 #include <stdio.h> /* Scope depends on block */ int main( void ) { // main int x = 1, y = 10; printf( "A: %d %d\n", x, y ); A: 1 10 { // new block, level 2 int x = 3; B: 3 10 printf("B: %d %d\n", x, y); x = 5; y = 20; printf("C: %d %d\n", x, y); C: 5 20 { // new nested block, level 3 int x = 7; printf( "D: %d %d\n", x, y D: 7 20 ); y = 30; printf( "E: %d %d\n", x, y E: 7 30 ); } //end block level 3 printf( "F: %d %d\n", x, y ); } //end block level 2 F: 5 30 G: 1 30 printf( "G: %d %d\n", x, y ); return 0; } //end main 7 Table 1: C Operator Precedence Table in Decreasing Order L 1 2 Description Function call ( ) Array subscript [ ] Struct member . Struct dereference expr++ Decrement (post) expr-- Associativity L 5 left-to-right 6 Description Operator Bitwise left shift << Bitwise right shift >> Less than < Greater than > LT or equal to <= GT or equal to >= Equal to == Not equal to != Associativity left-to-right left-to-right Indirection * Reference (addr) & Unary plus + 8 Bitwise AND & left-to-right Unary minus - 9 Bitwise XOR ^ left-to-right Logical negation ! 10 Bitwise OR | left-to-right 11 Logical AND && left-to-right Bitwise NOT ~ 7 right-to-left left-to-right Increment (pre) ++expr 12 Logical OR || left-to-right Decrement (pre) --expr 13 Conditional ? : right-to-left 14 Assignment = += -= *= /= %= >>= <<= &= ^= |= right-to-left 15 Comma , left-to-right Size in bytes 4 -> Increment (post) Cast 3 Operator ( type ) sizeof Multiplication * Division / Modulo % Addition + Subtraction - left-to-right Highest precedence is Level 1. Lowest precedence is Level 15. left-to-right 8