Programming Fundamentals CS-1001: Dr. Muhammad Mobeen Movania
Programming Fundamentals CS-1001: Dr. Muhammad Mobeen Movania
CS-1001
Lecture 03
num=0
Input
num
Output F T Output
“Num is (num%2) ==0 “Num is
Odd” Even”
Stop
Another example Start
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