ECE 103 Engineering Programming Chapter 12 More C Statements

Slides:



Advertisements
Similar presentations
Selection Feature of C: Decision making statements: It allow us to take decisions as to which code is to be executed next. Since these statements control.
Advertisements

Introduction to C Programming
C++ Statements represent the lowest-level building blocks of a program and it may be like:. A simple statement is a computation terminated by a semicolon.
ECE 103 Engineering Programming Chapter 54 Recursion Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Local Variables and Scope Benjamin Fein. Variable Scope A variable’s scope consists of all code blocks in which it is visible. A variable is considered.
1 CS 161 Introduction to Programming and Problem Solving Chapter 9 C++ Program Components Herbert G. Mayer, PSU Status 10/20/2014.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
C++ for Engineers and Scientists Third Edition
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
ECE 103 Engineering Programming Chapter 10 Variables, AKA Objects Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
ECE 103 Engineering Programming Chapter 48 Typedef and Enum Type Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
PL / SQL By Mohammed Baihan. What is PL/SQL? PL/SQL stands for Procedural Language extension of SQL. PL/SQL is a combination of SQL along with the procedural.
ECE 103 Engineering Programming Chapter 24 Sorting Herbert G. Mayer, PSU CS Status 6/2/2015 Initial content copied verbatim from ECE 103 material developed.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
 2007 Pearson Education, Inc. All rights reserved Random Number Generation  rand function – Load – Returns "random" number between
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 49 Structures Unions, Part 1 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
CS 161 Introduction to Programming and Problem Solving Chapter 18 Control Flow Through C++ Program Herbert G. Mayer, PSU Status 10/8/2014 Initial content.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
CS 161 Introduction to Programming and Problem Solving Chapter 17 Nested Loops Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
Department of Electronic & Electrical Engineering Statements Blocks{} Semicolons ; Variables Names keywords Scope.
CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
Lecture 7: Variable Scope B Burlingame March 16, 2016.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Decision Making in C.
Functions Course conducted by: Md.Raihan ul Masood
C Functions -Continue…-.
Chapter 2, Part I Introduction to C Programming
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Scope, Parameter Passing, Storage Specifiers
Chapter 6 - Functions Outline 5.1 Introduction
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
ECE 103 Engineering Programming Chapter 32 Array Parameters
Herbert G. Mayer, PSU CS Status 8/2/2013
ECE 103 Engineering Programming Chapter 56 Runtime Errors
Scope.
Review for Final Exam.
ECE 103 Engineering Programming Chapter 19 Nested Loops
ECE 103 Engineering Programming Chapter 51 Random Numbers
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
ECE 103 Engineering Programming Chapter 46 argc, argv, envp
ECE 103 Engineering Programming Chapter 37 C Macro Parameters
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
ECE 103 Engineering Programming Chapter 62 Stack Implementation
ECE 103 Engineering Programming Chapter 64 Tree Implementation
ECE 103 Engineering Programming Chapter 20 Change in Flow of Control
ECE 103 Engineering Programming Chapter 35 C Pointers, Part 1
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
ECE 103 Engineering Programming Chapter 18 Iteration
ECE 103 Engineering Programming Chapter 22 Selection
Compiler vs linker The compiler translates one .c file into a .o file
CSC215 Lecture Control Flow.
Corresponds with Chapter 5
ICS103: Programming in C 5: Repetition and Loop Statements
Scope Rules.
Presentation transcript:

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 Wong @ PSU ECE

Syllabus Individual Statements Statement Blocks

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

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

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

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

#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

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

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