Introduction to Computers and Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to Computers and Programming

Description:

Common Exam Questions: Given a piece of code, identify the bug in the code ... For our last trick... #include stdio.h void main() int x = 2, y = 3; ... – PowerPoint PPT presentation

Number of Views:13
Avg rating:3.0/5.0
Slides: 22
Provided by: aviros
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Computers and Programming


1
Introduction to Computers and Programming
  • Class 9
  • Introduction to C
  • Professor Avi Rosenfeld

2
Loop Structures
  • Event Controlled
  • While Loops
  • Do While Loops
  • Counter Controlled
  • Counter while loops
  • For loops (next time)

3
Parts of a while Loop
  • Every while loop will always contain three main
    elements
  • Priming initialize your variables
  • Testing test against some known condition
  • Updating update the variable that is tested

4
Infinite Loop
  • Infinite Loop A loop that never ends
  • Generally, you want to avoid these!
  • There are special cases, however, when you do
    want to create infinite loops on purpose
  • Common Exam Questions
  • Given a piece of code, identify the bug in the
    code
  • You may need to identify infinite loops

5
Infinite Loop Example 1
  • include ltstdio.hgt
  •  
  • main ()
  • int index 1
  •   while (index lt 10)
  • printf ("Index d\n", index)

Index 1 Index 1 Index 1 Index 1 Index
1 forever
Here, I have deleted part 3 the index index
1 statement.
6
  • include ltstdio.hgt
  • void main()
  • int number,EvenNumbers 0, OddNumbers 0
  • printf("\nEnter a number between 0 9 inclusive
    (999 to end program) ")
  • scanf("d", number) / why is this scanf
    needed? /
  • while (number ! 999)
  • switch (number)
  • case 0 case 2 case 4 case 6 case 8
  • EvenNumbers /what is the ? /
  • printf("You entered a valid EVEN ")
  • break
  • case 1 case 3 case 5 case 7 case 9
  • OddNumbers
  • printf("You entered a valid ODD ")
  • break
  • default

7
  • include ltstdio.hgt
  • void main()
  • int number,EvenNumbers 0, OddNumbers 0
  • do
  • printf("\nEnter a number between 0 9
    inclusive (999 to end program) ")
  • scanf("d", number)
  • switch (number)
  • case 0 case 2 case 4 case 6 case 8
  • EvenNumbers
  • printf("You entered a valid EVEN ")
  • break
  • case 1 case 3 case 5 case 7 case 9
  • OddNumbers
  • printf("You entered a valid ODD ")
  • break
  • default

8
Shortcuts
  • C provides abbreviations for some common
    operations
  • Assignment operators
  • Increment/Decrement operators

9
Assignment Operators
  • Abbreviations are provided for the basic binary
    operations
  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Modulus ()

10
Shortcut Heaven (or Hell?)
  • include ltstdio.hgt
  • void main()
  • int x 2, y 3
  • x 3 / same as x x 3 x is 6 /
  • printf("X is now d\n", x)
  • y / 2 / same as y y / 2 y is 1 /
  • printf("Y is now d\n", y)
  • y 2 / same as y y 2 y is 3 /
  • printf("Y is now d\n", y)
  • x - 2 / same as x x - 2 x is 4 /
  • printf("X is now d\n", x)
  • x y / same as x x y x is 1 /
  • printf("X is now d\n", x)

11
Samples
  • Assume variable int c 10

Operator Initial Value Sample Meaning Assigns
10 c 7 c c 7 17 to c
- 10 c - 3 c c 3 7 to c
10 c 6 c c 6 60 to c
/ 10 c / 5 c c / 5 2 to c
10 c 6 c c 7 3 to c
12
Increment and Decrement Operators
  • C provides unary increment operators and
    decrement operators - (no spaces between them)
  • Increment operators add 1
  • Decrement operators subtract 1
  • Not for other assignment operators (, /, )
  • Guess where C comes from???

13
Postincrement
  • The operator is after the variable
  • Causes the initial value of the variable to be
    used in the expression where it appears AND THEN
    adds the 1 to the variable
  • For example,
  • int iCount 5
  • printf( d\n, iCount )
  • Would print 5 but iCount is worth 6 after the
    statement

14
Preincrement
  • The operator is before the variable
  • Adds 1 to the initial value of the variable
    BEFORE it is used in the expression where it
    appears
  • For example,
  • int iCount 5
  • printf( d\n, iCount)
  • Would print 6 and iCount is worth 6 after the
    statement

15
More examples postincrement vs. preincrement
  • int iTotal 0
  • int iCount 5
  • iTotal iCount 2
  • printf( d\n, iTotal)
  • printf( d\n, iCount)
  • Would print 10 for iTotal and then 6 for iCount
  • iTotal iCount 2
  • printf( d\n, iTotal)
  • printf( d\n, iCount)
  • Would print 12 for iTotal and then 6 for iCount

16
Decrement Operator --
  • Similar to increment in syntax/operation
  • Instead of writing
  • iCount iCount - 1 or
  • iCount - 1
  • You can write
  • iCount-- or --iCount
  • Subtle difference between the two new options
    call postdecrement and predecrement

17
Postdecrement
  • The -- operator is after the variable
  • Causes the initial value of the variable to be
    used in the expression where it appears AND THEN
    subtracts the 1 from the variable
  • For example,
  • int iCount 5
  • printf( d\n, iCount-- )
  • Would print 5 but iCount is worth 4 after the
    statement

18
Predecrement
  • The -- operator is before the variable
  • Subtracts 1 from the initial value of the
    variable BEFORE it is used in the expression
    where it appears
  • For example,
  • int iCount 5
  • printf( d\n, --iCount)
  • Would print 4 and iCount is worth 4 after the
    statement

19
For our last trick
  • include ltstdio.hgt
  • void main()
  • int x 2, y 3
  • printf("What is this number d\n", x y)
  • printf("But x is still d and y is d\n", x, y)
  • printf(What d ???\n", y / x-- x y)
  • / 0 /
  • printf("But x is still d and y is d\n", x, y)

20
Summary Table
Operator Sample Explanation
c Use value of c in expression THEN add 1 to c
c Add 1 to c THEN use the new value of c in expression
-- c-- Use value of c in expression THEN subtract 1 from c
-- --c Subtract 1 from c THEN use new value of c in expression
21
Introducing for loops
  • include ltstdio.hgt
  • void main()
  • int counter 100
  • while (counter--gt 0)
  • printf("The counter is at d ", counter)
Write a Comment
User Comments (0)
About PowerShow.com