0% found this document useful (0 votes)
21 views

3 Structured Program Development in C

Uploaded by

MAECHAILAH MANOS
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

3 Structured Program Development in C

Uploaded by

MAECHAILAH MANOS
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

Basic Programming for Electrical Engineers

Structured Program Development, Program Control and


Functions
Introduction

• Before writing a program, it is essential to have a


thorough understanding of the problem and a planned
approach in solving the problem.
• Algorithms
– A solution to computing problem involves executing a series of
actions in a specific order. A procedure for solving a problem in
terms of: the actions to be executed, and the order in which
these actions are to be executed is called an algorithm.
– Specifying the order in which statements are to be executed in
a computer program is called program control.
Sample Algorithm
Pseudocode

• Pseudocode is an artificial and informal language that


helps you develop algorithms. It is similar to everyday
language; it is convenient and user-friendly and it is not
an actual computer language.
• Example:
– If student's grade is greater than or equal to 60
Print "passed"
else
Print "failed"
endif
Control Structures

• Normally, statements in a program are executed on after


the other in the order in which they’re written. This is
called sequential execution. Various C statements
enable you to specify that the next statement to be
executed may be other than the next one in sequence.
This is called transfer of control.
Control Structures

• In C, unless directed otherwise, the computer executes C


statements one after the other in the order in which
they’re written.
• A flowchart is a graphical representation of an algorithm
or of a portion of an algorithm.
– Rectangle symbol  action symbol
– Diamond symbol  decision symbol
– Oval symbol  Start and End of flowchart
Sample Flowchart
The if Statement
Logical Operators

• Sometimes you might need to ask more than one


relational question at once. For example, “If it's 7AM and
a weekday and not my vacation, then ring the alarm.”
Logical operators let you combine two or more relational
expressions into a single expression that evaluates to
either true or false.
Logical Operators
Compound Assignment Operators

• Examples:
Simple Activity

• Write a program that checks if a number is a non-zero


integer or not. If it is, it tells if it is odd or even.
Packaging Code in Functions

• Function are central to C programming and to the


philosophy of C program design. We will discuss:

– What a function is and what its parts are


– How to declare local variables in a function
– How to return a value from a function to the program
– How to pass arguments to a function
What is a Function?

• A function is a named, independent section of C code that


performs a specific task and optionally returns a value to
the calling program.
– named: each function has a unique name
– independent: can perform its task without interference from or
interfering with other parts of the program
– performs a task: performs a specified task coded by the
programmer
– can return a value to the calling program
How a Function Works

• A C program does not execute the statements in a


function until the function is called by another part of the
program. When a function is called, the program can send
the function information in the form of arguments. When
the function's statements have finished, execution passes
back to the same location in the program that called the
function. Functions can send information back to the
program in the form of a return value.
Functions
Activity

• Create a program that computes the area of a square (in


m2) with the use of a function. (the program asks for the
length of one side and does not accept integers that are
less than or equal to zero)
Local Variables

• Variables declared in a function are called local variables.


local means that the variables are private to that particular
function and are distinct from other variables of the same
name declared elsewhere in the program.
`
Functions and Structured Programming

• By using functions in C programs, structured


programming can be practiced, in which individual
program tasks are performed by independent sections of
program code.
Advantages of Structured Programming

• It’s easier to write a structured program, because complex


programming problems are broken into a number of
smaller tasks. Each task is performed by a function in
which code and variables are isolated.
• It’s easier to debug structured programming.
Planning a Structured Program

• Planning can be done with nothing more than a pen and


paper. The plan should be a list of specific tasks your
program performs. Begin with a global idea of the
program’s function.
Planning a Structured Program

• Example: Program to manage contacts (list of names and


addresses). What would the program do?

– Enter new names and addresses


– Modify existing entries
– Sort entries by last name
– Print mailing labels
• Enter new names and addresses
– Read the existing address list from disk
– Prompt the user for one or more new entries
– Add the new data to the list
– Save the updated list
• Modify existing entries
– Read the existing address list from disk
– Modify one or more entries
– Save the updated list
The while Repetition Statement

• A repetition statement allows you to specify that an


action is to be repeated while some condition remains
true. A sample pseudocode is:

While there is money in my Steam Wallet


purchase a game and deduct the amount
in wallet
Example
Activity

• Using a while loop, write a program that gets the factorial


of a number between 1 and 8.
• Factorial of a number is given by:
Recursion

• The term recursion refers to a situtation in which a


function calls itself either directly or indirectly.
– Indirect --> occurs when one fucntion calls another function that
then calls the first function.
• C allows recursive functions and they can be useful in
some situations.
Recursion

• Recursion can be used to calculate the factorial of a


number. The factorial of a number x is written x ! and is
calculated as follows:
Increment and Decrement Operators
• The increment and decrement operators can be used only
with variables, not with constants.
– ++x; //x = x + 1
– --y; //y = y - 1
• They can be placed before its operand (prefix mode) or
after its operand (postfix mode)
– x = 10;
– y = x++;
• Most programs involve repetition, or looping. A loop is a
group of instructions the computer executes repeatedly
while some loop-continuation condition remains true.
– Counter-controlled repetition (definite repetition)
– Sentinel-controlled repetition (indefinite repetition)
Counter-Controlled Repetition

• Requires:
– the name of a control variable (loop counter)
– the initial value of the control variable
– the increment (or decrement) by which the control variable is
modified each time through the loop
– the condition that tests for the final value of the control variable
(i.e., whether looping should continue)
for Repetition Statement

• The for repetition statement handles all the details of


counter-controlled repetition
do…while Repetition Statement

• The do…while repetition statement is similar to the while


statement. In the while statement, the loop-continuation
condition is tested at the beginning of the loop while the
do…while statement tests the loop continuation after the
loop body is performed.
break and continue Statements
• The break and continue statements are used to alter the
flow of control. The break statement, when executed in a
loop, causes an immediate exit from that loop. The
continue statement, when executed in a loop, skips the
remaining statements in the body of that control statement
and performs the next iteration of the loop.
Activity

• Use a while-loop statement to replicate the following


output
switch Multiple-Selection Statement

• Occasionally, an algorithm will contain a series of


decisions in which a variable or expression is tested
separately for each of the constant integral values is may
assume and different actions are taken. C provides the
switch multiple-selection statement to handle such
decision making.
• If a match is found between expression and one of the
templates, execution is transferred to the statement that
follows the case label
• If no match is found, execution is transferred to the
statement following the optional default label or the switch
statement’s closing brace.
Grade Counter

You might also like