Programming Fundamentals
Programming Fundamentals
other than C)
• click here to skip the Programming Fundamentals part.
U1.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
‹#›
1
P
Programming
i F Fundamentals
d t l
U1.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
‹#›
2
Objectives
• Understand the different types of programming languages.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
3
Computer Components
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
4
Software
Software is comprised of instructions that get a
computer to perform a task.
Application Software System Software
Word Processors Operating Systems
Spreadsheets Windows
Painting programs Macintosh
Macintosh OS
Web browsers, email Unix
programs Linux
Drivers
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
5
Programming Languages
• Programming languages allow programmers to code
software.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
6
Machine Languages
• Comprised of 1s and 0s
• Diffi
Difficult
lt tto program – one misplaced
i l d 1 or 0 will
ill cause th
the
program to fail.
• Example of code:
1110100010101 111010101110
10111010110100 10100011110111
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
7
Assembly Languages
• A step towards easier programming.
• A
Assembly
bl language
l code
d needs
d to
t be
b translated
t l t d to
t machine
hi
language before the computer processes it.
• Example:
E l
ADD 1001010, 1011010
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
8
High-Level Languages
• High-level languages represent a giant leap towards easier
programming.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
9
Procedural Languages
• Procedural languages
The focus of such languages is on sequence of activities
t be
to b carried
i d out.
t
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
10
Object-Oriented Languages
• Object-oriented languages:
Focus
F on modeling
d li data.
d t
Programmers
g code using
g “blueprints”
p of data models
called classes.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
11
Compiling
• All HL programs need to be translated to machine code
so that a computer can process the program.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
12
Programming Example
• Simple programming problem: Convert a length from feet
into meters.
• Pseudocode:
Input the length of an item, LengthInFeet, in feet and Compute
the length of the item in meters:
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
13
Programming Example
Partial Code in C:
float LengthInFeet;
L
LengthInMeters
thI M t = L
LengthInFeet
thI F t * .3048;
3048
printf (“%f”, LengthInMeters);
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
14
Input & Variables
• Input operations get data into the programs
• A user is
i prompted
t d to
t enter
t data:
d t
Write “Enter the length in feet”
Input
p LengthInFeet
g
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
15
Variables
• Needed because we might want the program to run using
different values:
R
Referring
f i to t the
th variable
i bl LengthInFeet
L thI F t by
b its
it name gives
i
the value stored in it.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
16
Constants
• Many a time we need to used fixed values for certain
computations.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
17
Types of Data
• Numeric Data
Integer data
data, II.e.,
e whole numbers
numbers, 10 25 -4545 0
Floating point data – have a decimal point 23.0, -5.0
• B
Boolean
l d
data
t
TRUE/FALSE
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
18
Data Processing and Output
• LengthInMeters = .3048 * LengthInFeet
The above statement is a processing statement.
Take
T k the
th value
l iin th
the variable
i bl LengthInFeet,
L thI F t
Multiply by .3048, and
Set the value of the variable LengthInMeters
g to the
result of the multiplication.
Also stated as assign the result of multiplication to the
variable LengthInMeters
• Write LengthInMeters
Output
O t t the
th value
l ini LengthInMeters
h t the
to th monitor.
it
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
19
Assignment Statements
• If the variable being assigned to already contains a data
value,
the
th previously
i l stored
t d value
l is
i overwritten
itt
E.g.
E g the following statements:
Age= 12;
Age=15;
ge 5;
Have an effect of storing 15 in the variable Age.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
20
Assignment Statements
• The statement
Age = Age + 1
Changes the value of the variable Age in such a way that
the new value is one greater than the previous value.
• Working: Take the value of Age, add 1, and store the result
back in the same variable.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
21
Operations on Data
• Operator is a symbol that is used to perform certain operations
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
22
Operators
• C, like most programming languages, supports operators to
carry out various kinds of operations
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
23
Precedence of Operators
• Operator precedence:
A set of rules decide the order of operations in an
expression
i
Operator
p Precedence clarifies unambiguously,
g y, which
operations should be performed first in a given
expression.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
24
Associativity of Operators
• Operator Associativity:
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
25
Structured Programming
• A method for designing and coding programs in a
systematic, organized manner.
• It combines
the principles of top-down
top down design,
design
modularity and
the
e use o
of the
e three
ee accep
accepted
ed co
control
o sstructures
uc u es o
of
sequence,
repetition and
selection.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
26
Control Structures
• Sequence –in sequential order.
The simplest of control structures –
start at the beginning and continue in sequential order.
• S
Selection
l ti – selectively
l ti l execute t statements
t t t
Called a branch,
it requires a condition to determine when to execute
statements.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
27
Control Structures
Repetition – repeat statements more than once
Called a loop,
p,
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
28
Counter-Controlled Loop: (Definite Repetition)
• Counter-controlled
Counter controlled loop:
Execute a set of statements repeatedly a specified
number of times
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
29
Event-Controlled Loop: (Indefinite Repetition)
• An event-controlled loop
Terminates when some event occurs.
occurs
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
30
Steps in Developing a Program
Developing a Program:
1. Analyze the problem
2 Design
2. D i th the program
3. Code the program
4 Test the program
4.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
31
Step-1 Analyze the Problem
Brewster’s Thousands
The problem: Brewster wants to invest money at a local
bank. There are many options such as interest rates,
terms of deposit, compounding frequencies. He needs a
program to compute, for any given initial investment, the
final maturity (value) of the deposit.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
32
Step-2 Design the Program
• Create an outline of the program
• Algorithm
g Examples:
p Instructions on how to make
a cake, use the bank’s ATM, etc.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
33
Step-3 Code the Program
• Once the design is completed, write the program
code.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
34
Errors in Development
• While writing a program, certain errors may get induced in the
code.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
35
Step-4 Testing the program
• Purpose:
P L
Locate
t any errors (bugs)
(b ) / problems
bl
• Testing
T ti is i done
d th
throughout
h t the
th development
d l t cycle
l
• Ulti
Ultimate
t test
t t is
i to
t run the
th program to
t see if the
th outputs
t t are correctt
for the given inputs.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
36
Good Programming Practice
• It is best not to take the “big bang” approach to coding.
• U
Use an incremental
i l approachh by
b writing
ii your code
d in
i
incomplete, yet working, pieces.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
37
Modular Programming
• Determine the major tasks that the program must
accomplish.
Each of these tasks will be a module.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
38
Code Modules
• A module is an independent, self-contained section of code
that performs a single task.
Typically,
yp ca y, the
e main
a module
odu e cacalls
soother
e modules
odu es in o
order
de to
o
have them perform certain tasks.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
39
Program Control & Modules
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
40
Structure Chart
• A structure chart shows what modules exist and how they
are related.
41
Documentation
• Internal Documentation
Comments explain to the reader the logic and decision
processes off the
th programmer.
Comments are ignored by an interpreter or compiler.
• External Documentation
External
e a docudocumentation
e a o includes
c udes a use
user’s
s gu
guide
de a and,
d,
typically, a more technical system administrator’s guide.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
42
Over to C Fundamentals…
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
43
CF
Fundamentals
d t l
U1.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
‹#›
44
Objectives
• History of C
• Characteristics of C
• Convertingg a C Program
g to Executable
• Dissecting a simple C Program
• g Variables in C
Defining
• Printing Out and Inputting Variables
• Constants & Literals
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
45
History of C
• Developed by Brian Kernighan and Dennis Ritchie of
AT&T Bell Labs in 1972
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
46
Features of C
• C can be thought of as a “high level assembler”
• M
Most popular
l programming
i l
language f writing
for ii system
software
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
47
Writing C Programs
• A programmer uses a text editor to create or modify files
g C code.
containing
• A file containing
g source code is called a source file.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
48
Getting an executable program
• The process of conversion from source code to machine
executable code is a multi step process.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
49
Conversion from .C to executable
1
1. Preprocessing
2. Compilation
p
3. Linking
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
50
Stage 1: Preprocessing
Performed by a program called the preprocessor
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
51
Stage 2: Compilation
Performed by a program called the compiler.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
52
Stage 3: Linking
• Combines the program object code with other object code to
produce the executable file.
p
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
53
Developing a C Program
Editor
Source File pgm.c
Preprocessor
Modified Source Code in RAM
Compiler
Program Object Code File
Linker
Executable File
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
54
A Simple C Program
/* Filename: hello.c
Author: --------
Date written: --/--/----
Description: This program prints the greeting
“Hello,
, World!”
*/
#include <stdio.h>
int main ( void )
{
printf ( “Hello,
Hello, World!\n”
World!\n ) ;
return 0 ;
}
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
55
Structure of a C Program
int main ( )
{
statement(s)
return 0 ;
}
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
56
Explanation
return 0;
}
flag success Hello
to operating Welcome to the Course!
“end”
system
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
57
Tokens
• The smallest element in the C language is the
token.
token
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
58
Tokens can be any of:
• Numeric Literals
• Character Literals
• String Literals
• Keywords
• Names (identifiers)
• Punctuation
• Operators
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
59
Numeric Literals
• Numeric literals are an uninterrupted sequence of
digits
• May contain
A period,
A leading
g + or – sign
g
A scientific format number
A character to indicate data type
• Examples:
E l
123
98.6
1000000
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
60
Character Literals
• Singular!
• One character defined character set.
• Surrounded on the single quotation mark.
• Examples:
‘A’
‘a’
‘$’
$
‘4’
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
61
String Literals
• A sequence characters surrounded by double quotation
marks.
• Considered a single item.
item
• Examples:
“UMBC”
“I like ice cream.”
“123”
“CAR”
CAR
“car”
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
62
Keywords
• Sometimes called reserved words.
• Are defined as a p
part of the C language.
g g
• Can not be used for anything else!
• Examples:
int
while
for
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
63
Reserved Words (Keywords) in C
64
Names / Identifiers
• Variables must be declared before use and immediately
after “{”
• Must begin with a letter and the rest can be letters, digits,
and
d underscores.
d
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
65
Which Are Legal Identifiers?
AREA area_under_the_curve
3D num45
Last-Chance #values
x_yt3 pi
num$$ %done
%
lucky*** continue
Float integer
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
66
Punctuation
• ; : , ‘ “{ } ( )
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
67
Things to remember
• Statements are terminated with semicolons
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
68
Variables and Constants in C
U1.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
‹#›
69
Topics
• Variables
Naming
Declaring
g
Using
• The Assignment Statement
• Constants
• Data Input & Output
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
70
Naming Conventions
• Begin variable names with lowercase letters
In addition to the conventions one must follow all the naming rules
as discussed in previous sessions.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
71
Declaring Variables
• All variables need to be declared before they are used.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
72
Declaring Variables
When we declare a variable
Space is set aside in memory to hold a value of the specified
data type
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
73
More About Variables
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
74
Integer Types in C
• C supports different kinds of integers, like
signed int
unsigned int
long int
short int
And more…
• Li
Limits
it depend
d d upon size
i i bytes
in b t which
hi h in
i turn
t d
depends
d
upon environment
maxima and minima defined in “limits.h”
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
75
The int Data Type (Contd.)
Use
scanf ((“%i”
%i , &ch) ;
to read a single integer into the variable ch.
Use
printf(“%i”,
i tf(“%i” ch)
h) ;
to display the value of an integer variable.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
76
The char Data Type
• The char data type holds a single character.
char ch;
• Example assignments:
• Th
The char
h isi held
h ld as a one-byte
b t integer
i t i memory. The
in Th
ASCII code is what is actually stored, so we can use
them as characters or integers, depending on our need.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
77
The char Data Type (Contd.)
Use
scanf (“%c”, &ch) ;
t read
to d a single
i l character
h t into
i t the
th variable
i bl ch.
h
Use
printf(“%c”, ch) ;
to display the value of a character variable.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
78
char I/O
#include <stdio.h>
int main ( )
{
char ch ;
Input: A
Output : The value of A is 65
65.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
79
Real Types In C
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
80
Notes About Variables
• You must not use a variable until you somehow give it a
value.
• You can not assume that the variable will have a value
b f
before you give
i it one.
Some compilers do, others do not! This is the
source of many errors that are difficult to find.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
81
Operators in C
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
82
Arithmetic Operators and their Usage
Name Operator Example
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
83
Relational Operators
< less than
> greater than
<= less than or equal to
>= greater than or equal to
== is equal
q to
!= is not equal to
Relational
R l ti l expressions
i evaluate
l t tto the
th integer
i t
values:
1 (true) on success
0 (false) on failure
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
84
Logical Operators
• At times we need to test multiple conditions in order to
make a decision.
• Logical operators combine simple conditions to make
complex conditions.
• Result into a 0 (false) or 1 (true)
|| OR if ( z == 0 || x > 10 )
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
85
Truth Table for &&
0 0 0
0 nonzero 0
nonzero 0 0
nonzero nonzero 1
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
86
Truth Table for ||
Expression1 Expression2 Expression1 || Expression2
0 0 0
0 nonzero 1
nonzero 0 1
nonzero nonzero 1
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
87
Truth Table for !
Expression ! Expression
0 1
nonzero 0
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
88
Precedence/Associativity Table
Operator Associativity
() [] -> . left to right
! ~ ++ -- - + (cast) * & sizeof right to left + - : unary
* / % left to right
+ - left to right
<< >> left to right
< <= >= > left to right
== != left to right
& left to right
| left to right
^ left to right
&& left to right
|| left to right
?: right to left
= += -= *= /= %= etc right to left
, left to right
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
89
U
User IInteraction
t ti ini C
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
90
Functions
• Functions are named code blocks that make reusability of
code possible.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
91
I/O Example
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
92
Displaying Variables
• A Function that allows us to display formatted data:
printf( ).
)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
93
printf( “%f\n”, diameter );
• The name of the function is “printf”.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
94
scanf (“%f”, &meters) ;
• This function is used to
read values from the standard input; and
Store them in memory.
memory
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
95
Format Specifiers
• Format specifiers are normal strings with embedded
“conversion
conversion specifications
specifications” which are placeholders for
arguments
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
96
Selection: the if statement
if ( condition )
{
( )
statement(s) /* body
/ y of the if statement */
/
}
• The braces are not required if the body contains only a single
statement.
• However, they
H th are a good
d idea
id and
d are required
i d by
b the
th C Coding
C di
Standards.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
97
Examples
if ( age >= 18 )
{
printf(“Vote!\n”) ;
}
if ( value == 0 )
{
printf (“The value you entered was zero.\n”);
printf (
p (“Please try
y again.\n”)
g \ ) ;
}
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
98
Some Control Constructs in C
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
99
Selection: the if-else statement
if ( condition )
{
statement(s) /* the if clause */
}
else
{
statement(s) /* the else clause */
}
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
100
Example
if ( age >= 18 )
{
printf(“Vote!\n”) ;
}
else
{
printf(“Maybe next time!\n”) ;
}
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
101
Repetition Structure
• There are three repetition structures in C: -
the while loop
the
th for
f loop
l
the do-while loop.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
102
The while Repetition Structure
while ( condition )
{
statement(s)
}
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
103
while Loop
• The simplest C loop is the while
• Parentheses must surround the condition
• Condition could be based on user input or some calculation
• Braces must be added if more statements are to be executed
total= 0;
while (
(number != -1)
) {
total = total + number;
printf(“Enter another number: “) ;
scanf(“%d”
scanf( %d , &number) ;
}
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
104
The for Loop Repetition Structure
• The for loop handles details of the counter-controlled loop
“automatically”.
}
initialization test modification
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
105
A for Loop That Counts From 0 to 9
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
106
We Can Count Backwards, Too
for ( i = 9; i >
>= 0; i = i - 1 )
{
printf (“%d\n”,
( %d\n , i) ;
}
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
107
Count By 2’s or 7’s or Whatever
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
108
The do-while Repetition Structure
do
{
statement(s)
t t t( )
} while ( condition ) ;
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
109
Example
do
{
printf (“Enter a number, 0 to terminate: “);
scanf (“%d”, &number) ;
if (number == 1)
printf (“\n Do Something\n”) ;
else if (number == 2)
printf (“\n Do Something Else\n”) ;
} while ( num != 0 );
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
110
An Equivalent while Loop
printf (“Enter a number, 0 to terminate:“) ;
scanf (“%d”, &number) ;
while ( number != 0 )
{
if (number == 1)
printf
i tf (“\
(“\n Do
D S thi \ ”) ;
Something\n”)
else if (number == 2)
printf (“\n Do Something Else\n”) ;
printf
i f (“
(“Enter a number
b “) ;
, 0 to terminate:“)
i
scanf (“%d”, &number) ;
}
111
An Equivalent for Loop
printf (“Enter a positive number, 0 to terminate: “) ;
scanf (“%d”, &number) ;
for ( ; number != 0; )
{
if (number == 1)
printf (“\n Do Something\n”) ;
else if (number == 2)
printf (
p (“\n Do Something
g Else\n”)
) ;
printf (“Enter a number , 0 to terminate:“) ;
scanf (“%d”, &number) ;
}
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
112
So, Which Type of Loop Should One Use?
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
113
That s all for now
That’s now…
We are ready for writing a simple C program.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal U1.
‹#›
114