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

Session 5 - Flow of Program

Uploaded by

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

Session 5 - Flow of Program

Uploaded by

Brian Were
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

SESSION FIVE

C FLOW OF PROGRAM

5.1 Introduction

5.2 Learning Outcomes

5.3 Flow of Program

5.4 Precedence and Associativity of operators

5.5 Example C Program

5.6 How to compile source code to Executable code

5.7 I/O – Processing – I/O cycle

5.7 Types of errors

5.1 Introduction

A programmer can use several techniques in a C code to control the flow of a


program. Since code flow is purely the logic required for the program, a
programmer should have an idea about the code flow for each particular code.
The programmer converts the logic into C code, and he may use the code flow
control techniques available in the C during the process. In this session, we will
learn more about flow of a c program.

5.2 Learning Outcomes

At the end of this session, you should be able to:

 Describe the flow of a program


 Demonstrate flow of C program through writing simple
programs.

1
5.3 Flow of Program

While writing a program in C, we have to follow some specific flow of types of


statements. Figure 1 shows the sequential flow of a program:

Figure 1

Sometimes, processing statements and output statements are required to be


repeated for finite number of times. In such a situation, flow of statements is
shown with the help of Figure 2 below-

2
Figure 2

5.4 Precedence and Associativity of operators (Arithmetic and


Assignment operators)

Precedence and Associativity of Arithmetic and Assignment operators are shown


in Table 1 below-

Table 1

Example

int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;
e = (a + b) * c / d;
3
= (20+10) * 15/5
= 30 * 15/5
= 450 /5
= 90

e= a + (b * c) / d;
= 20+ (10 * 15)/5
= 20 + 150/5
= 20 + 30
e= 50

5.5 Example C Program

Write a program in C to display the area and perimeter of rectangle. Length and
Breadth of rectangle is input through the keyboard.

/* Program to display area and perimeter of a rectangle [rectangle.c] */


#include < stdio.h >

#include<conio.h>
void main ( )
{
// Variable Declaration
float len, wid;
float area, peri ;
clrscr ( );

// Input
printf ( “ \n Enter the Length \n “ );
scanf ( “ %f “, &len );
printf ( “ \n Enter the Breadth \n “ );
scanf ( “ %f “, &wid );

// Processing
area = len * wid ;
peri = 2 * ( len + wid );

// Output
printf ( “ \n Area = %f “, area );
printf ( “ \n Perimeter = %f “, peri );

4
getch ( );
}

Output

Enter the Length


10
Enter the Breadth
8
Area = 80
Perimeter = 36

5.6 How to compile Source code to Executable code

a. Instructions written by a programmer in C language is called program. Any


program is stored in a file (for example- rectangle.c) called source file and the
code stored in it is called source code.

b. Source code is in human readable form. For machine (i.e. computer) to


understand, it should be converted into machine readable form called
machine code/object code. This task is done by C compiler which first of all
checks for any syntax error and if there is no syntax error then convert source
code into object code and creates new object code file (for example-
rectangle.obj) automatically. This is done by pressing Alt + F9 key.

c. To execute any object code, it needs to be converted into executable code


which is stored in a separate file (for example- rectangle.exe). This is done by
pressing Ctrl+ F9 key. A new executable file will be created automatically
which gives output of the program.

This is shown with the help of Figure 1 below:

5
Figure 1: Source Code to Executable Code

6
5.7 I/O – Processing – I/O cycle

Figure: I/O-CPU Processing

a. In any C program, any of these two things will happen- either I/O or CPU
processing.

b. I/O - > CPU processing - > I/O - CPU processing - > I/O -> CPU processing
and so on is called I/O- Processing – I/O cycle.

c. At the time of Input or Output, CPU is free.

d. As we know that speed of computer is very fast, so, it performs Output and
CPU processing at very fast speed. (Although speed of Output depends on
the speed of output device and speed of CPU processing depends on speed
of processor used.)

e. But Input is not in the hand of computer because it is to be provided by


user to the computer. The computer stops only when user provides input
to the user.

5.8 Types of Errors

There are three types of errors that may be found in any C program. They
include:

a. Syntax Errors

b. Logical Errors

c. Runtime Errors

5.8.1 Syntax Errors

7
Errors that occur when you violate the rules of writing C syntax are known as
syntax errors. These errors indicate something that must be fixed (corrected)
before the code can be compiled. All these errors are detected by compiler and
thus are also known as compile-time errors. Example include:

 Missing Semi colon ( ; )

 Missing parentheses ( } )

 Using variable without declaring it.

 Spelling of pre-defined functions is wrong. Etc.

5.8.2 Logical Errors

On compilation and execution of a program, desired output is not obtained when


certain input values are given. These types of errors which provide incorrect
output but appear to be error free are called logical errors. These are one of the
most common types of errors done by beginners of programming.
These errors solely depend on the logical thinking of the programmer. Example
include:

 Mistake in formula

 Mistake in logic

 Mistake in sequence of steps Etc.

5.8.3 Run time Errors

Errors which occur during program execution (run-time) after successful


compilation are called run-time errors. One of the most common run-time error
is division by zero also known as Division error. These types of error are hard to
find as the compiler doesn’t point to the line at which the error occurs. Example
include:

 Divide by zero

 Insufficient memory space Etc.

5.9 More Examples of C programs

a. Write algorithm, draw flowchart and write C program to swap both numbers
(Two numbers are input through the keyboard).

8
Output-

Enter the value of A 10


Enter the value of B 20
a = 20

b = 10

9
b. A four digit number ‘n’ is input through the keyboard. Write algorithm, draw
flowchart and write C program to display the sum of its digits.

[ Hint – Let n = 3457 then sum of its digits = 3 + 4 + 5 + 7 = 19 ]

Output

10
Enter any four digit number (1000-9999) 9875
Sum of its digits = 29

5.10 Summary

In this session, we learned about the flow of program and different types of errors
that can be found in a program.

11

You might also like