Introduction To Computer Programming - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Introduction To Computer Programming

Description:

Eat breakfast. Carpool to work. Get out of bed. Take off pajamas. Take a shower. Get dressed. Eat breakfast. Carpool to work. Sequence II. Sequence I ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 25
Provided by: mrrerkchai
Category:

less

Transcript and Presenter's Notes

Title: Introduction To Computer Programming


1
Introduction To Computer Programming
  • Lecture 2

Structured Programming Development
By Mr. Rerkchai Fooprateepsiri E-mail
rerkchai_at_mut.ac.th
2
Lecture 2 Structured Programming Development
Outline
- Arithmetic in C
- Decision Marking Equality and
Relational Operators
- Algorithms
- Pseudocode
- Control Structures
- The if/Else Selection Structure
2
ITEC 0120 Introduction to Computer Programming
(1/11/2004)
3
Lecture 2 Structured Programming Development
Arithmetic in C
C arithmetic operations
3
ITEC 0120 Introduction to Computer Programming
(1/11/2004)
4
Lecture 2 Structured Programming Development
Arithmetic in C (2)
Example 1
algebra
m a b c d e
5
C m (abcde)/5
Example 2
algebra
y mx b
C y m x b
4
ITEC 0120 Introduction to Computer Programming
(1/11/2004)
5
Lecture 2 Structured Programming Development
Arithmetic in C (3)
Example 3
algebra
z p r q w / x - y
C z p r q w / x - y
1
2
4
3
5
Example 4
C y a x x b x c
1
2
4
3
5
5
ITEC 0120 Introduction to Computer Programming
(1/11/2004)
6
Lecture 2 Structured Programming Development
Arithmetic in C (4)
Precedence of arithmetic operators
6
ITEC 0120 Introduction to Computer Programming
(1/11/2004)
7
Lecture 2 Structured Programming Development
Arithmetic in C (5)
Example 5 From Example 4 suppose a2, b3, c7,
and x5
Step1
y 2 5 5 3 5 7 2 5 10 y
10 5 3 5 7 10 5 50 y 50
3 5 7 3 5 15
( Leftmost multiplication first )
Step2
( Leftmost multiplication )
Step3
( Multiplication before addition)
7
ITEC 0120 Introduction to Computer Programming
(1/11/2004)
8
Lecture 2 Structured Programming Development
Arithmetic in C (6)
Example 5 (Continue)
Step 4
y 50 15 7 50 15 65 y 65 7
65 7 72 y 72
( Leftmost addition )
Step 5
( Last Operation )
Final Step
8
ITEC 0120 Introduction to Computer Programming
(1/11/2004)
9
Lecture 2 Structured Programming Development
Decision Marking Equality and Relational
Operators
Equality and relational operators
9
ITEC 0120 Introduction to Computer Programming
(1/11/2004)
10
Lecture 2 Structured Programming Development
Decision Marking Equality and Relational
Operators (2)
Example (6) Using Condition Operator in if
statement
include ltstdio.hgt main() int num1, num2
printf(Enter two integer, and I will tell you
) printf(the relationship they satisfy\n)
printf(First Value ) scanf(d,num1)
printf(Second Value ) scanf(d,num2)
if(num1 num2) printf(d is equal to
d\n,num1,num2)
10
ITEC 0120 Introduction to Computer Programming
(1/11/2004)
11
Lecture 2 Structured Programming Development
Decision Marking Equality and Relational
Operators (3)
Example 6 (Continue)
if(num1 ! num2) printf(d is not equal to
d\n,num1,num2) if(num1 lt num2)
printf(d is less than d\n,num1,num2)
if(num1 gt num2) printf(d is greater than
d\n,num1,num2) if(num1 lt num2)
printf(d is less than or equal to
d\n,num1,num2) if(num1 gt num2)
printf(d is greater than or equal to
d\n,num1,num2) return 0
11
ITEC 0120 Introduction to Computer Programming
(1/11/2004)
12
Lecture 2 Structured Programming Development
Decision Marking Equality and Relational
Operators (4)
Precedence and associativity of the operators
discussed so far
12
ITEC 0120 Introduction to Computer Programming
(1/11/2004)
13
Lecture 2 Structured Programming Development
Algorithms
The solution to any computing program involves
executing a series of action in a specific order.
A procedure for solving a problem in terms of 1.
the action to be executed, and 2. the order in
which these actions are to be executed Is called
an algorithm.
13
ITEC 0120 Introduction to Computer Programming
(1/11/2004)
14
Lecture 2 Structured Programming Development
Algorithms (2)
Example 7 Consider rise-and-shine algorithm
followed by one junior executive for getting out
of the bed and going to work
14
ITEC 0120 Introduction to Computer Programming
(1/11/2004)
15
Lecture 2 Structured Programming Development
Pseudocode
Pseudocode is an artificial and information
language that helps programmers develop
algorithm. The psedocode programs are not
actually executed on computers. Rather, they
merely help the programmer Think out a program
before attempting to write it in a programming
language such as C. Psedocode consists only of
action statements those that are executed when
the program has been coonverted from psedocode to
C and is run in C.
15
ITEC 0120 Introduction to Computer Programming
(1/11/2004)
16
Lecture 2 Structured Programming Development
Control Structures
Normally, statement in program are executed on
after the other in the order in which they are
written. This is called sequential execution.
Various C statements we will soon discuss
enable the programmer to specify that the next
statement to be executed may be other than the
next one is sequence. This is called transfer of
control.
16
ITEC 0120 Introduction to Computer Programming
(1/11/2004)
17
Lecture 2 Structured Programming Development
Control Structures (2)
Add grade to total
total total grade counter counter 1
Add 1 to counter
Flowcharting Cs sequence structure
17
ITEC 0120 Introduction to Computer Programming
(1/11/2004)
18
Lecture 2 Structured Programming Development
The If Selection Structure
A selection structure is used to choose among
alternative course of action. For Example,
suppose the passing grade on an exam is 60.
Psedocode statement If students grade is
greater than or equal to 60 Print Passed
The preceding psedocode if statement may be write
in C as if(grade gt 60)
printf(Passed\n)
18
ITEC 0120 Introduction to Computer Programming
(1/11/2004)
19
Lecture 2 Structured Programming Development
The If Selection Structure (2)
true
Print Passed
grade gt60
false
Flowcharting Cs single selection structure
19
ITEC 0120 Introduction to Computer Programming
(1/11/2004)
20
Lecture 2 Structured Programming Development
The If/Else Selection Structure
The if/else selection structure allows the
programmer to specify that different actions are
to be performed when the condition is true than
when the condition is false. For example,
Psedocode statement If students
grade is greater than or equal to 60
Print Passed else Print
Failed
The preceding psedocode if statement may be write
in C as if(grade gt 60)
printf(Passed\n) else
printf(Failed\n)
20
ITEC 0120 Introduction to Computer Programming
(1/11/2004)
21
Lecture 2 Structured Programming Development
The If Selection Structure (2)
false
true
Print Failed
Print Passed
grade gt60
Flowcharting Cs single selection structure
21
ITEC 0120 Introduction to Computer Programming
(1/11/2004)
22
Lecture 2 Structured Programming Development
The If/Else Selection Structure (3)
Nested if/Else structure test multiple cases by
placing if/else structures inside if/else
structure. For Example, the following psedocode
statement will print A for exam grades greater
than or equal to 90, B for greater than or equal
to 80, C for grades greater than or equal to 70,D
for grade greater than or equal to 60, and F for
all other grades.
Psedocode statement If students grade
is greater than or equal to 90
Print A else If students
grade is greater than or equal to 80
Print B
else If
students grade is greater than or equal to
70 Print C else
If students grade is grater than or equal to
60 Print D else
Pint F
22
ITEC 0120 Introduction to Computer Programming
(1/11/2004)
23
Lecture 2 Structured Programming Development
The If/Else Selection Structure (4)
The preceding psedocode if statement may be write
in C as if(grade gt 90)
printf(A\n) else if(grade gt 80)
printf(B\n) else if(grade gt
70) printf(C\n) else
if(grade gt 60) printf(D\n)
else printf(F\n)
23
ITEC 0120 Introduction to Computer Programming
(1/11/2004)
24
Lecture 2 Structured Programming Development
The If/Else Selection Structure (5)
The Following example includes a compound
statement in the else part of an if/else structure
if(grade gt 60)
printf(Passed\n) else
printf(Failed\n) printf(You must
take this course again.\n)
24
ITEC 0120 Introduction to Computer Programming
(1/11/2004)
Write a Comment
User Comments (0)
About PowerShow.com