0% found this document useful (0 votes)
32 views14 pages

Week 3

Cc103

Uploaded by

Bryan Cabrido
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views14 pages

Week 3

Cc103

Uploaded by

Bryan Cabrido
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

WEEK 3

COURSE OUTLINE

COURSE CODE : IT 122

TITLE : Introduction to C Programming 1

TARGET POPULATION : All BS Information Technology Students

INSTRUCTOR : MR. PINK FLOYD B. JANEO

Overview:

C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to


develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11
computer in 1972. In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available
description of C, now known as the K&R standard. The UNIX operating system, the C compiler, and
essentially all UNIX application programs have been written in C.

Content

● Operators
● Flowchart with decision making
● If statement in flowchart
● If else statement in flowchart

Objectives:

● Learn the different types of operators used in C Programming.


● Be able to make use of logical operators with if statement

Instruction to the Learner

Each chapter in this module contains a major lesson involving the use of Flowchart and its purpose.
The units are characterized by continuity, and are arranged in such a manner that the present unit is
related to the next unit. For this reason, you are advised to read this module. After each unit, there are
exercises to be given. Submission of task given will be every Monday during your scheduled class hour.
OPERATORS

An operator is a symbol that tells the compiler to perform specific mathematical or logical
functions. C language is rich in built-in operators and provides the following types of operators:

∙ Arithmetic Operators
∙ Relational Operators
∙ Logical Operators
∙ Assignment Operators
∙ Misc. Operators

Arithmetic Operators

The following table shows all the arithmetic operators supported by the C language. Assume
variable A holds 10 and variable B holds 20, then:

OPERATOR DESCRIPTION EXAMPLE


+ Adds two operands. A + B = 30

- Subtracts second operand from A - B = -10


the first.
* Multiplies both operands. A * B = 200

/ Divides numerator by de- B/A=2


numerator.
% Modulus Operator and B%A=0
remainder of after an integer
division.

++ Increment operator increases A++ = 11


the integer value by one.
-- Decrement operator decreases A-- = 9
the integer value by one.

Relational Operators

The following table shows all the relational operators supported by C. Assume variable A holds
10 and variable B holds 20, then:

OPERATOR DESCRIPTION EXAMPLE


== Checks if the values of two (A == B) is not true.
operands are equal or not. If
yes, then the condition becomes
true.

!= Checks if the values of two (A != B) is true.


operands are equal or not. If the
values are not equal, then the
condition becomes true.
> Checks if the value of left (A > B) is not true.
operand is greater than the
value of right operand. If yes,
then the condition becomes
true.

< Checks if the value of left (A < B) is true.


operand is less than the value of
right operand. If yes, then the
condition becomes true.
>= Checks if the value of left (A >= B) is not true.
operand is greater than or equal
to the value of right operand. If
yes, then the condition becomes
true.

<= Checks if the value of left (A <= B) is true.


operand is less than or equal to
the value of right operand. If
yes, then the condition becomes
true.

Logical Operators

Following table shows all the logical operators supported by C language. Assume variable A
holds 1 and variable B holds 0, then:

OPERATOR DESCRIPTION EXAMPLE


&& Called Logical AND operator. If (A && B) is false.
both the operands are non-zero,
then the condition becomes
true.
|| Called Logical OR Operator. If (A || B) is true.
any of the two operands is non-
zero, then the condition
becomes true

! Called Logical NOT Operator. It !(A && B) is true.


is used to reverse the logical
state of its operand. If a
condition is true, then Logical
NOT operator will make it false.

Assignment Operators

The following tables lists the assignment operators supported by the C language:

OPERATOR DESCRIPTION EXAMPLE


= Simple assignment operator. C = A + B will assign the value of
Assigns values from right side A + B to C
operands to left side operand.

+= Add AND assignment operator. C += A is equivalent to C = C + A


It adds the right operand to the
left operand and assigns the
result to the left operand.
-= Subtract AND assignment C -= A is equivalent to C = C - A
operator. It subtracts the right
operand from the left operand
and assigns the result to the left
operand.

*= Multiply AND assignment C *= A is equivalent to C = C * A


operator. It multiplies the right
operand with the left operand
and assigns the result to the left
operand.
/= Divide AND assignment C /= A is equivalent to C = C / A
operator. It divides the left
operand with the right operand
and assigns the result to the left
operand.

%= Modulus AND assignment C %= A is equivalent to C = C %


operator. It takes modulus using A
two operands and assigns the
result to the left operand.
<<= Left shift AND assignment C <<= 2 is same as C = C << 2
operator.

>>= Right shift AND assignment C >>= 2 is same as C = C >> 2


operator.
&= Bitwise AND assignment C &= 2 is same as C = C & 2
operator.

^= Bitwise exclusive OR and C ^= 2 is same as C = C ^ 2


assignment operator.
|= Bitwise inclusive OR and C |= 2 is same as C = C | 2
assignment operator.

Misc Operators ↦sizeof & ternary

Besides the operators discussed above, there are a few other important operators including
sizeof and ? : supported by the C Language.

OPERATOR DESCRIPTION EXAMPLE


Sizeof() Returns the size of a variable. sizeof(a), where a is integer, will
return 4.

& Returns the address of a &a; returns the actual address


variable of the variable.
* Pointer to a variable. *a;

?: Conditional Expression. If Condition is true ? then value


X : otherwise value Y

Operators Precedence in C

Operator precedence determines the grouping of terms in an expression and decides how an
expression is evaluated. Certain operators have higher precedence than others; for example, the
multiplication operator has a higher precedence than the addition operator.
For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher
precedence than +, so it first gets multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with the lowest
appear at the bottom. Within an expression, higher precedence operators will be evaluated first.

CATEGORY OPERATOR ASSOCIATIVITY


Postfix () [] -> . ++ - - Left to right

Unary + - ! ~ ++ - - (type)* & sizeof Right to left


Multiplicative */% Left to right

Additive +- Left to right


Shift << >> Left to right

Relational < <= > >= Left to right


Equality == != Left to right

Bitwise AND & Left to right


Bitwise XOR ^ Left to right

Bitwise OR | Left to right


Logical AND && Left to right

Logical OR || Left to right


Conditional ?: Left to right

Assignment = += -= *= /= %=>>= <<= &= ^= | Left to right


=
Comma , Left to right
Flowchart with Decision making

Decision-making structures require that the programmer specifies one or more conditions to be
evaluated or tested by the program, along with a statement or statements to be executed if the
condition is determined to be true, and optionally, other statements to be executed if the condition is
determined to be false.
Shown below is the general form of a typical decision-making structure found in most of the
programming languages:

C programming language assumes any non-zero and non-null values as true, and if it is either zero
or null, then it is assumed as false value.

STATEMENT DESCRIPTION

if statement An if statement consists of a Boolean expression


followed by one or more statements.
if...else statement An if statement can be followed by an optional
else statement, which executes when the
Boolean expression is false.

nested if statements You can use one if or else if statement inside


another if or else if statement(s).
switch statement A switch statement allows a variable to be tested
for equality against a list of values.

nested switch statements You can use one switch statement inside another
switch statement(s).
If statement flowchart

An if statement consists of a Boolean expression followed by one or more statements.

Example:
1. Draw a flowchart that will check if variable a is less than 20.
Start

Initialize a

Input a = 11

True 11 < 20

a is less
than 20

End
If else statement Flowchart

An if statement can be followed by an optional else statement, which executes when the Boolean
expression is false.

Example:

1. Draw a flowchart that will print “Young” if the age is less than or equal to 19, else print
“Old”.

Start

Input age = 15

15 <= 19 False

Old

End

Initialize age
2. Draw a flowchart that will input a number print “I love Philippines” if the number if below 30,
else print “I am hungry”.

Start

Initialize number

Input number = 20

True 20 < 30

I Love
Philippines

End

If...else if...else Statement

An if statement can be followed by an optional else if...else statement, which is


very useful to test various conditions using single if...else if statement.

When using if…else if…else statements, there are few points to keep in mind:

∙ An if can have zero or one else's and it must come after any else if's.
∙ An if can have zero to many else if's and they must come before the else.
∙ Once an else if succeeds, none of the remaining else if's or else's will be tested.
Example

1. Draw a flowchart that will print “Like” if the number is equal to 5, Print “Share” if the number is
equal to 6, else print Subscribe.

Start

Initialize x

x=6

6 == 5

false

6 == 6 true Print Share

End

2. Draw a flowchart that will find the largest among three numbers.
Start

Initializa a, b and c

a = 5, b= 10, c= 2

5 >= 10 && 5 >= 2

False

10 >= 5 && 10 >= 2 True Print “b is


a
the largest
number”
c

End

You might also like