 Constants A constant is a fixed value of a data type that cannot be changed Integer Constants Whole numbers → Do not have decimal points Examples: 83.

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
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
1 ICS 101 – LAB 2 Arithmetic Operations I Putu Danu Raharja kfupm.edu.sa Information & Computer Science Department CCSE - King Fahd University.
Chapter 2 Basic Elements of Fortan
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Data types and variables
1 Chapter 2: Data Types & Operations Part 1 ICS101: Computer Programming Al-Hashim, Amin G.
Chapter 2 Data Types, Declarations, and Displays
Introduction to C Programming
21 March, 2000 CS1001 Lecture 4 Data Types + Algorithms = Programs Data Types & Arithmetic Errors Arithmetic Operators and Functions Program Testing Tips.
1 Chapter 2: Data Types & Operations Part 3 ICS101: Computer Programming Al-Hashim, Amin G.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
1 CSC103: Introduction to Computer and Programming Lecture No 6.
Chapter 2: Using Data.
Unit 3 Lesson 2: Rational Expressions
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Arithmetic Expressions
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
C BASICS QUIZ (DATA TYPES & OPERATORS). C language has been developed by (1) Ken Thompson (2) Dennis Ritchie (3) Peter Norton (4) Martin Richards.
CHAPTER 2:BASIC FORTRAN Data Types INTEGER REAL COMPLEX CHARACTER LOGICAL.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
Chapter 2: Basic Elements of C++
Chapter 7: Expressions and Assignment Statements
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chapter 2 - Introduction to C Programming
Chapter 7: Expressions and Assignment Statements
Arithmetic operations & assignment statement
Chapter 2 - Introduction to C Programming
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
Chapter 2 - Introduction to C Programming
Chapter-3 Operators.
Chapter 2 - Introduction to C Programming
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Core Objects, Variables, Input, and Output
Chapter 3 Operators and Expressions
Chapter 2 - Introduction to C Programming
Unit 3: Variables in Java
DATA TYPES AND OPERATIONS
Introduction to C Programming
ICS 101 Lab 3 Hossain Arif ICS Dept
Chapter 2 Modular Programs with Calculations and Strings
Presentation transcript:

 Constants A constant is a fixed value of a data type that cannot be changed Integer Constants Whole numbers → Do not have decimal points Examples: DATA TYPES AND OPERATIONS

 Constants Real Constants Numbers that have decimal points Examples: Logical Constants Two values.TRUE..FALSE. Character Constants One character or string of characters between two single quotes 'THIS IS CHAPTER TWO' 'ISN''T IT?'

 Variables Occupies a place in the computer’s memory Must have a name to be referenced later Its value could be changed May be of different types - Integer - Real - Logical - Character

 Variable Names There are some rules for choosing variable names in FORTRAN Should start with an alphabetic character ( A, B, C,…,Z ) Its length should not exceed 6 characters Could contain digits (0, 1, 2,…., 9) but not the first character Should not contain special characters Should not contain blanks

 Variables Integer Variables Can hold only integer values Can be defined using INTEGER statement Examples: INTEGER A, B, X, NUM INTEGER Y Real Variables Can hold only real values Can be defined using REAL statement Examples: REAL X, Y, WAT REAL AB3

 Variables Implicit definition it is a good practice to explicitly define all variables used in your program Variables that are assigned values but not defined will be assumed to be of REAL type unless the variable name starts with any of the following letters: I J K L M N if the variable name starts with I J K L M N and not defined, it will be assumed as INTEGER

 Variables Logical Variables Can only have logical values Values can be -.TRUE. -.FALSE. Can be defined using LOGICAL statement Example: LOGICAL FLAG, TEST, FLAG1

 Variables Character Variables Can hold only character values Can be defined using CHARACTER statement The length can be defined, otherwise will be assumed as 1 Examples: CHARACTER NAME*10 CHARACTER T1, T2 CHARACTER A*8, B CHARACTER*5 Z, Z1, Z2 CHARACTER*7 Z, Z1*3, Z2

Arithmetic Operations Addition, Subtraction, Multiplication, Division, Exponentiation Operators: + - * / ** Examples: X – Y X + Y – 4 / Z – A + B – C Priority ( ) ** * / + -

Arithmetic Operations Integer Operations The result of arithmetic operations with both operands as integer is integer Examples: 70 – 31 3**2 8 / 3 Real Operations The result of arithmetic operations with both operands as real is real Examples: 70.0 – ** / 3.0

Arithmetic Operations Mixed-mode Operations The result of an arithmetic operation with one integer operand and one real operand is real Examples: 70.0 – 31 3** / 3 70 – **2 8 / 3.0

Examples Example 1: Evaluate the following arithmetic expression / 5 * 2 ** 2 ** 3 Example 2: Evaluate the following arithmetic expression 14.0 / 5 * (2 * (7 - 4) / 4) ** 2

Examples Example 3: Rewrite the following FORTRAN expression as a mathematical form X + Y / W – Z Example 4: Rewrite the following FORTRAN expression as a mathematical form X ** (1.0 / 2.0) / Y ** Z Example 5: Convert the following mathematical expression into FORTRAN expression. Use minimum number of parenthesis

Logical Operations Logical Operations evaluate to either.TRUE. or.FALSE. Logical Operators.AND..OR..NOT. Example :.FALSE..OR..NOT..TRUE..AND..TRUE. Relational Operators - The values of arithmetic expressions can be compared using relational operators - The result of a relational operation is.TRUE. or.FALSE. Relational Operators :. EQ..NE..GT..GE..LT..LE. Examples : X.EQ. Y Z + A.GT. X

Logical Operations Logical Expressions evaluate to either.TRUE. or.FALSE. Example 1: Given that X has a value of 3.0, Y has a value of 5.0, Z has a value of 10.0, and FLAG is a logical variable with.FALSE. Value, evaluate the following FORTRAN expression:.NOT.FLAG.AND. X*Y.GT. Z.OR. X+Y.GT. Z Priority Arithmetic expressions Relational expressions Logical expressions.NOT. FLAG.OR. FLAG.NOT. FLAG. AND. FLAG.NOT..NOT. FLAG X.GT. Y – Z / 2.0

Assignment Statement The Assignment Statement in FORTRAN assigns a value to a variable. The general form is: variable = expression Exception - integer values can be assigned to real variables - real values can be assigned to integer variables Example: INTEGER M, N REAL A, B A = 6.0 B = A + 9/2 M = B N = B A = N A = M + N N = A + B M = N + 3 **3.0 A = B + M

Input Statement READ*, list of variables separated by commas Note the followings each reading statement starts reading from a new line reading continues from the next line if the input data is not enough data values in a line should be separated by commas or blanks data values must agree in types with the variables they are read into - except that integer values can be read into real variables - but real values can not read into integer variables

Output Statement PRINT*, list of variables, expressions, or constants separated by commas Note the followings each PRINT statement starts printing on a new line printing continues in the next line if the line is not enough to hold the output of the print statement a variable that does not have a value will produce ???? if it is printed

A Complete Program The following program reads three real numbers, prints them, computes their average and prints it: C THIS PROGRAM READS 3 REAL NUMBERS CAND COMPUTES AND PRINTS THE AVERAGE C REAL NUM1, NUM2, NUM3, SUM, AVG PRINT*, 'ENTER THREE REAL NUMBERS' READ*, NUM1, NUM2, NUM3 PRINT*, ' THE NUMBERS ARE', NUM1, NUM2, NUM3 SUM = NUM1 + NUM2 + NUM3 AVG = SUM / 3 PRINT*, ' THE AVERAGE IS', AVG END