15.1 More About High-Level Programming Languages Syntax and Semantics Each programming language contains its own rules for both syntax and semantics. Syntax.

Slides:



Advertisements
Similar presentations
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
True or false A variable of type char can hold the value 301. ( F )
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Program Design and Development
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
Introduction to C Programming
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CIS Computer Programming Logic
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
Pascal Programming Strings, Arithmetic operators and output formatting National Certificate – Unit 4 Carl Smith.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat.
Copyright 1999 by Larry Fuhrer. Pascal Programming Getting Started...
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
PHP Logic. Review: Variables Variables: a symbol or name that stands for a value – Data types ( Similar to C++ or Java): Int, Float, Boolean, String,
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Programming, an introduction to Pascal
Pascal Programming Today Chapter 11 1 Chapter 11.
Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
BY ILTAF MEHDI (MCS, MCSE, CCNA)1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI (MCS, MCSE, CCNA)2 Chapter No: 04 “Loops”
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Chapter 4 – C Program Control
REPETITION CONTROL STRUCTURE
Primitive Data, Variables, Loops (Maybe)
Variables, Expressions, and IO
Week 4 – Repetition Structures / Loops
Chapter 2.2 Control Structures (Iteration)
User-Defined Functions
Formatted and Unformatted Input/Output Functions
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
Arrays, For loop While loop Do while loop
Chapter 4: Algorithm Design
Chapter 2 - Introduction to C Programming
Programming Funamental slides
Chapter 2 - Introduction to C Programming
Programming Funamental slides
1) C program development 2) Selection structure
Iteration: Beyond the Basic PERFORM
Chapter 4: Algorithm Design
Chapter 2 - Introduction to C Programming
Lecture3.
SELECTIONS STATEMENTS
Chapter 2 Programming Basics.
Computer Science 1 For..do loop Dry Run Take notes on the For loop
Chapter 4 - Program Control
DATA TYPES There are four basic data types associated with variables:
C Programming Mr. KAJAL MAJI ASSISTANT PROFESSOR DEPARTMENT OF PHYSICS
COMPUTING.
Getting Started With Coding
Presentation transcript:

15.1 More About High-Level Programming Languages Syntax and Semantics Each programming language contains its own rules for both syntax and semantics. Syntax A set of rules that specifies how to write instructions Tells programmers how to combine symbols, numbers and letters that can be used when writing instructions. Semantics Determines the meaning of instructions

15.1 More About High-Level Programming Languages Syntax and Semantics Each programming language contains its own rules for both syntax and semantics. Syntax A set of rules that specifies how to write instructions Tells programmers how to combine symbols, numbers and letters that can be used when writing instructions. Semantics Determines the meaning of instructions

15.2 Input Statements, Output Statements and Assignment Statements Input Statement A flexible program can handle different sets of data without changing its program code. This requires that the data be separated from the program. Data Set 1 Program to process data Result Set 1 Data Set 2 Program to process data Result Set 2 Input Output Same program

15.2 Input Statements, Output Statements and Assignment Statements Input Statement Obtain inputs from users and store data as variables DescriptionPseudocodeCPascal Input an integer A Input A Int A; scanf (“%d”, &A); VAR A: integer; readln(A) Input a character Ch Input Ch Char Ch; scanf (“%c”, &Ch); VAR Ch: Char; readln(Ch) Input a real number Y Input Y float Y; scanf (“%f”, &Y); VAR Y: real; readln(Y) Examples of input statements

15.2 Input Statements, Output Statements and Assignment Statements Output Statements used to print the result of processed data The output can be displayed on the screen, saved to a file, or printed on a paper The output is usually formatted DescriptionPseudocodeCPascal Output a string ABC Output “ABC”printf(“ABC”);writeln(‘ABC’); Output an integer 10 Output 10printf(“10”);writeln(‘10’); Output an integer- typed variable X Output Xprintf(“%d”, X);writeln(X); Output a real-typed variable R Output Rprintf(“%f”, R);writeln(R); Examples of output statements

15.2 Input Statements, Output Statements and Assignment Statements Variable A kind of memory location The variable value is the content of the memory location Can be used to store data while the program is running

15.2 Input Statements, Output Statements and Assignment Statements Assignment statement Used to place a value of an expression into a variable Assignment operator Literally means ‘become’ = in C and := in Pascal Format of an assignment statement variable name = expression

15.2 Input Statements, Output Statements and Assignment Statements Assignment Statements StatementEvent X = 100 The value 100 is assigned to X. X = 100 * 2 The expression is an arithmetic expression and X is assigned to be 100 x 2. Therefore, X becomes 200 X = X + 20 The expression contains a variable X and X is assigned to be X X (its original value is 200) + 20 = 220. Therefore 220 is assigned to X. The value change of X in different assignment statements

15.2 Input Statements, Output Statements and Assignment Statements Assignment Statements DescriptionPseudocodeCPascal Assign 9 to X X = 9X = 9;X := 9; Assign a number K to Y Y = KY = K;Y := K; Add A to B B = B + AB = B + A;B := B + A; Subtract A from B B = B - A B := B – A; Increase M by1 M = M + 1M++;M := M + 1; Decrease N by 1 N = N - 1N--;N := N – 1; Examples of assignment statements

15.2 Input Statements, Output Statements and Assignment Statements Assignment Statements OperatorDescriptionExample + Add A + B - Subtract A – B * Multiply A * B / Divide A / B Sqr Square Sqr(A) Sqrt Square root Sqrt(A) x^y Power of y A^9 Mod Remainder A Mod B Common operators

15.2 Input Statements, Output Statements and Assignment Statements Assignment Statements OperatorDescriptionExample + Add A + B - Subtract A – B * Multiply A * B / Divide A / B Sqr Square Sqr(A) Sqrt Square root Sqrt(A) x^y Power of y A^9 Mod Remainder A Mod B Common operators

15.3 Sequence, Selection and Iteration Sequence control structure The simplest type of control structure Contain a series of steps or statements that are executed in the order in which they are written PseudocodeFlowchart statement_1 statement_2 …… statement_n statement_1statement_2statement_n

15.3 Sequence, Selection and Iteration Selection control structure Allow the program to choose between two or more alternatives depending on the specific conditions Type of selection control structure PseudocodeFlowchart with ELSE part IF { condition } THEN { THEN part } ELSE { ELSE part } ENDIF ELSE partTHEN part Condition NoYes

15.3 Sequence, Selection and Iteration Selection control structure Type of selection control structure PseudocodeFlowchart without ELSE part IF { condition } THEN { THEN part } THEN part Condition Yes No

15.3 Sequence, Selection and Iteration Selection control structure Relational operator ExampleExplanation =X = 100 X is equal to 100 <>X <> 100 X is NOT equal to 100 >X > 100 X is greater than 100 <X < 100 X is less than 100 >=X >= 100 X is greater than or equal to 100 <= X <= 100 X is less than or equal to 100 Examples of relational operators

15.3 Sequence, Selection and Iteration Selection Logical operator ExampleExplanation AND(X = 1) AND (Y = 5) X is equal to 1 and Y is equal to 5. OR(X = 1) OR (Y = 5) X is equal to 1 or Y is equal to 5. NOTNOT(X = 1) X is NOT equal to 1. Examples of logical operators

15.3 Sequence, Selection and Iteration Iteration The same set of statements (the body) will be repeated as long as a certain condition is met Pretest loop The condition is tested before the body of the loop Posttest loop The condition is tested after the body of the loop

15.3 Sequence, Selection and Iteration Iteration Loop-body Conditio n Yes No Loop-body Conditio n Yes No Pretest loop Posttest loop

15.3 Sequence, Selection and Iteration Iteration FOR-loop Repeat the statements in the loop-body a specific number of times Suggested format of a FOR-loop: FOR { variable } = { initial value } TO { final value } { loop-body } NEXT

15.3 Sequence, Selection and Iteration Iteration WHILE-loop A pretest loop The body is executed repeatedly while the specified condition is met. WHILE { condition } { loop-body } Loop-body Conditio n Yes No

15.3 Sequence, Selection and Iteration Iteration DO…WHILE-loop A posttest loop The program will run the loop-body at least once and then check whether the condition is met DO { loop-body } WHILE { condition } Loop-body Conditio n No Yes

15.3 Sequence, Selection and Iteration Iteration REPEAT…UNTIL-loop A posttest loop The loop-body will run repeatedly until the ‘exit condition’ is met REPEAT { loop-body } UNTIL { condition } Loop-body Conditio n Yes No

15.3 Sequence, Selection and Iteration Iteration REPEAT…UNTIL-loop A posttest loop The loop-body will run repeatedly until the ‘exit condition’ is met REPEAT { loop-body } UNTIL { condition } Loop-body Conditio n Yes No

15.4 Tracing Program Flow Dry Run Used to examine the logic and code of a program Steps of dry run Prepare a set of test data Create a trace table Input the test data into the program Let the test data run through the program one line at a time Write down the values of the program variables as they change

15.4 Tracing Program Flow Dry Run Example PROGRAM DVT 10Input Num1 ; allow users to enter a number 20Num2 = Num Num1 = Num Num2 = Num1 × 5 50Num2 = Num2 / 2 60Output Num1, Num2

15.4 Tracing Program Flow Dry Run Example StatementValue of Num1Value of Num2 Input Num1 3 (test data)Undefined Num2 = Num Num1 = Num Num2 = Num1 × Num2 = Num2 / Suppose the value of the test data is 3. therefore Num1 = 3. At the end of PROGRAM DVT, Num1 = 4 and Num2 = 10.

15.4 Tracing Program Flow Echo Checking To insert output statements at strategic locations in the program Aim: The intermediate values can be printed at each stage of the calculation or logic.

15.4 Tracing Program Flow Echo Checking C programPascal program /* Program DVT */ #include Void main() { int Num1, Num2; printf (“Enter a number: “); scanf (“%d”, &Num1); printf (“%d and %d\n”, Num1, Num2); Num2 = Num1 + 2; printf (“%d and %d\n”, Num1, Num2); Num2 = Num1 – 1; printf (“%d and %d\n”, Num1, Num2); Num2 = Num1 * 5; printf (“%d and %d\n”, Num1, Num2); Num2 = Num1 / 2; printf (“%d and %d\n”, Num1, Num2); printf (“The results: “); printf (“Num1 = %d and Num2 = %d\n”, Num1, Num2); } { Program DVT } PROGRAM DVT; VAR Num1, Num2 : integer; BEGIN WRITE (‘Enter a number: ‘); readln (Num1); writeln (Num1, ‘ and ‘, Num2); Num2 := Num1 + 2; writeln (Num1, ‘ and ‘, Num2); Num2 = Num1 – 1; writeln (Num1, ‘ and ‘, Num2); Num2 := Num1 * 5; writeln (Num1, ‘ and ‘, Num2); Num2 = Num1 DIV 2; writeln (Num1, ‘ and ‘, Num2); write (‘The results: ‘); writeln (‘Num1=’, Num1, ‘ and Num2=’, Num2) END Perform echo checking to print intermediate values

15.4 Tracing Program Flow Echo Checking Enter a number: 3undefined The results: Num1 = 4 and Num2 = 20 The output of the above program when the value of test data is 3

15.4 Tracing Program Flow Echo Checking Enter a number: 3undefined The results: Num1 = 4 and Num2 = 20 The output of the above program when the value of test data is 3