Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Introduction to C Programming
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Programming Languages and Paradigms The C Programming Language.
CSE 303 Lecture 16 Multi-file (larger) programs
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Wednesday, 12/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Wednesday, 12/11/02  QUESTIONS??  Today: CLOSING CEREMONIES!  HW #5 – Back Monday (12/16)
0 Chap. 4 Functions and Program Structure 4.1 Basics of Functions 4.2 Functions Returning Non-integers 4.3 External Variables 4.4 Scope Rules 4.5 Header.
Functions and Program Structure Chapter 4. Introduction Functions break large computing tasks into smaller ones Appropriate functions hide details of.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
 2007 Pearson Education, Inc. All rights reserved C Preprocessor.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 19 - The Preprocessor Outline 19.1 Introduction 19.2 The #include Preprocessor Directive 19.3.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 12 - The Preprocessor Directives (Macros)
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Chapter 4 Functions and Program Structure Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
1 Homework / Exam Finish up K&R Chapters 3 & 4 Starting K&R Chapter 5 Next Class HW4 due next class Go over HW3 solutions.
C Hints and Tips The preprocessor and other fun toys.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Engineering Computing I Chapter 4 Functions and Program Structure.
CPS120: Introduction to Computer Science Lecture 14 Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
1 Chapter 3: Loops and Logic. 2 Control Statements If statement Example NumberCheck.java Relational operators (, >=, ==, !=) Using code blocks with If.
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used to group declarations and.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Adv. UNIX:pre/111 Advanced UNIX v Objectives of these slides: –look at the features of the C preprocessor Special Topics in Comp. Eng.
Functions and Program Structure Chapter 4. Introduction Functions break large computing tasks into smaller ones Appropriate functions hide details of.
Presentation By :- Nikhil R. Anande ( ) Electronic & Communication Engineering. 3 nd Year / 5 th Semester FACULTY GUIDE : RAHIUL PATEL SIR MICROCONTROLLER.
Programming in C Project Organization Compiler Directives.
Functions and Program Structure CSE 2031 Fall June 2016.
Week 3-4 Control flow (review) Function definition Program Structures
Functions Course conducted by: Md.Raihan ul Masood
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
Functions Separate Compilation
Programmazione I a.a. 2017/2018.
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Variable Names Names are a sequence of visible characters
User-Defined Functions
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 6 - Functions Outline 5.1 Introduction
Register Variables Declaring a variable as a "register" variable is an advisory to the compiler to keep the normal location of the variable in a register,
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Programming in C Miscellaneous Topics.
Programming in C Miscellaneous Topics.
Homework Any Questions?.
C Programming Getting started Variables Basic C operators Conditionals
Functions and Program Structure
Homework Finish up K&R Chapters 3 & 4
Functions and Program Structure
CSC215 Lecture Control Flow.
Presentation transcript:

Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control Flows and Functions

Programming III Remember to Check – – Any problems with basic compilation? Have you check the resource folders? – Do you know where they are?

Programming III This lecture covers Chapter 3 and 4 from K&R – You can find Chapter 5,6,9,10 (5 + 6, ) in Modern Approach in C with much more detail

Statement and Blocks X = 5; //statement { } // inside of a block or compound statement if (exp) s1; else s2; if (exp) { s1; s2; } else s3; if (exp) s1; else if (exp) s2; else if (exp) s3; else s4;

Wrong! Why?

Example (Binary Search)

For and While Loops while (expression) s1; while (true) ; while (expr) { s1; s2; } for (e1; e2; e3) s1; for ( ; ; ) ; for (e1; e2; e3) { s1; s2; }

Example

Shellsort

Reverse String Comma Operator

DO While

Break and Continue

Goto and Labels

Note about Goto It will help to exit nested loops But you can always write code without goto – I have never needed in C, C++, C#, or Java. I assume that I will not needed in most other languages – My only code that has goto in C++, is a C library for the WiiMote controller that came with it. However, the issue with goto is that makes code look messy. – But there is nothing wrong with having a jump – We do it in Assembly

Another example of Goto

But we can do it without GOTO

Functions and Program Structure Break large computation task into smaller task Enable you (or other people) to reuse it Ideally, you always want to have small functions You may want to break the C files into many, depending on your project – This is true for any other language Compile multiple files : gcc –Wall main.c getline.c strindex.c -o pattern.out

Example -- ould

main

int getline(char s[], int lim)

int strindex(char s[], char t[])

Functions return-type function-name (argument declarations) { declarations; statements; return expression; // if void, just return }

Functions returning non-integer

main calling atof

atof (note) double atof(char []); – says that atof returns a double and takes a character. It is better to used either function declaration – Use extern if needed.

Function declaration #include //already has a declaration extern void myGetLine(); float pow(float x, float y); int main() { … pow(5,2); } float pow(float x, float y) { … }

atoi

extern External variables are global variables Functions are always external If in separate file – use extern keyword

Push and Pop

Use of Extern

Header Files Allows to keep ideas functions together It is easier to reuse.

Static variables It has two uses – In private variables, it allows to keep its value – In external (global) variables It allows to hide variables from other source files – static int value; A note about register value – It is unlikely that modern compilers will use this. But if they do, it is unclear that the developer will gain an advantage – register int x;

Block Structure (scope) if (n > 0) { int x = 10; for (int x = 0; x < n; x++) … }

Block Structure (scope) int x; int y; f (double x) { double y; … }

Initialization external and static variables are initialized to zero – However, I like to be safe, so always initialized. Automatic (function vars) and Register variables – Undefined (garbage) External and Static variables – Initialization value must be constant Automatic and Register variables – Initialization does not have to be constant

Initialization Arrays can be initialized with braces or by individual members of the arrays Characters arrays can use string or {}.

We can also do recursion in C

The C Pre-Processor #include “filename” #include Macro Substitution – #define name replacementText – #define foreverloop for ( ; ; ) – #define max(A,B) ( (A) > (B) ? (A) : (B)) x = max(p+q,r+s) Which it will be replaced by x = (( p+ q) > ….. ) It has pitfalls max(i++,j++) // wrong – #define square(x) x * x square (z + 1)

Avoid macro Functions Avoid macro functions as much as possible It can brings lots of problems – Undesired side effects – Makes it harder for debugging

#define #define has a #undef If you use #param in a definition it will replace it with quotes If use ##, then you can get concatenation

“ “ #param If you use #param in a definition it will replace it with quotes

Conditional Inclusions if !defined(HDR) #define HDR #if #elif #else #endif #ifdef #ifndef

Conditional Inclusion (2)