Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Programming Fundamentals

CS-1001
Lecture 03

Dr. Muhammad Mobeen Movania


Outlines
• What is flow chart
– Flow chart symbols
– Flow chart examples
• Types of languages
• Program compilation process
• What is an IDE
• Parts of a program
• A simple C hello world program
• Keywords
• Variables: programmer defined identifiers
• Operators
• Syntax
• Procedural vs Object oriented programming
• Types of logic constructs
Introducing Flowchart
• Represent algorithm or pseudocode pictorially
• Logic is represented through shapes
• Easier to understand and implement
• Several tools exist like Raptor, Flowgorithm,
etc.
• Flowcharts can be made in MS Visio, draw.io
and other tools
Flowchart symbols

Taken from: http://www.breezetree.com/images/flow-chart-symbols.png


Taken from: http://www.breezetree.com/images/flow-chart-symbols.png
Flow chart examples
• Problem: Checking if the given input number is even or odd
Start

num=0

Input
num

Output F T Output
“Num is (num%2) ==0 “Num is
Odd” Even”

Stop
Another example Start

• Problem: Calculate sum of all sum=0, i=0,


N=0, tmp=0
numbers input by a user

Input N

T Input
i != N
tmp
F

Output sum=sum+tmp
sum i=i+1

Stop A
Another flowchart example
• Another example but Start
generally data flow
sum=0, i=1,
should always be from N=0, tmp=0
top to bottom
Input N

Input
i>N
No tmp
Yes
sum=sum+tmp
Output i=i+1
sum
A
Stop
Types of languages
– Low-level: used for communication with computer
hardware directly.
– Often written in binary machine code (0’s/1’s) directly.
– Example: machine language
– High-level: closer to human language
Python JavaScript
C
C++
BASIC Ruby
FORTRAN
Java
Visual Basic
C#
COBOL
Program compilation process
• A program written in a high level language is
converted to machine code by a compiler
• The steps involved are as follows
a) Create file containing the program with a text editor.
b) Run preprocessor to convert source file directives to
source code program statements.
c) Run compiler to convert source program into
machine instructions.
d) Run linker to connect hardware-specific code to
machine instructions, producing an executable file.
• Steps b–d are often performed by a single
command or button click.
Compilation process
What is an integrated development
environment (IDE)
• An integrated development environment, or
IDE, combine all the tools needed to write,
compile, and debug a program into a single
software application.
• Examples are Microsoft Visual C++,
CodeBlocks, Eclipse, Dev C++, CodeWarrior,
Borland C++ etc.
Example: VisualStudio2012
Parts of a program
• Common elements in programming
languages:
– Key Words
– Programmer-Defined Identifiers
– Operators
– Punctuation
– Syntax
A simple hello world C program

#include <stdio.h>

int main() {
printf(“Hello World”);
return 0;
}
Syntax
• The rules of structure/form that must be
followed when writing a program
• Controls the use of key words, operators,
programmer-defined symbols, and
punctuation
Semantics
• What the syntax stands for
• What it means
• Interpretation of an expression
Tokens in C
• A C program consists of various tokens
• A token is either a keyword, an identifier, a constant, a
string literal, or a symbol
• Example
– printf("Hello, World! \n");
• In the above example, we have 5 tokens:
– printf
– (
– "Hello, World! \n“
– )
– ;
Semicolon
(Statement terminator)
• In C program, the semicolon is a statement
terminator
• That is, each individual statement must be
ended with a semicolon
• It indicates the end of one logical entity.
• Following are two different statements:
– printf("Hello, World! \n"); return 0;
Comments
• are helping text in your C program and they are ignored by the compiler
• Single line comments start with //
• Multiline comments start with /* and terminates with the characters */
• Example
– int main() {
– //this is a single line comment
– printf(“C is fun”);
– /*
– This is an example of multiline comments
– */
– return 0;
– }
• Note that you cannot have comments within comments
Key words
• Also known as reserved words
• Have a special meaning in C
• Can not be used for any other purpose
auto else long switch
break enum register typedef
case extern return union
char float short unsigned
const for signed void
continue goto sizeof volatile
default if static while
do int struct _Packed
double
Variables: programmer defined
identifiers
• A variable is a named storage location in the computer’s memory
for holding a piece of data.
• Not part of the C language
• Used to represent various things: variables (memory locations),
functions, etc.
• Variables are separated using comma (,)
• A C identifier is a name used to identify a variable, function, or any
other user-defined item
– An identifier starts with a letter A to Z or a to z or an underscore _
followed by zero or more letters, underscores, and digits (0 to 9)
• C does not allow punctuation characters such as @, $, and % within
identifiers
• Identifiers are case sensitive so good and GOOD are different
identifiers
Examples of identifiers
• Must start with a letter or underscore but it is not
recommended to start with underscore
– Valid identifiers: abc, abc123, abc_123
– Invalid identifiers: $abc, abc 123, 123abc
• Are case sensitive
– abc, ABC, Abc, ABc, abC are all valid and
different
• Multi-word identifiers can be written together
without space or with underscore
– hello_world or helloWorld
Procedural vs Object oriented
programming
• Procedural programming: focus is on the
process
– Procedures/functions are written to process data

• Object-Oriented programming: focus is on


objects, which contain data and the means to
manipulate the data
– Messages sent to objects to perform operations
Types of Logic Constructs
• Three basic types
– Sequence
– Selection
– Iteration
• Sequence
– The most basic logic structures in computer programming
– an action, or event, leads to the next ordered action in a
predetermined order
– can contain any number of actions, but no actions can be
skipped in the sequence
• Example
– MAKING PIZZA: base--> tomato sauce--> cheese---> bake
Selection
• A selection (also called a decision) is also one of the
basic logic structures in computer programming
– a question is asked, and depending on the answer, the
program takes one of two courses of action, after which
the program moves on to the next event
– Also referred to as an if-then-else construct because it
directs the program to perform in this way: If Condition A
is True then perform Action X else perform Action Y
• Example:
– MAKING PIZZA: base--> tomato sauce--> topping?(if yes,
add parsley. if not, skip) --> cheese---> bake
Iteration
• An iteration is a single pass through a group/set
of instructions
• Most programs often contain loops of
instructions that are executed over and over
again
• The computer repeatedly executes the loop,
iterating through the loop
• Example
– MAKING PIZZA: base--> tomato sauce--> topping?(if
yes, add parsley and go back to "topping?". if not,
skip) --> cheese---> bake
Next Lecture
• Introduction to C programming

You might also like