Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 104

FUNDAMENTALS

PROGRAMMING
DEC20012 :

CHAPTER 1
COURSE LEARNING OUTCOME (CLO) :
CLO1 (Apply knowledge of basic concepts and fundamentals of
structured programming in solving a variety of engineering and
scientific problems using a high level programming language.
 The set of characters used to form words numbers and
expressions
 The characters in C are grouped into the following two

2.1 Know the Variables, Constants


categories:

1. Source Character Set


a. Alphabets
b. Digits
c. Special Characters
d. White Spaces

and Data Types


2. Execution Character Set
a. Escape Sequence

2
 Source Character Set
* ALPHABETS
Uppercase letters A-Z

2.1 Know the Variables, Constants


Lowercase letters a-z

* DIGITS
0, 1, 2, 3, 4, 5, 6, 7, 8, 9

* WHITESPACE CHARACTERS
Space, Horizontal tab, Vertical Tab, Form Feed, New Line

and Data Types


3
Symbol Meaning
| Vertical bar
 Source Character Set \
`
Backslash
Apostrophe
* SPECIAL CHARACTERS - Minus sign

2.1 Know the Variables, Constants


= Equal to sign
Symbol Meaning { Left brace
~ Tilde } Right brace
! Exclamation [ Left bracket
mark ] Right bracket
# Number sign : Colon
$ Dollar sign " Quotation mark
% Percent sign ; Semicolon
^ Caret < Opening angle

and Data Types


& Ampersand bracket
* Asterisk > Closing angle
( Lest parenthesis bracket
) Right parenthesis ? Question mark
_ Underscore , Comma
+ Plus sign . Period
4
 Execution Character Set
ESCAPE SEQUENCE

2.1 Know the Variables, Constants


Escape Character Result Escape Character Result
Sequen Sequen
ce ce
\b blank Moves previous \\ Back slash Present
space position back slash
\t horizontal Moves next \’ Single Present
tab horizontal tab quote Apostrophe
\v vertical tab Moves next \" Double Present

and Data Types


vertical tab quote Double
\r carriage Moves quotes
return beginning of \? Question Present
the line mark Question
\f form feed Moves initial Mark
position of next \0 Null Null 5
 There are 4 types of data in C :
int

2.1 Know the Variables, Constants


char
double
float

and Data Types


6
INTEGER
(int)
 Keyword int is used to declare an integer numbers

2.1 Know the Variables, Constants


variables type. Direct Access Storage

Syntax : int variable_name; bilangan


Example : int bilangan;
int num1, num2; num1

 num2 space
This declaration tells the compiler that the memory

and Data Types


required to hold the data type of integer called bilangan,
num1 and num2.

7
CHARACTER
(char)
 Keyword char is used to declare a character variables type.

2.1 Know the Variables, Constants


Syntax : char variable_name;

Example : char huruf;


char name [12];
 This declaration tells the compiler that the memory space

and Data Types


required to hold the data type of character called huruf, and
name.

8
CHARACTER
(char)
 Variable characters store any print or non-printed

2.1 Know the Variables, Constants


characters in computer character sets include:
Capital and lowercase letters
Decimal digits (0-9)
Special Characters

and Data Types


 These characters are represented in one byte (8
bits) in the computer memory.

9
FLOATING POINT
(float/double)

2.1 Know the Variables, Constants


Keyword double is used to declare a
floating point variables type.
 There are 3 types of floating point in C :
◦ float

and Data Types


◦ double
◦ long double
10
FLOATING POINT
(float/double)
The three types differ in terms of the range of values ​and

2.1 Know the Variables, Constants


the number of digits after the decimal point can be
represented.
 An example of IBM compatible systems and Turbo C++ :
* float 1.175494e-38 to 3.402823e+38
(6 decimal places)
* double 2.225074e-308 to 1.797693e + 308

and Data Types


(15 decimal places)
* long double 3.362103e-4932 to 1.189731e + 4932
(19 decimal places)
11
Keyword Full Name Size* Range*
signed: -128 to
1
Character or 127

2.1 Know the Variables, Constants


char by
small integer unsigned: 0 to
te
255
signed: -32767 to
1
32768
int Integer wo
unsigned: 0 to
rd
65535

and Data Types


Boolean, can 1
1 (true) or 0
bool take either true by
(false)
or false. te
4
Floating point 3.4e +/- 38 (7
float by 12
 Keywords are the reserved words

2.1 Know the Variables, Constants


used in programming.
 Each keywords has fixed meaning
and that cannot be changed by
user.
 C programming is case sensitive,

and Data Types


all keywords must be written in
lowercase.
13
2.1 Know the Variables, Constants
and Data Types
14
2.2 Understand The Fundamentals Of C
Programme
15
2.2 Understand The Fundamentals Of C

The C program starting point is
identified by the word main()

This inform the computer as to where
the program actually start

The 2 braces { and } signify the begin
and end segment of the program

Programme
16
2.2 Understand The Fundamentals Of C
Header File

Contain definition function and variables which
can be incorporated into any C program by
using the pre-processor #include statement

To use any of the standard function, the
appropriate file should be included

All header file have the extension of “.h” and
generally reside in the /usr/include

Programme
subdirectories

Example : #include<math.h>, #include<string.h>
17
2.2 Understand The Fundamentals Of C
Header File

The use of angle “< >” inform
the compiler to search
compiler include directories
for the specified files

Programme
18
2.2 Understand The Fundamentals Of C
Return Statement

The return statement terminates the
execution of a function and returns
control to the calling function.

Execution resumes in the calling
function at the point immediately

Programme
following the call
19
2.2 Understand The Fundamentals Of C
The Usage of Comments

Why use comment ?
 Documentation of variables and
function of their usage
 Explaining difficult section of code
 Describe the program, author , date,
modification changes, revision,etc

Programme

Use /* close */ or // for single line

20
The symbolic name in functional and variable

2.2 Understand The Fundamentals Of C


such as number1 & sum is called the identifier.
InC, the identifier is use as official name for :
functions, variables, constants and labels.
The identifier in C must meet the following
conditions :
 begins with the alphabet or underscore (_)
 consists of letters (A. .. Z, a. .. z), digits (0 ... 9)
or character (_)

Programme
 Not the C reserved word (keyword)
21
2.2 Understand The Fundamentals Of C
Valid Invalid Reason?
polipd 2polipd

_bek meow?

rekod_201 rekod-301

Gross_Pay Gross pay

Programme
22
2.2 Understand The Fundamentals Of C
Valid Invalid Reason?
polipd 2polipd Begin with
number
_bek meow? Illegal
character
rekod_201 rekod-301 Illegal

Programme
character
Gross_Pay Gross pay Illegal blank
23
2.2 Understand The Fundamentals Of C
 The identifier name design
by the user for the purpose
of definition is :
1. variable
2. constant
3. function

Programme
24
2.2 Understand The Fundamentals Of C
Variable

A Variable is a name given to the memory
location where the actual data is stored

Variable name may have different data
types to identify the type of value stored.

Suppose we declare variable of type integer
then it can store only integer values.

Programme
25
Variable

2.2 Understand The Fundamentals Of C


Can consist of letters @ number @ underscore "_"
Example: NAME, NAMA_BAPA, NUMBER1

Case sensitive
Example: AGE, Age, AgE, agE is a different variables

SAME AS IDENTIFIER
Cannot start with numbers
Example: 1NAMA

Programme
Keyword cannot be used as a variable names
Example: printf, while, loop

26
Variable

2.2 Understand The Fundamentals Of C


IS THIS A LEGAL VARIABLES ?
“x” pay_income
JUMLAH main
x+y 17th
Sumx2 Kadar+Cukai
3ing continue
hourly-rate bil_aksara

Programme
gross pay item 10
Hasil2 _KIRAJUMLAH
27
Direct Access Storage

3.2 Understand The Fundamentals Of C


Variable Declaration no
To declare a
#include <stdio.h>
#include <conio.h> variable
named no and
the type is integer
main()
{
int no;

printf(" Sila masukkan nombor kegemaran : “);


scanf(“%d”,&no);
printf("Nombor kegemaran anda ialah: \n");

Programme
printf(“%d”, no);
getch();
return 0;
}
DEC2012 - FUNDAMENTAL PROGRAMMING 28
Direct Access Storage

no1

3.2 Understand The Fundamentals Of C


Variable Declaration no2
#include <stdio.h> To declare
#include <conio.h> variables named
purata
no1, no2, purata
main()
{ and the type is integer
int no1, no2, purata;
printf(" Sila masukkan nombor 1 : \n“);
scanf(“%d”,&no1);
print("Sila masukkan nombor 2 : “);
scanf(“%d”,&no2);
purata = (no1 + no2)/2;

Programme
printf("Purata 2 no ialah:%d”,purata);
getch();
return 0;
}
29
Variable Declaration

2.2 Understand The Fundamentals Of C


 Examples :
char code;
float
kadarBayaran;
double jum_pendapatan;
int a, b;
??? ???
code kadarBayaran

Programme
??? ??? ???

jum_pendapatan a b 30
Variable Assignment

2.2 Understand The Fundamentals Of C


 There are TWO ways to assign initial values ​to variables:
◦ Using the assignments statement after declaration

Examples :
int i;
.
.
i = 0;
◦ Assigns initial values ​during declaration
Examples :
int i = 0;

Programme
int a, b = 0;

31
Variable Assignment

2.2 Understand The Fundamentals Of C


 Assigning initial values to variables is necessary to avoid garbage
errors. int lebar = 20;
int tinggi = 10;
int panjang = 10;
int luas, isipadu;
luas = lebar * tinggi;
isipadu = panjang * lebar * tinggi;

20 10
lebar tinggi

Programme
10 200 ???

panjang luas isipadu


32
Variable Assignment

2.2 Understand The Fundamentals Of C


 Assigning initial values to variables is necessary to avoid garbage
errors. int lebar = 20;
int tinggi = 10;
int panjang = 10;
int luas, isipadu;
luas = lebar * tinggi;
isipadu = panjang * lebar * tinggi;

20 10
lebar tinggi

Programme
10 200 2000

panjang luas isipadu


33
Constant

2.2 Understand The Fundamentals Of C


Entities that appear in the program code as a fixed
value.
Example: Price for photocopy paper RM 0.06

There are four classes of constants in C which will


be discussed:
 Integer

Programme
 Floating point
 Character
34
Integer Constant

2.2 Understand The Fundamentals Of C


 Some rules for writing a valid integer constant for the C :
 Must begin with a non-zero digit (if there is more than one
digit)
 May consist of the digits 0 to 9
 Ifthere is no sign (+ or -) in a constant integer, then the
computer will consider it as a positive
 Comma (,) is not allowed. For example: 1,500,000 is invalid. It
should be written as 1500000.

Programme
 Spaces between the digits are not allowed
Valid or Not ?  -15 0179 1,756 +250 7500
0
35
Floating Point Constant

2.2 Understand The Fundamentals Of C


 Positive and negative decimal numbers with the integers,
the point and fractional part.
 It can be represented with ordinary notation or scientific
notation.
Example : 20.35 (ordinary notation)  0.2035 x 102 (scientific notation)

 Scientific form is written in C as 0.2035E+2 or 0.2035e+2.

Programme
 (‘E’ @ ‘e’ is exponential)

36
Floating Point Constant

2.2 Understand The Fundamentals Of C


 There are three types of floating point
constants in C:
float

double

long double

Programme
37
Character Constant

2.2 Understand The Fundamentals Of C


 A character enclosed by sign-opener ‘ and ' (single quote).
◦ Examples : ‘ A’ ‘z’ ‘?’ ‘.’ ‘5’ ‘1’ ‘$’
◦ Output : A z ? . 5 1 $
without ‘ ’
 A space that enclosed by the opening pair is also a constant
singular character.
Example : ‘ ’

Programme

38
Character Constant

2.2 Understand The Fundamentals Of C


 There are special characters that start with \ (backslash).
 This character is the character that cant see such as :
\ n  represents a new line
\ 0  represent the NULL character
\ t  represents a tab
\ a  to produce sound when a message displayed

Programme
39
Quiz
Constant

3.2 Understand The Fundamentals Of C


 Provide a space for a value that does not change
throughout the implementation of the program
 3 techniques :
 #define
 Syntax - #define pi 3.142
 Const
 Syntax - const float pi = 3.142;

Programme
 Enum – there is some values
 Syntax - enum Color
Click the
{RED, BLUE, GREEN};
Quiz button on iSpring Pro
toolbar to edit your quiz
DEC2012 - FUNDAMENTAL PROGRAMMING 40
 A numeric constant / string / a variable / value

2.2 Understand The Fundamentals Of C


combined with constant, variable and other
expressions.
 Examples :
A= 4+9
CLASS = “DTP5”
JUM = NO * NUM

Programme
41
2.2 Understand The Fundamentals Of C
Which one
#include <stdio.h> is the
#include <conio.h> Expression?
main()
{
int jumlah;
jumlah =4+9;
printf(“jumlah ialah:%d”,jumlah);
getch();

Programme
return 0;
}

42
Whenever and input/output function

2.2 Understand The Fundamentals Of C


(printf() and scanf()) is
used, the program should include a
header file stdio.h as follows :

#include<stdio.h>

Programme
43
The printf()Function

2.2 Understand The Fundamentals Of C


 printf() send data to the output device regarding to
a given format.
 The format is :
printf(“Print the output statement”);
 Example 1:

Coding

printf(“Politeknik Port Dickson”);

Programme
Output

Politeknik Port Dickson

44
The printf()Function

2.2 Understand The Fundamentals Of C


 The format string contains details on how the data appears.
 It may consist of format specifications (%d %f %c %s).
 There is often a “%” sign followed by a letter or a “\” followed by a
letter.
 Example 2 : A %d tells C to insert a decimal integer
value at that place in the string

Coding

printf(“Here is an integer : %d \n”, 5);


Output

Programme
Here is an integer : 5

45
The printf()Function

2.2 Understand The Fundamentals Of C


 There a several format specifications :
 %d – print an integer
 %u - print an unsigned integer
 %ld – print a long integer
 %f – print a float
 %lf – print a long float (double)
 %e – print a floating point in scientific notation

Programme
 %c – print a character
 %s – print a string

46
The printf()Function

2.2 Understand The Fundamentals Of C


 Any occurences of the “\” character inside a string is called an
escape sequence.
 Some common escape sequences :
 \’ - output a single quote ( ‘) character
 \’’ - output a double quote ( ‘‘) character
 \? - output a question mark (?) character
 \\ - output a back slash (\) character
 \a - cause an audible (bell) or visual alert
 \b - move the cursor back one position on the current line
 \f - move the cursor to the start of the next logical page
 \n - move the cursor to the beginning of the next line

Programme
 \r - move the cursor to the beginning of the current line
 \t - move the cursor to the next horizontal tab position
 \v - move the cursor to the next vertical tab position
47
Printing Integers Numbers(%d)

2.2 Understand The Fundamentals Of C


 Example 1 :
Coding
int num1;
num1= 5;
printf(“The Integer Number is = %d”, num1);

Output
The Integer Number is = 5

Programme
48
Printing Floating Numbers(%f)

2.2 Understand The Fundamentals Of C


 Example 1 :
Coding
float num1;
num1= 33.77;
printf(“The Float Number is = %f”, num1);

Output

Programme
The Float Number is = 33.77

49
Printing Floating Numbers(%f)

2.2 Understand The Fundamentals Of C


 Example 2 :
Coding
printf(“%.1f\n”, 12.345678);
printf(“%.2f\n”, 12.345678);
printf(“%.3f\n”, 12.345678);
Output

12.3

Programme
12.35 The output is rounded to 2 decimal places
12.346
50
Printing Strings(%s)

2.2 Understand The Fundamentals Of C


 You can specify a field by placing a decimal
number between the % and s.
 If the field is larger than the data requires, it is
padded with spaces on the left
 Example 1 :
Coding
printf(“Computer”);
printf(“%13s \n”, “Computer”);

Programme
Output
Computer
Computer 51
Printing Strings(%s)

2.2 Understand The Fundamentals Of C


 If the width parameter is omitted, then the
field width is assumed to be the default
width (the length of the string).
 Example
Coding 2:
printf(“Computer”);
printf(“%4s \n”, “Computer”);
Output

Programme
Computer
Computer

52
Printing Characters(%c)

2.2 Understand The Fundamentals Of C


 Same as string, you can specify a field
width by placing a decimal number
between the % and c.
 Example
Coding
1:
printf(“%c%c%c%c”, ‘J’,’U’,’N’,’E’);

Output

Programme
JUNE

53
Printing Characters(%c)

2.2 Understand The Fundamentals Of C


 Example 2 :
Coding
char let1, let2, let3, let4;
let1 = ‘J’;
let2 = ‘U’;
let3 = ‘N’;
let4 = ‘E’;
printf(“%c%c%c%c”,let1,let2,let3,let4);
Output

Programme
JUNE

54
Displaying Several Values At Once

2.2 Understand The Fundamentals Of C


 A single printf()can be use to display
several values.
 Example 1 :
Coding
int year = 21;
float height = 1.73;
printf(“Ahmad is %d years old and %.2f m height”,
year, height);

Programme
Output

Ahmad is 21 years old and 1.73 m height


55
The scanf() Function

2.2 Understand The Fundamentals Of C


 The scanf()function allows users to
communicate with the program.
 It is an input from the keyboard.
 The program pauses and waits for you to
input a line from the keyboard.
 After the line has been input and <ENTER>
pressed, C makes the assignment of the

Programme
input data to the variables (var1, var2, var3
…. )
56
The scanf()Function

2.2 Understand The Fundamentals Of C


 Syntax :
scanf (“format string”, &var1, &var2, …, &varn);

• Format of the data to be • Variables whose types may


input. be int, char, float in
• Consist of format which the input will be
specifications stored
such as %c, %d,etc

Programme
57
The scanf()Function

2.2 Understand The Fundamentals Of C


 Format specifications for scanf()are :
 %d – read an integer
 %f – read a float
 %lf – read a long float (double)
 %c – read a character
 %s – read a string

Programme
58
The scanf()Function

2.2 Understand The Fundamentals Of C


 Example 1 :
Coding
scanf(“%c %d”, &num1, &num2);

A character that will


be stored in variable num1

Programme
An integer that will be
stored in variable num2
59
The scanf()Function

2.2 Understand The Fundamentals Of C



Example 2 :
Coding
int day, month, year;
printf(“Please enter a date :\n”);
scanf(“%d %d %d”, &day, &month, &year);
printf(“Day: %d\t Month:%d\t Year:%d”, day, month, year);

Output
Please enter a date:

Programme
17 8 1996
Day: 17 Month: 8 Year:1996
60
 To perform a mathematical

2.2 Understand The Fundamentals Of C


operations @ logical to a value
 3 Types of operators :
Assignment Operators
Mathematical Operators
Relational Operators
Logical Operators

Programme
Unary Operators

61
Assignment Operators (=)

2.2 Understand The Fundamentals Of C


 Assignment operator is used in assigning values.
 In general, assignment operator assigns the value of the
right hand side operand to Left hand side Operand.
 Assignment operator is binary operator  so it must need
two operands.
 Assignment operator symbol is = (equal to).
 Assignment operator have two values those are Left-side
value also called as L-Value and Right-side Value also
called as R-Value.

Programme
 Left-side value of Assignment Operator must be Variable.

62
Assignment Operators (=)

2.2 Understand The Fundamentals Of C


There are following assignment operators
supported by C language:
Operat
Description Example
or
Simple assignment operator, Assigns
C = A + B will assign
= values from right side operands to left
value of A + B into C
side operand
Add AND assignment operator, It
C += A is equivalent to C
+= adds right operand to the left operand
=C+A
and assign the result to left operand
Subtract AND assignment operator, It
subtracts right operand from the left C -= A is equivalent to C =

Programme
-=
operand and assign the result to left C - A
operand
Multiply AND assignment operator,
It multiplies right operand with the C *= A is equivalent to C = 63
*=
Assignment Operators (=)

2.2 Understand The Fundamentals Of C


There are following assignment operators
supported by C language:
Operat
Description Example
or
Divide AND assignment operator,
It divides left operand with the right C /= A is equivalent to C = C
/=
operand and assign the result to left / A
operand
Modulus AND assignment
operator, It takes modulus using C %= A is equivalent to C =
%=
two operands and assign the result C % A

Programme
to left operand
C <<= 2 is same as C = C <<
<<= Left shift AND assignment operator
2
Right shift AND assignment C >>= 2 is same as C = C >>
>>=
operator 2 64
Assignment Operators (=)

2.2 Understand The Fundamentals Of C


Operator Example Meaning
+= n += 5 n = n + 5

-= n -= 5 n = n - 5
*= n *= 5 n = n * 5

/= n /= 5 n = n / 5

Programme
%= n %= 5 n = n % 5

65
Assignment Operators (=)
#include <stdio.h>

2.2 Understand The Fundamentals Of C


#include <conio.h>
main() 22
{
int n = 22;
n += 9; n
printf(“Original value of n = “,n);
n -= 5;
printf(“After n -= 5 = “,n); Original value of n = 22
n *= 2; After n += 9, n = 31
printf(“After n *= 2 = “,n); After n -= 5, n = 26
n /= 3; After n *= 2, n = 52
printf(“After n /= 3 = “,n); After n /= 3, n = 17
n %= 7; After n %= 7, n = 3

Programme
printf(“After n %= 7 = “,n);
getch();
return 0;
}
66
Mathematical Operators

3.2 Understand The Fundamentals Of C


Used in mathematic expressions
Operat
Function Example
or
+ Addition var=a+b
– Subtraction var=a-b
* Multiplication var=a*b
/ Division var=a/b
% Modulus var=a%b
++ Increment var++

Programme
-- Decrement var--
+ Unary plus +var
– Unary minus -var 67
Mathematical Operators

3.2 Understand The Fundamentals Of C


 Thecombination of one or more arithmetic
operations
 Examples :
5+6
21 – 6 + 7 * 4 / 2
 Evaluated according to priority level
(PRECEDENCE)

Programme
68
Mathematical Operators

3.2 Understand The Fundamentals Of C


Priority Level

Priority Operato
(PRECEDENCE)
Level r
1 (High) ( )
2 * / %

Programme
3 (Low) + -
69
Mathematical Operators

3.2 Understand The Fundamentals Of C


 Example :

(9–(3+2))*3

(9–5)*3

4*3

12

Programme
70
Mathematical Operators

3.2 Understand The Fundamentals Of C


Algebra Expression C Arithmetic Expression
14 – 5 + 6 14 – 5 + 6

16 – 8 16 – 8 / 2
2

15 + 5 + 7 ( 15 + 5 + 7 ) / 2

Programme
2

71
Mathematical Operators

3.2 Understand The Fundamentals Of C


 Example:

Arithmetic Expression C Expression

4+2x3-(5÷ 4 + 2 * 3 – (5 /
2) 2)

Programme
X3 + 5 mod 2 X^3+5%2
DEC2012 - FUNDAMENTAL PROGRAMMING 72
Mathematical Operators
DIRECT CALCULATION EXAMPLE

3.2 Understand The Fundamentals Of C


#include <stdio.h>
#include <conio.h>
main()
{
int jumlah;
jumlah = 4+9;
printf(“jumlah ialah:%d”,jumlah);
printf (“\n”);
getch();
return 0;

Programme
}

DEC2012 - FUNDAMENTAL PROGRAMMING 73


Mathematical Operators
INPUT CALCULATION EXAMPLE

3.2 Understand The Fundamentals Of C


#include <stdio.h>
#include <conio.h>
main()
{
int no1, no2 ,jumlah;
printf(“masukkan no 1 :”);
scanf(“%d”,&no1);
printf(“\nmasukkan no 2 :”);
scanf(“%d”,&no2);
jumlah = no1 + no2;
printf(“\nJumlah ialah :%d”,jumlah);
getch();

Programme
return 0;
}

DEC2012 - FUNDAMENTAL PROGRAMMING 74


Relational Operators

3.2 Understand The Fundamentals Of C


A comparison of 2
values or expressions
producing a value
which is
true @ false

Programme
DEC2012 - FUNDAMENTAL PROGRAMMING 75
Relational Operators

3.2 Understand The Fundamentals Of C


If TRUE = 1 , FALSE = 0
Operator Meaning Expressi Valu
on e
< Less Than 6<9 1
<= Less Than or Equal 5 <= 5 1
To
> Greater Than 2>6 0
>= More Than or 9 >= 5 1

Programme
Equal To
== Equal To 7 == 5 0
DEC2012 - FUNDAMENTAL PROGRAMMING 76
Relational Operators

3.2 Understand The Fundamentals Of C


If x=4, y=7, what is the result for:
Symbol Operator Comparison
== Equal to false ? x == y
> Greater than true ? y > x
< Less than true ?x<y
!= Not equal to true ? y != x

Programme
<= Less than or equal to true ? x <= y
>= More than or equal tofalse ? x >= 5
DEC2012 - FUNDAMENTAL PROGRAMMING 77
Logical Operators

3.2 Understand The Fundamentals Of C


 To test 2 conditions that will give you
a value either true or false.
 The results based on the Truth Table
 3 types of logical operators :
 NOT (!)
 AND (&&)

Programme
 OR (||)
DEC2012 - FUNDAMENTAL PROGRAMMING 78
Logical Operators

3.2 Understand The Fundamentals Of C


 NOT Operator - !

Conditio NOT
n1
1 0
0 1

Programme
79
Logical Operators

3.2 Understand The Fundamentals Of C


 AND Operator - &&

Condition Condition AND


1 2
1 1 1
1 0 0
0 1 0

Programme
0 0 0
80
Logical Operators

3.2 Understand The Fundamentals Of C


 OR Operator - ||

Condition Condition OR
1 2
1 1 1
1 0 1
0 1 1

Programme
0 0 0
81
Logical Operators

3.2 Understand The Fundamentals Of C


#include <stdio.h>
#include <conio.>
main ()
{
int markah, hadir;

printf( "sila masukkan markah : “);


scanf (“%d”,&markah);

printf( "sila masukkan kehadiran : “);


scanf(“%d”,&hadir);

if (markah >= 50 && hadir >= 80)


printf( "layak“);
else

Programme
printf( "tak layak“);
getch();
return 0;
}
82
Unary Operators

3.2 Understand The Fundamentals Of C


A unary operator is an
operator, which operates on
one operand. i.e. it operates on
itself.

Programme
83
Unary Operators : Increment &
Decrement

3.2 Understand The Fundamentals Of C


A unique operator  need ONE operand
only
Symb Meaning Expressio Value
ol n
++ Increment i++ i = i + 1
1
-- Decrement i-- i = i – 1
1

Programme
84
Unary Operators : Increment &
Decrement

3.2 Understand The Fundamentals Of C


 TWO types of increment & decrement operator:
◦ Prefix (before)
◦ Postfix (after)

Expressi Meaning
on
i++ Add the value of i after used in the
expression

Programme
++i Add the value of i before used in the
expression
i-- Minus the value of i after used in the 85
Quiz
Unary Operators : Increment &
Decrement

3.2 Understand The Fundamentals Of C


* Postfix Example :
1. i = 5; i 5 j
j = i++ - 2;
i 5 3 j
i 6 3 j

* Prefix Example :
i 5 j
2. i = 5;

Programme
j = ++i – 2; i 6 j
Click the i 6
Quiz button on iSpring Pro 4 j
toolbar to edit your quiz
DEC2012 - FUNDAMENTAL PROGRAMMING 86
3.3 Apply the fundamentals of C
Programming
87
1. Click Dev C++
icon
2. To create a source file
File  New  Source File

3.3 Apply the fundamentals of C


Programming
88
3.3 Apply the fundamentals of C
Programming
89
3. Type the program
4. Compiling the program

3.3 Apply the fundamentals of C


Programming
DEC2012 - FUNDAMENTAL PROGRAMMING 90
4. Execute the program

3.3 Apply the fundamentals of C


Output :

Programming
DEC2012 - FUNDAMENTAL PROGRAMMING 91
A memory location showing
 Adding Two Integers : the name of variable

integer1 ?

integer2 ?

3.3 Apply the fundamentals of C


sum ?

Programming
DEC2012 - FUNDAMENTAL PROGRAMMING 92
 Adding Two Integers :

Output :

3.3 Apply the fundamentals of C


Programming
DEC2012 - FUNDAMENTAL PROGRAMMING 93
3.2 Understand The Fundamentals Of C
#include <stdio.h>
#include <conio.h>

main( )
{
int a, b, c, jum ;

printf("Selamat Datang.");
printf("\nAturcara ini menjumlahkan 3 nombor.");
printf("\nMasukkan 3 nombor dalam bentuk: nnn nnn nnn
<enter>");
scanf("%d%d%d",&a,&b,&c);
jum = a + b + c;
printf("\nJumlah adalah:%d",jum);
printf("\nTerima kasih. ");

Programme
A memory location showing
getch(); the name of variable
return 0;
? ? ? ?
}
a b c jum
DEC2012 - FUNDAMENTAL PROGRAMMING 94
Selamat Datang._

3.2 Understand The Fundamentals Of C


#include <stdio.h>
#include <conio.h>

main( )
{
int a, b, c, jum ;

printf("Selamat Datang.");
printf("\nAturcara ini menjumlahkan 3 nombor.");
printf("\nMasukkan 3 nombor dalam bentuk: nnn nnn nnn
<enter>");
scanf("%d%d%d",&a,&b,&c);
jum = a + b + c;
printf("\nJumlah adalah:%d",jum);
printf("\nTerima kasih. ");

Programme
A memory location showing
getch(); the name of variable
return 0;
? ? ? ?
}
a b c jum
DEC2012 - FUNDAMENTAL PROGRAMMING 95
Selamat Datang.
Aturcara ini menjumlahkan 3 nombor. _

3.2 Understand The Fundamentals Of C


#include <stdio.h>
#include <conio.h>

main( )
{
int a, b, c, jum ;

printf("Selamat Datang.");
printf("\nAturcara ini menjumlahkan 3 nombor. ");
printf("\nMasukkan 3 nombor dalam bentuk: nnn nnn nnn
<enter>");
scanf("%d%d%d",&a,&b,&c);
jum = a + b + c;
printf("\nJumlah adalah:%d",jum);
printf("\nTerima kasih. ");

Programme
A memory location showing
getch(); the name of variable
return 0;
? ? ? ?
}
a b c jum
DEC2012 - FUNDAMENTAL PROGRAMMING 96
Selamat Datang.
Aturcara ini menjumlahkan 3 nombor.
Masukkan 3 nombor dalam bentuk: nnn nnn nnn <enter> _

3.2 Understand The Fundamentals Of C


#include <stdio.h>
#include <conio.h>

main( )
{
int a, b, c, jum ;

printf("Selamat Datang.");
printf("\nAturcara ini menjumlahkan 3 nombor. ");
printf("\nMasukkan 3 nombor dalam bentuk: nnn nnn nnn
<enter>");
scanf("%d%d%d",&a,&b,&c);
jum = a + b + c;
printf("\nJumlah adalah:%d",jum);
printf("\nTerima kasih. ");

Programme
A memory location showing
getch(); the name of variable
return 0;
? ? ? ?
}
a b c jum
DEC2012 - FUNDAMENTAL PROGRAMMING 97
Selamat Datang.
Aturcara ini menjumlahkan 3 nombor.
Masukkan 3 nombor dalam bentuk: nnn nnn nnn <enter> 10 20 30 _

3.2 Understand The Fundamentals Of C


#include <stdio.h>
#include <conio.h>

main( )
{
int a, b, c, jum ;

printf("Selamat Datang.");
printf("\nAturcara ini menjumlahkan 3 nombor. ");
printf("\nMasukkan 3 nombor dalam bentuk: nnn nnn nnn
<enter>");
scanf("%d%d%d",&a,&b,&c);
jum = a + b + c;
printf("\nJumlah adalah:%d",jum);
printf("\nTerima kasih. ");

Programme
A memory location showing
getch(); the name & value of variable
return 0;
10 20 30 ?
}
a b c jum
DEC2012 - FUNDAMENTAL PROGRAMMING 98
Selamat Datang.
Aturcara ini menjumlahkan 3 nombor.
Masukkan 3 nombor dalam bentuk: nnn nnn nnn <enter> 10 20 30 _

3.2 Understand The Fundamentals Of C


#include <stdio.h>
#include <conio.h>

main( )
{
int a, b, c, jum ;

printf("Selamat Datang.");
printf("\nAturcara ini menjumlahkan 3 nombor.");
printf("\nMasukkan 3 nombor dalam bentuk: nnn nnn nnn
<enter>");
scanf("%d%d%d",&a,&b,&c);
jum = a + b + c;
printf("\nJumlah adalah:%d",jum);
printf("\nTerima kasih.");

Programme
A memory location showing
getch(); the name & value of variable
return 0;
10 20 30 60
}
a b c jum
DEC2012 - FUNDAMENTAL PROGRAMMING 99
Selamat Datang.
Aturcara ini menjumlahkan 3 nombor.
Masukkan 3 nombor dalam bentuk: nnn nnn nnn <enter> 10 20 30
Jumlah adalah: 60 _

3.2 Understand The Fundamentals Of C


#include <stdio.h>
#include <conio.h>

main( )
{
int a, b, c, jum ;

printf("Selamat Datang.");
printf("\nAturcara ini menjumlahkan 3 nombor. ");
printf("\nMasukkan 3 nombor dalam bentuk: nnn nnn nnn
<enter>");
scanf("%d%d%d",&a,&b,&c);
jum = a + b + c;
printf("\nJumlah adalah:%d",jum);
printf("\nTerima kasih. ");

Programme
A memory location showing
getch(); the name & value of variable
return 0;
10 20 30 60
}
a b c jum
DEC2012 - FUNDAMENTAL PROGRAMMING 100
Selamat Datang.
Aturcara ini menjumlahkan 3 nombor.
Masukkan 3 nombor dalam bentuk: nnn nnn nnn <enter> 10 20 30
Jumlah adalah: 60

3.2 Understand The Fundamentals Of C


#include <stdio.h> Terima kasih_
#include <conio.h>

main( )
{
int a, b, c, jum ;

printf("Selamat Datang.");
printf("\nAturcara ini menjumlahkan 3 nombor. ");
printf("\nMasukkan 3 nombor dalam bentuk: nnn nnn nnn
<enter>");
scanf("%d%d%d",&a,&b,&c);
jum = a + b + c;
printf("\nJumlah adalah:%d",jum);
printf("\nTerima kasih. ");

Programme
A memory location showing
getch(); the name & value of variable
return 0;
10 20 30 60
}
a b c jum
DEC2012 - FUNDAMENTAL PROGRAMMING 101
Mathematic operations,

3.3 Apply the fundamentals of C


relational and logical can
be combined in an
expression

Programming
DEC2012 - FUNDAMENTAL PROGRAMMING 102
Example :

3.3 Apply the fundamentals of C


Assume that a=2, b=5, c=15, d=17

1. (a >= 1) && (b == 5)
 (2 >= 1) && (5 == 5)
 1 && 1
 1

2. ! (c > a)

Programming
 ! (15 > 2)
 ! 1
 0

103
Example :

Assume that a=2, b=5, c=15, d=17

3.3 Apply the fundamentals of C


3. (c >= (a * 3) ) || (a == 3)
 (15 >= (2 * 3) ) || (2 == 3)
 (15 >= 6 ) || 0
 1 || 0
 1

Programming
4. ! ( (a < b) || (c > d ) )
 ! ( (2 < 5) || (15 > 17) )
!( 1 || 0 )
! 1

DEC2012 - FUNDAMENTAL PROGRAMMING 0 104

You might also like