Download presentation
Presentation is loading. Please wait.
Published byYanti Sasmita Modified over 5 years ago
1
ECE 103 Engineering Programming Chapter 12 More C Statements
Herbert G. Mayer, PSU CS Status 7/11/2015 Initial content copied verbatim from ECE 103 material developed by Professor Phillip PSU ECE
2
Syllabus Individual Statements Statement Blocks
3
Individual Statements
A statement is a valid combination of keywords and expressions that forms a complete action A C program consists of statements that are executed in sequence to accomplish a specified task Individual statements are terminated by a semicolon ; Syntax: statement; An additional semicolon by itself creates a NULL statement; null statements generate no code 2
4
Statement Blocks 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. 3
5
Variables can be declared inside a block:
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
6
5 #include <stdio.h> /* 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; 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 <stdio.h> /* 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; 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 5
7
#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 ); } //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. 6
8
7 #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 ); } //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 7
9
8 #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 ); } //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 10 B: 3 10 C: 5 20 D: 7 20 E: 7 30 F: 5 30 G: 1 30 8
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.