Presentation is loading. Please wait.

Presentation is loading. Please wait.

Department of Electronic & Electrical Engineering Statements Blocks{} Semicolons ; Variables Names keywords Scope.

Similar presentations


Presentation on theme: "Department of Electronic & Electrical Engineering Statements Blocks{} Semicolons ; Variables Names keywords Scope."— Presentation transcript:

1 Department of Electronic & Electrical Engineering Statements Blocks{} Semicolons ; Variables Names keywords Scope

2 Department of Electronic & Electrical Engineering int x=5; int y=6; int z; z=x*2; x = y + z; printf("X"); Statements and semi colons. A statement is the smallest standalone element of a program. In C a statement must be terminated with a semicolon. examples: C ignores new lines and spaces (Very bad style but legal) declarations assignments function calls

3 Department of Electronic & Electrical Engineering Blocks and {} Curly brackets {} can be used to form a CODE BLOCK. Blocks are use to: Create a compound statement. Contain the body of a function. Blocks can be nested. { { } { } }

4 Department of Electronic & Electrical Engineering Compound Statements. if ( x>y) { printf(" x>y \n"); x=y; } Note that and if acts on a single statement if ( x>y) printf(" x>y \n"); x=y; /* not part of the if. Always executed. */

5 Department of Electronic & Electrical Engineering Function body void foo(int x, int y) { if ( x>y) { printf(" x>y " \n); x=y; } function body More about functions later...

6 Department of Electronic & Electrical Engineering Comments, Types Variables and Constants. { /* Declare some variables */ /*(must be at the start of a block) */ int i; float x,y,z; char c; /* Some assignment statement */ i=5; /* 5 is an integer constant */ x=2.0; /* 2.0 is a floating point constant */ y=3.0; c='X'; /* single quotes for a char */ z=x*y*4.0; /* x*y*4.0 is an expression */ } Comments delimited by /*.... */ Type Name Assignments Expressions

7 Department of Electronic & Electrical Engineering Demo: Run this with the debugger. int main() { /* Declare some variables */ /*(must be at the start of a block) */ int i; float x,y,z; char c; /* Some assignment statement */ i=5; /* 5 is an integer constant */ x=2.0; /* 2.0 is a float constant */ y=3.0; c='X'; /* single quotes for a char */ z=x*y*4.0; /* x*y*4.0 is an expression */ }

8 Department of Electronic & Electrical Engineering Examining variables using the debugger. Run the previous code to the i=5 line using the debugger A window appears showing the values of your variables. Note the variables are random values (whatever is in the memory). Step through the code and watch the values change!

9 Department of Electronic & Electrical Engineering Names for Variables and Functions In C you must declare variables/functions before they used. e.g. { int x; float z; ……. x and z are names.

10 Department of Electronic & Electrical Engineering Rules for Variable names Rules for constructing variable names Characters can be alphabets (a-z,A-Z), digits (0-9) and underscores ( _ ). Start with alphabet or underscore (not a digit). C keywords are not allow e.g. if for do int...

11 Department of Electronic & Electrical Engineering These keywords can not be used as names. All the keywords of the C language!

12 Department of Electronic & Electrical Engineering { int x; int z=4; x=3; { int x; /* a new variable */ int y=z; x=4; } /* what is x here ? */ y=5; } Declaring variables, nested blocks, scope. Hides the previous x BAD IDEA compile time error ! y does not exists here. The part of a program in which you can use a variable is called it's scope. you can see variables declared in a containing block.

13 Department of Electronic & Electrical Engineering Scope. Global: Variables declared outside functions can be seen every where (unless hidden by name reuse) Local: Variables declared within curly brackets are not visible outside the block. int x; /* global */ void main() { int x; /* local */ }

14 Department of Electronic & Electrical Engineering Statements smallest stand alone part of your code. Blocks{} create a compound statement from a number of other statements. Semicolons ; marks end of statements

15 Department of Electronic & Electrical Engineering Names labels for variables and functions Scope extent of a program in which a variable can be seen.


Download ppt "Department of Electronic & Electrical Engineering Statements Blocks{} Semicolons ; Variables Names keywords Scope."

Similar presentations


Ads by Google