Review : C Programming Language

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Introduction to Assembly language
OUTPUT INTERFACE – Microprocessor Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
MEMORY HIERARCHY – Microprocessor Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
Railway Foundation Electronic, Electrical and Processor Engineering.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
INTRODUCTION TO C PROGRAMMING LANGUAGE Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
FUNCTION – Microprocessor Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a.
IF-ELSE IF-ELSE STATEMENT SWITCH-CASE STATEMENT Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
RELATIONAL OPERATORS LOGICAL OPERATORS CONDITION Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
CONTROLLING PROGRAM FLOW
Making Decision – Microprocessor
BITWISE OPERATIONS – Microprocessor Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Optimised C/C++. Overview of DS General code Functions Mathematics.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Week 8: Decisions Bryan Burlingame 21 October 2015.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Introducing programming March 24. Program structure Statements to establish the start of the program Variable declaration Program statements (block statements)
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
COMP Loop Statements Yi Hong May 21, 2015.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Presentation By :- Nikhil R. Anande ( ) Electronic & Communication Engineering. 3 nd Year / 5 th Semester FACULTY GUIDE : RAHIUL PATEL SIR MICROCONTROLLER.
Loops. About the Midterm Exam.. Exam on March 12 Monday (tentatively) Review on March 5.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
MULTI-DIMENSION ARRAY STRING Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 4: Making Decisions.
Iteration statement while do-while
Engineering Innovation Center
Building Java Programs
Building Java Programs
Arrays, For loop While loop Do while loop
Looping and Repetition
Chapter 10 Programming Fundamentals with JavaScript
Building Java Programs
Logical assertions assertion: A statement that is either true or false. Examples: Java was created in The sky is purple. 23 is a prime number. 10.
Computer Science Core Concepts
ICT Programming Lesson 3:
SSEA Computer Science: Track A
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Repetition Statements (Loops) - 2
Building Java Programs
Iteration Statement for
Looping and Repetition
Presentation transcript:

Review : C Programming Language 353156 – Microprocessor Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat

Assembly VS C Up until now, you have learnt a lot about assembly instruction to control your microprocessor/controller Assembly depends on microprocessor architecture Pro : your programs run fast Con: If you change your platform from ARM to Intel, you have to re-write your assembly code For C, if your microprocessor model has a C compiler, you can program in C and let C compiler compiles your program to assembly language Pro : You don’t need to worry about registers inside microprocessor You can use your old code and compile it on other architectures without the need to modify.

C Program Structure C compiler parses them before compiles a program. #include #define Define global variables which all of the functions in C can see and use. Main program in C start in { } of main function

Basic Data Types in C Data types Memory Consumption (bit) Range of stored data char 8 -128 to 127 unsigned char 0 to 255 int 32 -2,147,483,648 to 2,147,483,647 unsigned int 0 to 4,294,967,295 long unsigned long 0 to 4,294,967,295 float 3.4 x 10-38 to 3.4 x1038 double 64 1.7 x 10-308 to 1.7 x 10308

How to define a variable in C

Example 1 int main(void) { int A; int B; int C, D, E; int F = 0, G = 10, H = 0x20; unsigned int K, L = 10; A = 50; B = 100; }

Mathematic Operations Symbol Definition Example + Addition A + B - Subtraction A - B * Multiplication A * B / Division A / B % Modulo A % B int main(void) { int A = 10, B = 4; int C, D, E, F, G; C = A + B; D = A - B; E = A * B; F = A / B; G = A % B; } What is the value inside the variable C, D, E, F, and G ? int A = 10, B = 3, C; C = A % B; What is the value inside the variable C ?

Bitwise Operations int main(void) { unsigned int A, B, C, D, E, F, G, H; A = 0xCD; B = 0x88; C = A & B; D = A | B; E = A ^ B; F = !A; G = A << 2; H = A >> 2; } What is the value inside the variable C, D, E, F, G and H ? Symbol Definition Example & AND A & B | OR A | B ^ XOR A ^ B ! NOT !A << Shift Left A << B >> Shift Right A >> B Mostly, we use bitwise operations with unsigned data type.

Greater than or equal to Logical Operations Symbol Definition == EQUAL != NOT EQUAL > Greater than >= Greater than or equal to < Less than <= Less than or equal to Suppose, A = 100 and B = 50 Expression Truth value A == B A != B A > B A < B (A == 50) && (A == 100) (A == 50) || (A == 100) (A != B) && (B != A) (A == B) || (B == 50) !(A == B) && (B != 40) Symbol Definition && AND || OR ! NOT

Condition 3 kinds of if statement in C if statement if - else statement if – else if – else statement

Condition: IF statement false false true true A = A + 5 A = A + 5 A = A * 10 int main(void) { int A = 5; if ( A == 5 ) { A = A + 5; A = A * 10; } int main(void) { int A = 5; if ( A == 5 ) A = A + 5; }

Condition: IF-ELSE statement false true true false A = A + 10 A = A - 10 A = A + 10 A = A - 10 A = A * 10 A = A / 10 int main(void) { int A = 5; if ( A == 5 ) { A = A + 10; A = A * 10; } else { A = A – 10; A = A / 10; } int main(void) { int A = 5; if ( A == 5 ) A = A + 5; else A = A – 10; }

Condition: IF- ELSE IF - ELSE statement True action Condition 2 Condition N False action true false int main(void) { int A = 4; if ( A == 1 ) { A = A + 10; } else if ( A == 2 ){ A = A – 10; } else if (A == 3) { A = A * 2; } else if (A == 4) { A = A / 4; } else { A = (A + 2) * 3; }

Example 1 int main(void) { int A, B; if ( (A + 5) <= 6 ) { B = A + 10; } else if ( A == 2 || A == 5 ){ B = A – 10; } else if (A == 3 || A > 7) { B = A * 2; } else if (A == 4) { B = A / 4; } else { B = (A + 2) * 3; } B = B + 1; What is the value stored in a variable B at the end of program If A = -1 1 2 4 5 7 10

Iteration Statement Iteration statement sometimes called “Loop” is the part of code that repeat itself as long as the condition is valid. In C : we have 3 types of iteration statement while do-while for

WHILE statement while (condition) { statement-1; statement-2; … false while (condition) { statement-1; statement-2; … statement-n; } true while (condition) statement;

Example 2 int main(void) { int count = 0; while (count <= 3) count = count + 1; } What is the value stored in a variable count at the end of program ?

DO-WHILE statement do { statement-1; statement-2; … do statement-n; } while (condition); do statement; while (condition);

Example 3 int main(void) { int count = 0; do count = count + 1; } while (count <= 3); } What is the value stored in a variable count at the end of program ?

Example 4 int main(void) { int count = 0; do count = count + 1; } while (count < 0); } int main(void) { int count = 0; while (count < 0) count = count + 1; } What is the value stored in a variable count at the end of program ?

FOR Statement for(expr1; expr2; expr3) for(expr1; expr2; expr3) { … statement-n; } for(expr1; expr2; expr3) statement; EXPR1 = Initialize EXPR2 = Loop Condition EXPR3 = Updater

Example 5 i A 4 2 3 ? 1 6 1 3 int main(void) { int i, A = 0; for( i = 1; i <= 3; i=i+1) { A = A + i; } 4 2 3 ? 1 A 6 1 3

Assignment 8 (1) int main(void) { int A, B = 5, SUM = 0; for(A = 1; A <= B; A++) { if(A <= 2) { SUM = SUM + 10; } else { SUM = SUM + 1; } What is the value stored in variables A, B, and SUM at the end of program ?

Assignment 8 (2) int main(void) { unnsigned int A = 0x1; int B, C; for(B = 0; B < 4; B++) { A = A << 1; C = A + 1; } What is the value stored in variables A, B, and C at the end of program ?