You are on page 1of 32

Introduction to C

Language
-
Statements, Expressions,
and Operators
The Pieces of a C Program
Learning Outcome
What a statement is
What an expression is
What an operator is
Operator precedence
The if statement
Statements
-
Statements
A complete instruction that directs the computer to
carry out some task.
Usually written one per line, end with semicolon
(except for preprocessor directives: #define and
#include)
E.g.:
X = 2 + 3; is an assignment statement.
Instruct computer to add 3 to 2 and assign the result to
the variable X.
White Space on Statements
Refer to spaces, horizontal tabs, vertical tabs, and
blank line in C code.
C compiler is not sensitive to white space.
Statement X=2+3; is equivalent to X = 2 + 3;
White space is kept track in strong constant.
E.g.:
“How now brown cow” is different from “How now
brown cow”
Important to keep your statement readable.
Compound Statements
Also called a block.
A group of two or more C statements enclosed in braces.
E.g.:
{
printf (“Hello,”);
printf (“world!”);
}
Put braces on their own line to make it clear the start
and ending of a C program.
Expressions
Expressions
Anything that evaluates to a numeric value.
Simple Expression:
Consist of single item such as a simple variable, literal
constant, or symbolic constant.
Literal constant: evaluates to its own value.
Symbolic constant: evaluates to the value created using
the #define directive.
Variable: evaluates to the current value assigned to it by
the C program.
Expression Description

PI A symbolic constant (defined in C


Program)
20 A literal constant

Rate A variable

-1.25 A literal constant


Expressions
Complex Expression:
Consist of simple expression connected by operators.
E.g.: 2 + 8 is an expression consisting of simple
expression 2 and 8 connected by the addition operator +.
E.g.: 1.25 / 8 + 5 * rate + rate * rate / cost
 Much more complex because contains multiple operators.
 Evaluation depends on operator precedence.
Operators
Operators
A symbol that instructs C to perform some operations
or action on one or more operands.
An Operand is something that an operator acts on.
All operands are expression.
Four categories of operator:
The assignment
Mathematical
Relational
Logical
Assignment Operator
The equal sign ( = ).
Use in programming different from in regular math.
E.g.: X = Y
In C program, does not mean X is equal to Y.
I means assign the value of Y to X.
In C assignment statement, the right side can be any
expression. The left side must be a variable name.
Format: variable = expression;
 Expression is evaluated and the resulting value is assigned to
variable.
Mathematical Operator
Perform math operation such as addition and
subtraction.
C has two Unary math operators and five binary math
operators.
Unary Math Operator
Operator Symbol Action Example

Increments the
Increment ++ operand by one ++ X

Decrement _ _ Decrements the --X


operand by one
Unary Math Operator
Can only be used with variable not constant.
Operation performed is to add one or subtract one
from the operand.
E.g.:
++ X; is equivalent to X = X + 1;
- - Y; is equivalent to Y = Y – 1;
Binary Math Operator
Operator Symbol Action Example

Addition + Adds two operands X+Y

Subtraction - Subtracts the second operand from the X–Y


first one

Multiplication * Multiplies two operands X*Y

Division / Divides the first operand by the second X/Y


operand

Modulus % Gives the remainder when the first X%Y


operand is divided by the second
operand
Binary Math Operator
Take two operands and operates by giving the result of
the two operands.
Operator Precedence:
The rules of the order in which operations are
performed.
Operator Precedence and Parentheses
Operators Precedence

Unary increment and decrement: + + -- First

Multiplication, division, modulus: * / % Second

Addition and subtraction: + - Third


Operator Precedence
12 % 5 * 2
12 % 5 evaluates to 2; 2 times 2 is 4 so the output is 4
X = 4 + 5 * 3
5 multiplies by 3 evaluates to 15; 15 added to 4 is 19 so the
output is 19.
X = (4 + 5) * 3
Expression in parentheses is evaluated first
5 added to 4 is 9; 9 multiplies by 3 is 27 so the output is
27.
Relational Operator
To compare expressions asking questions such as “Is X
greater than 100?” or “Is Y equal to 0?”
An expression containing a relational operator
evaluates to either true/ yes ( 1 ) or false/no ( 0 ).
Relational Operator
Operator Symbol Question Asked Example
Equal == Is operand 1 equal to operand X == Y
2?

Greater than > Is operand 1 greater than X>Y


operand 2?

Less than < Is operand 1 less than operand X<Y


2?

Greater than or >= Is operand 1 greater than or X>=Y


equal to equal to operand 2

Less than or <= Is operand 1 less than or equal X<=Y


to operand 2?

Not equal != Is operand 1 not equal to X!=Y


operand 2?
Relational Operator
Expression How it reads What it evaluates to

5 == 1 Is 5 equal to 1? 0 (false)

5>1 Is 5 greater than 1 1 (true)

5!=1 Is 5 not equal to 1? 1 (true)

(5+10) == (3*5) Is (5+10) equal to (3*5)? 1 (true)


Operator Precedence
Operators Precedence

< <= > >= First

!= = = Second
The if else Statement
Relational operator is usually used with the if else
statement to make C program control statement.
if else statement evaluates an expression and directs
program execution based on the result of that
evaluation.
Format:
If expression evaluates to true,
if (expression)
statement 1 is executed. If is
statement 1; false, control goes to else
else statement and execute
statement 2; statement 2.
/*Demonstrate the use of if statement with else clause*/

#include <stdio.h>

int x,y;

int main (void)


{
/*Input the two values to be tested*/

printf("\nInput an integer value for X: ");


scanf("%d", &x);
printf("\nInput an integer value for y: ");
scanf("%d", &y);

/*Test values and print result*/

if (x == y)
printf("X is equal to Y\n");
else
printf("X is not equal to Y\n");

getch();
return 0;
}
/*Demonstrate the use of if statement with else clause*/

#include <stdio.h>

int main (void)


{
int x, y;

/*Input the two values to be tested*/

printf("\nInput an integer value for X: ");


scanf("%d", &x);
printf("\nInput an integer value for y: ");
scanf("%d", &y);

/*Test values and print result*/

if (x + y == 10)
printf(”Welcome to C Language \n");
else
printf(”Hello C Language \n");

return 0;
}
Compound Assignment Operators
A shorthand method for combining a binary math
operation with an assignment operation.
E.g.:
Increase the value of X by 5.
Normal operator is X = X + 5;
Compound assignment operator is X += 5;
Compound Assignment Operators
Compound Normal

X *= Y X=X*Y

Y-=Z+1 Y=Y–Z+1

a/=b a = a / b

X+=Y/8 X=X+Y / 8

Y%=3 Y=Y%3
Exercise
-
Exercise
Write a C program that determines if someone is
legally an adult of not (age 21).
Write a C program that accept the price of two
products and calculate whether the total exceeds RM
100 budget. Display the output on screen.

You might also like