Upload
alexis-john-rubio
View
18
Download
1
Embed Size (px)
DESCRIPTION
Basic C Programming Outreach
Citation preview
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
Invocation
Engr. Nelson C. RodelasCpE Faculty
UNIVERSITY OF THE EAST – CALOOCANCollege of Engineering
CpE Department
Philippine National Anthem
UNIVERSITY OF THE EAST – CALOOCANCollege of Engineering
CpE Department
Welcome Remarks
Engr. Joan P. LazaroCpE Chairman
UNIVERSITY OF THE EAST – CALOOCANCollege of Engineering
CpE Department
Seminar Outline:• Introduction to C Programming
• Identifiers• Arithmetic Operators• Relational Operators• Logical Operators
• Basic Input and Output• Conditional Statements• Looping Statements
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.
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.
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.
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
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.
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.
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
Introduction to C Programming• Order of Precedence
Note: If same precedence operation is read from left to right.
Operators Precedence++, -- 1
*, /, % 2
+, - 3
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
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)
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)
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
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.
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
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.
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”);
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( );
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
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.
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.
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.
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
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
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
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.
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.
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.
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.
LOOPING STATEMENTS• Example 4:
• Create a program that will output your name 10 times.
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.
Distribution of Certificates
UNIVERSITY OF THE EAST – CALOOCANCollege of Engineering
CpE Department
Closing Remarks
UNIVERSITY OF THE EAST – CALOOCANCollege of Engineering
CpE Department