38
SEMINAR-WORKSHOP ON BASIC PROGRAMMING February 8, 2014 CLR 4, Engineering Building SPEAKERS: ENGR. JOAN P. LAZARO ENGR. ALEXIS JOHN M. RUBIO ENGR. PARALUMAN G. SIM UNIVERSITY OF THE EAST – CALOOCAN College of Engineering CpE Department

Basic C Programming Outreach

Embed Size (px)

DESCRIPTION

Basic C Programming Outreach

Citation preview

Page 1: Basic C Programming Outreach

SEMINAR-WORKSHOP ON BASIC PROGRAMMING

February 8, 2014CLR 4, Engineering Building

SPEAKERS:ENGR. JOAN P. LAZARO

ENGR. ALEXIS JOHN M. RUBIOENGR. PARALUMAN G. SIM

UNIVERSITY OF THE EAST – CALOOCANCollege of Engineering

CpE Department

Page 2: Basic C Programming Outreach

Invocation

Engr. Nelson C. RodelasCpE Faculty

UNIVERSITY OF THE EAST – CALOOCANCollege of Engineering

CpE Department

Page 3: Basic C Programming Outreach

Philippine National Anthem

UNIVERSITY OF THE EAST – CALOOCANCollege of Engineering

CpE Department

Page 4: Basic C Programming Outreach
Page 5: Basic C Programming Outreach

Welcome Remarks

Engr. Joan P. LazaroCpE Chairman

UNIVERSITY OF THE EAST – CALOOCANCollege of Engineering

CpE Department

Page 6: Basic C Programming Outreach

Seminar Outline:• Introduction to C Programming

• Identifiers• Arithmetic Operators• Relational Operators• Logical Operators

• Basic Input and Output• Conditional Statements• Looping Statements

Page 7: Basic C Programming Outreach

Introduction to C Programming• What is Programming?

• Programming is instructing a computer to do something for you with the help of a programming language.

• What is C Programming Language?• C was created at the Bell Telephone Laboratories in

1972 by Dennis Ritchie. It was developed into such a powerful and flexible language that its use quickly spread beyond Bell Labs. The C language is so named because its predecessor was called B.

• The B language was also developed at Bell Labs, by Ken Thompson.

Page 8: Basic C Programming Outreach

Introduction to C Programming• Identifiers

• one or more characters used to identify or name data elements such as variables, constants, arrays, records, and subprograms.

• an identifier is a combination of alphanumeric characters, the first being a letter of the alphabet or an underscore, and the remaining being any letter of the alphabet, any numeric digit, or the underscore.

Page 9: Basic C Programming Outreach

Introduction to C Programming• Constant

• a data item that remains unchanged throughout the execution of the program.

• a memory location whose constants stay the same during the execution of the program.

• Variable• a named data item, the value of which may change

during the execution of the program.• a memory location whose contents can be filled and

changed during the execution of the program.

Page 10: Basic C Programming Outreach

Introduction to C Programming• Data Type

• a definition or interpretation of a set of data specifying the possible range of values of the set, the operation that can be performed on the rules and the way in which values are stored in the memory.

DATA TYPE SIZE (BITS) RANGECHAR 8 0 to 255

INT 16 0 to 65535

FLOAT 32 3.4e-38 to 3.4e-38

Page 11: Basic C Programming Outreach

Introduction to C Programming• Expressions

• It is a combination of values, variables, operators, and functions that are interpreted (evaluated) according to the particular rules of precedence and of association for a particular programming language, which computes and then produces (returns, in a stateful environment) another value.

Page 12: Basic C Programming Outreach

Introduction to C Programming• Kind of Expressions

1. Arithmetic • an expression that contains arithmetic operators and

operands which can be reduced to a single numeric value.2. Relational

• an expression constructed by connecting constants, variables and/or other expressions by a relational operator.

3. Logical• an expression constructed by combining individual

conditional variables or expressions into a more complex statements by combining them with a logical operator.

Page 13: Basic C Programming Outreach

Introduction to C Programming• Arithmetic operator

• a symbol used to represent a mathematical operation.OPERATOR OPERATION ACTION

++ Increment Increments operand by 1

-- Decrement Decrements operand by 1

+ Addition Adds its two operand

- Subtraction Subtracts the 2nd operand to the 1st operand

* Multiplication Multiplies its two operand

/ Division Divides the 1st operand by the 2nd operand

% Modulus Gives the remainder when the 1st operand is divided by the 2nd operand

Page 14: Basic C Programming Outreach

Introduction to C Programming• Order of Precedence

Note: If same precedence operation is read from left to right.

Operators Precedence++, -- 1

*, /, % 2

+, - 3

Page 15: Basic C Programming Outreach

Introduction to C Programming• Relational operator

• a symbol used to compare operands and yields either true or false.

Operator Operation== Equal

> Greater than

< Less than

>= Greater than or Equal

<= Less than or Equal

!= Not Equal

Page 16: Basic C Programming Outreach

Introduction to C Programming• Logical operator

• a symbol that defines the logical connection between two or more conditions.

• Logical operator: NOT( ! )

Operator Meaning! NOT (Reverse the value of a condition)

&& AND (Both condition must be true)

|| OR (At least one condition is true)

CONDITION RESULTFALSE (0) TRUE (1)

TRUE (1) FALSE (0)

Page 17: Basic C Programming Outreach

Introduction to C Programming• Logical Operator: AND ( && )

• Logical Operator: OR ( || )1st CONDITION 2nd CONDITION RESULT

FALSE (0) FALSE (0) FALSE (0)

FALSE (0) TRUE (1) TRUE (1)

TRUE (1) FALSE (0) TRUE (1)

TRUE (1) TRUE (1) TRUE (1)

1st CONDITION 2nd CONDITION RESULTFALSE (0) FALSE (0) FALSE (0)

FALSE (0) TRUE (1) FALSE (0)

TRUE (1) FALSE (0) FALSE (0)

TRUE (1) TRUE (1) TRUE (1)

Page 18: Basic C Programming Outreach

Introduction to C Programming• Structure of a C Program

//Compiler Directives #include<header files>#include<stdio.h> //standard input & output library// main() Functionint main(void){ // start of main function// Program contents// Program contents// Program contents}//end of main fuction

Page 19: Basic C Programming Outreach

Basic Input / Output• printf( ) Function

• A library function that is used to display information on the screen.

• Syntax:printf(“control string”, arguments);where:• control string - contains characters to be displayed or

format codes that tell how to display the rest of the argument.

• argument list – list of values or/and variables.

Page 20: Basic C Programming Outreach

Basic Input / Output• printf( ) Function format

codes• printf ( ) Backslash codes

Format Code Meaning%d or %I Display an integer

%f Display a float

%c Display a character

Code Meaning\n Newline

\t Horizontal tab

\a Alert (beep)

\\ Backslash

Page 21: Basic C Programming Outreach

Basic Input / Output• scanf() Function

• A library function that reads data from the keyboard and assigns that data to one or more program variable.

• Syntax:scanf(“control string”, argument list);where: • control string – format codes of the values to input.• argument list – list of variables where the values

read from the keyboard will be stored. Variable must be prefix by ampersand (&) sign.

Page 22: Basic C Programming Outreach

Basic Input / Output• puts() Function – put string

• Also used to display text messages on the screen. It cannot display numeric values. It takes a single string as its argument and displays it, automatically adding a new-line at the end.

• Syntax: puts(“argument”);

• putch() Function – put character• displays any alphanumeric characters to the standard

output device. It displays only one character at a time• Syntax: putch(“character variable”);

 

Page 23: Basic C Programming Outreach

Basic Input / Output• gets() Function – get string

• A function that accepts a string from the standard input device, the keyboard. Its method is to read characters until it reaches a newline (‘\n’) character, which is generated by pressing the Enter key.

• Syntax: gets(string variable);

• getch() Function – get character• getch function prompts the user to press a character

and that character is not printed on screen• Syntax: variable = getch( );

Page 24: Basic C Programming Outreach

Basic Input / Output• Example #1:

• Create a program that will let the user enter their name, address, gender (M or F), age and contact number, the program will then output all the entered data.

• Example #2:• Create a program that will let the user enter two

numbers and will output their sum, difference, product and quotient

Page 25: Basic C Programming Outreach

Basic Input / Output• Hands – on Activity #1

• Write a program that will compute the monthly salary of an employee given the number of days work, rate per day, number of absence(s), the number of hour(s) late, also include the name, position and address of the employee.

Page 26: Basic C Programming Outreach

Conditional Statement• Conditional Statements checks an expression then

may or may not execute a statement a group of statements depending on the result of the Boolean expression.

• Boolean Expression is expression, which are answerable by TRUE or FALSE. A Boolean Expressions must have a relational operator to evaluate these things.

Page 27: Basic C Programming Outreach

Conditional Statement• IF-ELSE-IF Statement

• Syntax:if(<condition>){

<statement/s A>;}else if(<condition>){

<statement/s B>;}else{

<statement/s C>;}

where:• condition – logical expression

that determines whether the action is to be executed.

• statements A – action/actions to be performed if the logical expression is TRUE.

• statement B – action/actions to be performed if the first logical expression is FALSE.

• statement C – action/actions to be performed if both logical expression are FALSE.

Page 28: Basic C Programming Outreach

Conditional Statement• SWITCH Statement

• Syntax:switch(<expression>){

case <value 1>:<statement sequence>;break;

case <value 2>: <statement sequence>;

break;...

case <value n>:<statement sequence>;break;

default:<statement sequence>;

}

• where:expression – an int or char expression

also called selectorvalue – an expression to be matched with

a case labelstatement sequence – action associated

with preceding casesbreak statement – used to terminate the

statement sequence associated with each value. If break is omitted, execution will continue until the next case statement.

default statement – is the main statement to be executed if no matches are found in the case. It is optical and therefore if it is not present, no action takes place if all matches fail

Page 29: Basic C Programming Outreach

Conditional Statement• Example #3:

• Create a program that will let a user enter his / her discount code and amount bought and the program will compute for total amount to be paid.

DISCOUNT CODE DISCOUNTA 5%

B 10%

C 15%

D 20%

E NONE

Page 30: Basic C Programming Outreach

Conditional Statement• Hands – On #2:

• Create a program that will let the user enter his / her age, the program will then determine their age classification.

AGE CLASSIFICATION0 – 12 KIDS

13 – 18 TEENAGERS

19 – 25 YOUNG ADULT

26 – 40 ADULT

40 – 60 MIDDLE AGE

60 above SENIOR CITIZEN

Page 31: Basic C Programming Outreach

LOOPING STATEMENTS• In order to repeat a statement multiple times, a

control structure known as loops are to be used.• Loop allows a set of instructions to repeat until a

certain condition is reached. • C supports the same type of loop as other modern

structured languages.

Page 32: Basic C Programming Outreach

LOOPING STATEMENTS• FOR Loop Statement

• Syntax:for (initialization; condition;

incrementation) {

<statements/s>; }

• where:Initialization – an assignment

statement that is used to set the loop control variable.

Condition – a relational expression that determines when the loop will exist by testing the loop-control variable against some values.

Incrementation – defines how the loop-control variable will change each time the loop is repeated.

Page 33: Basic C Programming Outreach

LOOPING STATEMENTS• WHILE Loop Statement

• Syntax:while (<condition>) {

<statement/s>;

}

• where:statement – can be an empty

statement, a single statement or a block of statement that is to be repeated.

conditions – may be any valid expression. The loop iterates while the condition is true and program control passes to the next line following the loop if the condition is false.

Page 34: Basic C Programming Outreach

LOOPING STATEMENTS• DO-WHILE Loop Statement

• Syntax:do{

<statement/s>; } while (<condition>);

• where:statement – can be an empty

statement, a single statement or a block of statement that is to be repeated.

conditions – may be any valid expression. The loop iterates while the condition is true and program control passes to the next line following the loop if the condition is false.

Page 35: Basic C Programming Outreach

LOOPING STATEMENTS• Example 4:

• Create a program that will output your name 10 times.

Page 36: Basic C Programming Outreach

LOOPING STATEMENTS• Hands – On #4:

• Create a program that will let the user to enter n values and will compute the total of all entered values.

Page 37: Basic C Programming Outreach

Distribution of Certificates

UNIVERSITY OF THE EAST – CALOOCANCollege of Engineering

CpE Department

Page 38: Basic C Programming Outreach

Closing Remarks

UNIVERSITY OF THE EAST – CALOOCANCollege of Engineering

CpE Department