bbm101#–introduc/on#to# programmingi
TRANSCRIPT
![Page 1: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/1.jpg)
1
BBM 101 – Introduc/on to Programming I Fall 2014, Lecture 3
Aykut Erdem, Erkut Erdem, Fuat Akal
![Page 2: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/2.jpg)
2
Today
¢ Introduc/on to Programming § Basic Concepts § Developing Algorithms § Crea<ng Flowcharts
¢ The C Programming Language § Your first C Program § Programming Process § Lexical Elements of a C Program
§ Keywords, Iden<fiers, Constants, Data Types, Operators § Standard Input and Output § Type Conversion and Cas<ng
![Page 3: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/3.jpg)
3
Today
¢ Introduc/on to Programming § Basic Concepts § Developing Algorithms § Crea<ng Flowcharts
¢ The C Programming Language § Your first C Program § Programming Process § Lexical Elements of a C Program
§ Keywords, Iden<fiers, Constants, Data Types, Operators § Standard Input and Output § Type Conversion and Cas<ng
![Page 4: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/4.jpg)
4
What is a Program?
¢ A computer program is a set of instruc<ons for a computer to follow § e.g. instruc<ons to find the maximum value in a list of numbers
![Page 5: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/5.jpg)
5
What is a Program?
¢ A computer program is a set of instruc<ons for a computer to follow § e.g. instruc<ons to find the maximum value in a list of numbers
¢ An algorithm is a sequence of precise instruc<ons which leads to a solu<on § e.g. how to find the maximum value in a list of numbers
![Page 6: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/6.jpg)
6
What is a Program?
¢ A computer program is a set of instruc<ons for a computer to follow § e.g. instruc<ons to find the maximum value in a list of numbers
¢ An algorithm is a sequence of precise instruc<ons which leads to a solu<on § e.g. how to find the maximum value in a list of numbers
¢ Program is an algorithm expressed in a language that the computer can understand § e.g. The C Programming Language
![Page 7: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/7.jpg)
7
What is a Program?
¢ A computer program is a set of instruc<ons for a computer to follow § e.g. instruc<ons to find the maximum value in a list of numbers
¢ An algorithm is a sequence of precise instruc<ons which leads to a solu<on § e.g. how to find the maximum value in a list of numbers
¢ Program is an algorithm expressed in a language that the computer can understand § e.g. The C Programming Language
¢ Computer soAware is the collec<on of programs § e.g. MicrosoM Office, iTunes, FireFox etc.
![Page 8: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/8.jpg)
8
Pseudocode
¢ Ar<ficial, informal language that helps us develop algorithms § Similar to everyday English § Not actually executed on computers § Helps us “think out” a program before wri<ng it § Easy to convert into a corresponding C program
¢ Example: 1. Display “Enter two integer number” message 2. Read the 1st number from keyboard 3. Read the 2nd number from keyboard 4. Compute sum of entered numbers 5. Print “Sum = “ + sum
![Page 9: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/9.jpg)
9
The Art of Computer Programming, Donald Knuth (expected to be 7 volumes)
Program Design
¢ Programming is a crea<ve process § No complete set of rules for crea<ng a program
¢ Program Design Process § Problem Solving Phase
§ Result is an algorithm that solves the problem
§ Implementa<on Phase § Result is the algorithm translated into a programming language
![Page 10: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/10.jpg)
10
Problem Solving Phase
¢ Be certain the task is completely specified § What is the input? § What informa<on is in the output? § How is the output organized?
¢ Develop the algorithm before implementa<on § Experience shows this saves <me in ge_ng your program to run
§ Test the algorithm for correctness
![Page 11: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/11.jpg)
11
Implementa/on Phase
¢ Translate the algorithm into a programming language § Easier as you gain experience with the language
¢ Compile the source code § Locates errors in using the programming language
¢ Run the program on sample data § Verify correctness of results
¢ Results may require modifica<on of the algorithm and program
![Page 12: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/12.jpg)
12
SoAware (Bigger Programs) Development
¢ Problem Defini<on ¢ Analysis § Analyze the problem § Define input, output and variables
¢ Design § Design the algorithm
¢ Implementa<on § Coding the algorithm
¢ Tes<ng § Test and verify the correctness of the program
¢ Maintenance § Fix bugs and add new features
![Page 13: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/13.jpg)
13
Flowcharts
¢ A flowchart is a type of diagram, that represents an algorithm or process
Sum = 1st number + 2nd number
1st number
2nd number
![Page 14: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/14.jpg)
14
Basic Flowchart Symbols 1/2
Input
Sequence
Output
Start / Stop
![Page 15: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/15.jpg)
15
Basic Flowchart Symbols 2/2
Repeat
Repeat Loop While Loop
Decision True
False
False
True
False
True
![Page 16: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/16.jpg)
16
Example 1: Algorithm to find and display the sum of two integers entered via keyboard
![Page 17: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/17.jpg)
17
Example 1: Algorithm to find and display the sum of two integers entered via keyboard
¢ Algorithm 1. Display “Enter two integer number” message 2. Read the 1st number from keyboard 3. Read the 2nd number from keyboard 4. Compute sum of entered numbers 5. Print “Sum = “ + sum
![Page 18: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/18.jpg)
18
Example 1: Algorithm to find and display the sum of two integers entered via keyboard
¢ Algorithm 1. Display “Enter two integer number” message 2. Read the 1st number from keyboard 3. Read the 2nd number from keyboard 4. Compute sum of entered numbers 5. Print “Sum = “ + sum
1st number
2nd number
sum = 1st number + 2nd number
sum
“Enter two integer number”
![Page 19: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/19.jpg)
19
Example 2: Algorithm to display two integers entered via keyboard in descending order
![Page 20: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/20.jpg)
20
Example 2: Algorithm to display two integers entered via keyboard in descending order
¢ Algorithm 1. Read the number1 from keyboard 2. Read the number2 from keyboard 3. If number1 > number2
§ Print “number1 > number2” 4. otherwise
§ Print “number2 > number1”
![Page 21: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/21.jpg)
21
Example 2: Algorithm to display two integers entered via keyboard in descending order
¢ Algorithm 1. Read the number1 from keyboard 2. Read the number2 from keyboard 3. If number1 > number2
§ Print “number1 > number2” 4. otherwise
§ Print “number2 > number1”
number1
number2
number1 > number2
“number1 , number2” “number2 , number1”
True False
![Page 22: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/22.jpg)
22
Example 3: Algorithm to display three integers entered via keyboard in ascending order
![Page 23: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/23.jpg)
23
Example 3: Algorithm to display three integers entered via keyboard in ascending order
¢ Algorithm 1. Read the number1, number2
and number3 from keyboard 2. If number1 > number2
§ Big = number1 § Small = number2
3. Otherwise § Big = number2 § Small = number1
![Page 24: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/24.jpg)
24
Example 3: Algorithm to display three integers entered via keyboard in ascending order
¢ Algorithm 1. Read the number1, number2
and number3 from keyboard 2. If number1 > number2
§ big = number1 § small = number2
3. Otherwise § big = number2 § small = number1
4. If number3 > big § middle = big § big= number3
5. Otherwise § If number3 > small
– middle = number3 § Otherwise
– middle = small – small = number3
6. Display small, middle, big
![Page 25: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/25.jpg)
25
Example 3: Algorithm to display three integers entered via keyboard in ascending order
Number1, 2, 3
number1 > number2
True False
big = number1 small = number2
big = number2 small = number1
number3 > big middle = big big = number3 number3 > small
middle = number3 middle = small small = number3
“small, middle, big”
T F
T F
![Page 26: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/26.jpg)
26
Example 4: Algorithm to find n!
![Page 27: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/27.jpg)
27
Example 4: Algorithm to find n!
¢ Algorithm 1. Read n from keyboard 2. if n < 0
§ Display error message 3. else
§ factorial = 1 § value = 1 § while (value <= n)
– factorial = factorial * value – increment value by 1
4. Display factorial
![Page 28: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/28.jpg)
28
Example 4: Algorithm to find n!
¢ Algorithm 1. Read n from keyboard 2. if n < 0
§ Display error message 3. else
§ factorial = 1 § value = 1 § while (value <= n)
– factorial = factorial * value – increment value by 1
4. Display factorial
n
factorial = 1 value = 1
n < 0
True False
Can’t be Nega<ve!
while (value<=n)
factorial = factorial * value value = value + 1
factorial
![Page 29: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/29.jpg)
29
Today
¢ Introduc/on to Programming § Basic Concepts § Developing Algorithms § Crea<ng Flowcharts
¢ The C Programming Language § Your first C Program § Programming Process § Lexical Elements of a C Program
§ Keywords, Iden<fiers, Constants, Data Types, Operators § Standard Input and Output § Type Conversion and Cas<ng
![Page 30: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/30.jpg)
30
Anatomy of a Typical C Program
#preprocessor directives declarations variables functions int main (void){ declarations; statements; return value; }
![Page 31: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/31.jpg)
31
Your First C Program
/* Welcome to BBM 101 */
#include <stdio.h>
int main(void)
{ printf(“Hello world!\n”); return 0;
}
Hello world!
hello.c
$>> gcc hello.c –ansi –Wall –o hello $>> ./hello
![Page 32: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/32.jpg)
32
Your First C Program
/* Welcome to BBM 101 */
#include <stdio.h>
int main(void)
{ printf(“Hello world!\n”); return 0;
}
Hello world!
/* comments */
global declara<ons #include external files
main func<on
hello.c
![Page 33: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/33.jpg)
33
Your First C Program
/* Welcome to BBM 101 */
#include <stdio.h>
int main(void)
{ printf(“Hello world!\n”); return 0;
}
Hello world!
/* comments */
global declara<ons #include external files
main func<on
hello.c Text surrounded by /* and */ is ignored by computer
![Page 34: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/34.jpg)
34
Your First C Program
/* Welcome to BBM 101 */
#include <stdio.h>
int main(void)
{ printf(“Hello world!\n”); return 0;
}
Hello world!
/* comments */
global declara<ons #include external files
main func<on
hello.c
“stdio.h” allows standard input/output opera<ons
![Page 35: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/35.jpg)
35
Your First C Program
/* Welcome to BBM 101 */
#include <stdio.h>
int main(void)
{ printf(“Hello world!\n”); return 0;
}
Hello world!
/* comments */
global declara<ons #include external files
main func<on
hello.c
C programs contain one or more func<ons, exactly one
of which must be main
![Page 36: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/36.jpg)
36
The main(void) of hello.c
int main(void) { printf(“Hello world!\n”); return 0; }
Program int main(void)
Arguments
Return value
• No arguments. • Returns an integer variable.
return “0” to OS: “everything is OK”
Slide credit: Bert Huang
![Page 37: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/37.jpg)
37
C Statements
¢ One-‐line commands ¢ Always end in semicolon ; ¢ Examples: § call func<on: printf(“hello”); /* from stdio */ § declare variable: int x; § assign variable value: x = 123+456;
![Page 38: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/38.jpg)
38
The Programming Process
Create/Edit Program Compile Execute
“The cycle ends once the programmer is sa<sfied with the program, e.g., performance and correctness-‐wise.”
“Do not forget, your program always has at least twenty bugs even aMer you have fixed them all J”
![Page 39: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/39.jpg)
39
C Program Development
Preprocess: Preprocessor program processes the code.
Load: Loader puts program in memory.
Execute: CPU takes each instruc<on and executes it, possibly storing new data values as the program executes.
Compile: Compiler creates object code and stores it on disk.
Link: Linker links the object code with the libraries.
Create/Edit: Program is created in the editor and stored on disk.
Pre-‐processor
Editor
Compiler
Linker
Loader
Disk
CPU
Main Memory
![Page 40: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/40.jpg)
40
Lexical Elements
¢ Token: The smallest element of a program that is meaningful to the compiler
¢ Kinds of tokens in C: § Keywords § Iden<fiers § Constants/Literals § Operators § Punctuators
![Page 41: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/41.jpg)
41
Keywords
¢ 32 words are defined as keywords in C ¢ They have predefined uses and cannot be used for any other purpose in a C program
auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while
![Page 42: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/42.jpg)
42
Iden/fiers
¢ A sequence of lepers, digits, and the underscore character ‘_’ sa<sfying § iden<fier = c { c | d }* § with c = {‘A’,…, ‘Z’, ‘a’,…, ‘z’, ‘_’ }, d = {0,…,9}, and asterisk “*” means “0 or more”
¢ Case-‐sensi<ve § e.g., firstName and firstname are two different iden<fiers.
¢ Iden<fiers are used for § Variable names § Func<on names § Macro names
![Page 43: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/43.jpg)
43
Iden/fier Examples
¢ Valid iden<fiers § X § a1 § _xyz_33 § integer1 § Double
¢ Invalid iden<fiers § xyz.1 § gx^2 § 114West § int § pi*r*r
¢ Invalid iden<fiers § xyz.1 § gx^2 § 114West § int ß This is a keyword § pi*r*r
![Page 44: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/44.jpg)
44
Variables
¢ A variable is a loca<on in main memory where a value is stored (just like Algebra)
¢ Variables must be declared before they are used ¢ Variable declara<ons must appear before executable statements § A syntax error is raised at compile-‐<me if above two are violated
¢ Every variable has a name, a type, size and a value
![Page 45: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/45.jpg)
45
Basic Data Types
¢ Integer (int) ¢ Character (char) ¢ Floa<ng Point (float) ¢ Double Precision Floa<ng Point (double)
¢ Data Type Modifiers § signed / unsigned § short / long
![Page 46: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/46.jpg)
46
int
¢ 4 bytes (on Unix) ¢ Base-‐2 representa<on. ¢ need one bit for + or -‐ ¢ Range: -‐231 to 231 ¢ Variants: short (2 bytes), long (8 bytes), unsigned (only non-‐nega<ve)
Slide credit: Bert Huang
![Page 47: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/47.jpg)
47
char
¢ 1 byte ¢ ASCII representa<on in base-‐2 ¢ Range: 0-‐255 (lots of unused)
Slide credit: Bert Huang
![Page 48: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/48.jpg)
48
float
¢ Stands for “floa<ng decimal point” ¢ 4 bytes ¢ Similar to scien<fic nota<on: 4.288 * 103 ¢ Very different interpreta<on of bits than int and char. ¢ Range: -‐1038 to 1038
Slide credit: Bert Huang
![Page 49: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/49.jpg)
49
Basic Data Types
Type Size in Bytes Range signed char 1 -‐127 to +127 unsigned char 1 0 to 255 short int 2 -‐32,767 to +32,767 unsigned short int 2 0 to 65535 int 4 -‐32,767 to +32,767 unsigned int 4 0 to 65,535 long int 8 -‐2,147,483,647 to +2,147,483,647 unsigned long int 8 0 to 4,294,967,295 float 4 ~10-‐37 to ~1038 double 8 ~10-‐307 to ~10308 long double 16 ~10-‐4931 to ~104932
![Page 50: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/50.jpg)
50
Declaring a Variable
¢ A declara<on consists of a data type name followed by a list of (one or more) variables of that type char c;
int myCounter;
float rate;
double trouble;
¢ A variable may be ini<alized in its declara<on char c = ‘a’;
int a = 220, b = 448;
float x = 1.23e-6; /*0.00000123*/
double y = 27e3; /*27,000*/
¢ Variables that are not ini<alized may have garbage values ¢ Placing a new value replaces the previous value ¢ Reading variables from memory does not change them
![Page 51: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/51.jpg)
51
Constants
¢ Integer Constants § 0, 37, 2001
¢ Floa<ng-‐point Constants § 0.8, 199.33, 1.0
¢ Character Constants § ‘a’, ‘5’, ‘+’
¢ String Constants § “a”, “Monday”
¢ How to define? § #define PI 3.14; § const double PI = 3.14;
¢ Common Escape Sequences § \a audible alarm § \b backspace § \n newline § \r carriage return § \t horizontal tab § \f form-‐feed § \\ backslash § \” double quote
![Page 52: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/52.jpg)
52
Operators
¢ Arithme<c operators § *, /, %, +, -‐
¢ Assignment operator § =
¢ Logical operators § We will cover this next week in the selec<ve-‐structures lecture
![Page 53: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/53.jpg)
53
Arithme/c Operators
¢ For arithme<c calcula<ons § Addi<on (+), subtrac<on (-‐), mul<plica<on (*) and integer division (/) § Integer division truncates remainder
§ 7 / 5 evaluates to 1 § Modulus operator(%) returns the remainder
§ 7 % 5 evaluates to 2
¢ Arithme<c operators associate leM to right
¢ Operator precedence § Example: Find the average of three variables a, b and c
§ Do not use: a + b + c / 3 § Use: (a + b + c ) / 3 § See next slide for why
![Page 54: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/54.jpg)
54
Operator Precedence
Operator(s) Opera/on(s) Precedence
( ) Parentheses Evaluated first. Innermost parentheses is evaluated in case of nested parentheses. Same level parentheses are evaluated from leM to right.
*, /, % Mul<plica<on Division Modulus
Evaluated second. If there are several, they are evaluated from leM to right.
+, -‐ Addi<on Subtrac<on
Evaluated last. If there are several, they are evaluated from leM to right.
![Page 55: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/55.jpg)
55
Operator Precedence
y = 2 * 5 * 5 + 3 * 5 + 7
y = 10 * 5 + 3 * 5 + 7
y = 50 + 3 * 5 + 7
y = 50 + 15 + 7
y = 65 + 7
y = 72
Left-most multiplication
Left-most multiplication
Multiplication precedes addition
Left-most addition
Last operation
![Page 56: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/56.jpg)
56
Assignment Operator (=)
¢ variable = expression ;
¢ Expressions § Opera<ons
§ total = number1 + number 2 ;
§ Variables § temp = number1 ;
§ Constants § #define PI 3.14 circumference = 2 * PI * radius ;
§ Func<on Calls § maxValue = findMax(number1, number2) ;
¢ Precedence of the assignment operator is lower than the arithme<c operators’
![Page 57: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/57.jpg)
57
l-‐value vs. r-‐value 5 x
10 y
y = x ;
x = 5; y = 10;
l(eM)-‐value usage of y
r(ight)-‐value usage of x
There is a memory loca<on named y. This loca<on will receive a
value.
There is a memory loca<on named x, where there is a value si_ng, go and get me
that value.
“X + 1 = 3 ;” is an invalid l-‐value expression.
![Page 58: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/58.jpg)
58
Further Assignment Opera/ons
¢ Compound Assignment § “x += y ;” equals to “x = x + y ;”
¢ Nested Assignments § “x = y = z = 0 ;” equals to “x = (y = (z = 0)) ;” § “x -= y = z ;” equals to “x -= (y = z) ;” § “x = y += z ;” equals to “x = (y += z) ;”
![Page 59: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/59.jpg)
59
Increment/Decrement Operators
¢ Post-‐increment/-‐decrement § Use the value then increase/decrease § Nota<on: “i++” or “i-‐-‐” § i = 5; j = (i++) * 2; à i = 6 and j = 10
¢ Pre-‐increment/-‐decrement § Increase/Decrease the value then use § Nota<on: “++i” or “-‐-‐i” § i = 5; j = (++i) * 2; à i = 6 and j = 12
¢ Invalid Usage Examples § ++(i-3) § ++(++i) § 2 * i++ -i à ambiguous, compiler-dependent
![Page 60: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/60.jpg)
60
printf
¢ printf(“formatted text”,exp1,exp2,...);
¢ Use placeholders for variables: %d int %f float %c char
¢ Examples: § printf(“Hello world!\n”); § printf(“%d plus %d is %d\n”, x, y, x+y);
Slide credit: Bert Huang
![Page 61: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/61.jpg)
61
Increment/Decrement Operators
int main(void) { int a = 0 , b = 0, c = 0; a = ++b + ++c; printf(“\n%d %d %d”, a,b,c);
a = b++ + c++; printf(“\n%d %d %d”, a,b,c);
a = ++b + c++; printf(“\n%d %d %d”, a,b,c);
a = b-- + --c; printf(“\n%d %d %d”, a,b,c);
return 0; }
5 2 2
2 1 1
2 2 2
5 3 3
printf(“\n%d %d %d”, a,b,c);
printf(“\n%d %d %d”, a,b,c);
printf(“\n%d %d %d”, a,b,c);
printf(“\n%d %d %d”, a,b,c);
![Page 62: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/62.jpg)
62
scanf
¢ scanf(“formatted text”,&var1, &var2,...);
%c a single character is expected in the input %d an integer is expected in the input %f a floa<ng point is expected in the input
¢ Each argument must be a pointer to the variable where the results of input are to be stored.
![Page 63: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/63.jpg)
63
Standard Input and Output Example
#include<stdio.h>
int main(void)
{
float principal, rate, interest;
int years;
printf(“principal, rate, and years? “);
scanf(“%f %f %d”, &principal, &rate, &years);
rate /= 100;
interest = principal * rate * years;
printf(“interest = %f\n”, interest);
return 0;
}
Expects two float and one integer numbers as input (from keyboard)
scanf(“%f %f %d”, &principal, &rate, &years);
Prints a float number (on to the screen)
printf(“interest = %f\n”, interest);
![Page 64: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/64.jpg)
64
Type Conversion and Cas/ng
¢ If operands are of mixed data types, the compiler will convert one operand to agree with the other using the following hierarchy structure: long double (highest)
double
float long
int char/short (lowest)
![Page 65: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/65.jpg)
65
Implicit Cas/ng
¢ Done automa<cally by the compiler whenever data from different types is intermixed
¢ int i; double x = 17.7; i = x;
¢ float x; int i = 17; x = i;
i = 17
x = 17.0
![Page 66: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/66.jpg)
66
Explicit Cas/ng
int total_score = 333, num_students = 4;
float average;
average=total_score/num_students;
printf("Average score (no casting) is %.2f\n", average);
average=(float)total_score/(float)num_students;
printf("Average score (with casting) is %.2f\n", average);
Average score (no cas<ng) is 83.00 Average score (with cas<ng) is 83.25
average=(float)total_score/(float)num_students;
![Page 67: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/67.jpg)
67
Effects of Cas/ng
¢ Cas<ng a float as an int causes trunca<on float a = 3.1; int x = (int) a; /* x is now 3 */
¢ Be careful with math:
float a, b, c; int x = 2, y = 3;
a = x/y; /* what happens here? */
b = (float)x/y;
c = (float)x / (float)y;
Slide credit: Bert Huang
![Page 68: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/68.jpg)
68
Summary
¢ Introduc/on to Programming § Basic Concepts § Developing Algorithms § Crea<ng Flowcharts
¢ The C Programming Language § Your first C Program § Programming Process § Lexical Elements of a C Program
§ Keywords, Iden<fiers, Constants, Data Types, Operators § Standard Input and Output § Type Conversion and Cas<ng
![Page 69: BBM101#–Introduc/on#to# ProgrammingI](https://reader030.vdocuments.mx/reader030/viewer/2022012801/61bd08ad61276e740b0ead52/html5/thumbnails/69.jpg)
69
Next week
¢ Condi/onal Branching § Logical Expressions § if and If-‐else statements § switch statement § goto statement