Loops - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Loops

Description:

for( short i=0; i 10; i ) printf('%dn', i ); Loop Caveat. Never use the == or != operators in loops (or decisions) with floating point values. ... – PowerPoint PPT presentation

Number of Views:118
Avg rating:3.0/5.0
Slides: 12
Provided by: alanb9
Category:
Tags: caveat | loops

less

Transcript and Presenter's Notes

Title: Loops


1
Loops Functions
  • Chapters 10 11

2
Loops
  • While
  • while(condition) statement
  • short i1while( ilt10 ) printf(d\n, i
    ) i
  • Do-While
  • do statementwhile(condition)
  • short i1do printf(d\n, i )
    iwhile( i lt 10 )
  • For
  • for(initializer condition control) statement
  • for( short i0 ilt10 i ) printf(d\n, i )

3
Loop Caveat
  • Never use the or ! operators in loops (or
    decisions) with floating point values. Round-off
    error could cause your condition to never be true
    and thus put you in an infinite loop.

4
Functions a.k.a. Subprograms
  • The key to writing procedural programs is to
    break up code into small pieces by using
    functions.
  • Main reasons for creating many small functions
  • Reusability reduces the amount of code you need
    to write if you can repeatedly use the same code.
  • Ease of writing testing writing small
    functions takes very little time, and it allows
    you to immediately test the functionality of your
    function. It is much easier to debug code 10
    lines at a time than it is to debug several
    hundred or thousand lines at a time.

5
Rules of Thumb
  • Ideally, your functions should be only 10 15
    lines long.
  • To increase reusability, functions that could
    possibly be used in the future should be written
    as generic as possible to maximize the number of
    situations they will work in.

6
Writing a function
  • In C, functions must be both declared and
    defined.
  • A definition contains the function header, and
    the body.
  • Definitions dont need to be in a specific place
    or order.
  • A declaration (also called a prototype) is a copy
    of the function header placed immediately after
    your include statements and before your main()
    function.
  • You must make declarations so the compiler can
    check the syntax of your function calls.
  • The main() function doesnt need a declaration.
  • Definitions and declarations may both be made in
    two different forms ANSI form, and K R form.
    We will be dealing only with the ANSI form.

7
Function Definition Syntax
  • return_type function_name(arguments) body
  • An exampleint compute_division(int op1, int
    op2) float result result op1 /
    op2 printf(d/df,op1,op2,result) return
    0

8
Function Declaration Syntax
  • return_type function_name(argument types)
  • examples
  • int compute_division(int, int)
  • int compute_division(int op1, int op2)
  • In your declarations, you only need to specify
    the argument type, not the variable name it will
    be assigned to since its not needed for syntax
    checking of function calls. You may include the
    variable name if you wish, the compiler will just
    ignore the name. This means you can just copy
    and paste the function header below the include
    section, add a semicolon to the end and youve
    created youre done.

9
Return Types
  • Return types must be scalar values. They cannot
    be aggregate types (structs, arrays, etc.)
  • You can return a pointer to an aggregate type
    since a pointer is considered scalar.
  • You may also use a void return type. It is a
    good practice to always return a value from your
    functions indicating a success or failure.
    Customarily, a value of 0 indicates success. Any
    other value indicates failure.

10
Function Calls Arguments
  • Function calls and variable scope work exactly
    the same way in C as they do in Java.
  • long i declared in main is different than the
    long i declared in function compute_division
  • Arguments in C are passed by value by default.
    This means that only the value of the variable
    being passed is sent to the function. Changes to
    that variable inside of your function will not
    affect the value back in main.
  • Arguments may be passed by reference, meaning
    that your functions CAN affect the variables in
    the functions that they were called from through
    the use of pointers. This effectively allows you
    to return an unlimited number of values. Well
    cover this more when we get to the section on
    pointers.

11
Static Variables
  • Static variables allow you to preserve the value
    of a variable across function calls.
  • Lets assume we have this function
  • void test( )
  • static long i0
  • printf(d\n,i)
  • i
  • What will be print the first time we call this
    function?
  • The second?
  • The third?
Write a Comment
User Comments (0)
About PowerShow.com