Laboratory 3 OPERATORS, EXPRESSIONS, INSTRUCTIONS I. THEORETICAL BACKGROUND

Similar documents
Computers Programming Course 7. Iulian Năstac

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Arithmetic Operators. Portability: Printing Numbers

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Operators in C. Staff Incharge: S.Sasirekha

Informatics Ingeniería en Electrónica y Automática Industrial

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Laboratory 4. INSTRUCTIONS (part II) I. THEORETICAL BACKGROUND

Prepared by: Shraddha Modi

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Computers Programming Course 6. Iulian Năstac

Department of Computer Science

Expression and Operator

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Operators. Java operators are classified into three categories:

UNIT 3 OPERATORS. [Marks- 12]

C Programming Language (Chapter 2 of K&R) Variables and Constants

Fundamentals of Programming

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

Part I Part 1 Expressions

OBJECT ORIENTED PROGRAMMING

Operators and Expressions:

Computers Programming Course 5. Iulian Năstac

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

Operators in java Operator operands.

Data Types and Variables in C language

Pointers. 10/5/07 Pointers 1

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Fundamental of Programming (C)

Operators and Type Conversion. By Avani M. Sakhapara Assistant Professor, IT Dept, KJSCE

Operators & Expressions

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

A flow chart is a graphical or symbolic representation of a process.

C-LANGUAGE CURRICULAM

JAVA OPERATORS GENERAL

MCA Semester 1. MC0061 Computer Programming C Language 4 Credits Assignment: Set 1 (40 Marks)

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Lecture 02 C FUNDAMENTALS

Expressions and Precedence. Last updated 12/10/18

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.


Basic Assignment and Arithmetic Operators

C Programming Class I

Quick Reference Guide

C/C++ Programming Lecture 7 Name:

Expressions and Statementst t. Assignment Operator. C Programming Lecture 6 : Operators. Expression

C Programming Multiple. Choice

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Laboratory 10 POINTERS I. FUNDAMENTALS


COMP 208 Computers in Engineering

Review of the C Programming Language for Principles of Operating Systems

I BCA[ ] SEMESTER I CORE: C PROGRAMMING - 106A Multiple Choice Questions.

UNIVERSITY OF LIMERICK OLLSCOIL LUIMNIGH COLLEGE OF INFORMATICS & ELECTRONICS DEPARTMENT OF ELECTRONIC & COMPUTER ENGINEERING

Reserved Words and Identifiers

HISTORY OF C LANGUAGE. Facts about C. Why Use C?

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

Subject: PIC Chapter 2.

Programming. Elementary Concepts

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

File Handling in C. EECS 2031 Fall October 27, 2014

CS313D: ADVANCED PROGRAMMING LANGUAGE

Java Primer 1: Types, Classes and Operators

UNIT- 3 Introduction to C++

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Chapter 3: Operators, Expressions and Type Conversion

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme]

FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING UNIT IV INTRODUCTION TO C

These are reserved words of the C language. For example int, float, if, else, for, while etc.

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Chapter 4 C Program Control

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Part I Part 1 Expressions

DEPARTMENT OF MATHS, MJ COLLEGE

UNIT IV INTRODUCTION TO C

.Net Technologies. Components of.net Framework

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Infix to Postfix Conversion

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

Flow Control. CSC215 Lecture

Review of the C Programming Language

Department of Computer Applications

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Chapter 3. Section 3.10 Type of Expressions and Automatic Conversion. CS 50 Hathairat Rattanasook

Structures, Operators

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Basics of Programming

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

Engineering Computing I

Language Reference Manual simplicity

Transcription:

PROGRAMMING LANGUAGES Laboratory 3 OPERATORS, EXPRESSIONS, INSTRUCTIONS I. THEORETICAL BACKGROUND 1. Operators and expressions An expression combines variables and constants in order to produce new values. 1.1. Primary expressions: identifier constant string (expression) primary expression [expression] primary expression (expression list) lvalue. identifier primary expression => identifier Obs.: Expression list is described as follows: expression expression list, expression 1.2. Unary operators: *, &, -,!, ~, ++, --, (type name), sizeof. Unary expressions (are grouped from left to right): Explanations: * expression & lvalue - expression! expression ~ expression ++ lvalue -- lvalue lvalue ++ lvalue -- (type name) expression sizeof expression sizeof (type name) a) * => the indirection operator. The expression following it is a pointer and the result is a lvalue. E.g.: y = *px /* y represents the content of the address pointed by px */ b) & => the operator that obtains a pointer. The operand is lvalue and the result is a pointer. E.g.: px = &x ; y = *px ; <=> y = x Obs.: & applies for variables. Formats such as the following are wrong: &(x+1) &3 c) - => negation operator

d)! => logical negation operator. The result is either 0 or 1. e) ~ => operator for complementing to 1. Converts the bytes: The operands are either "short int" or "long int". 0 -> 1 1 -> 0 f) ++ => operator that increments the operand with 1. Obs.: ++n => increments n before using it n++ => increments n after its values has been used g) -- => the decrementing operator. h) (type name) => operator for type conversion. It produces the conversion of the expression value to the chosen type. This construction is also named "cast". i) sizeof => returns the dimension in bytes associated to its operand. Obs.: sizeof (type name) => returns the dimension in bytes of an object of the desired type sizeof (type) => is taken as an unit E.g.: sizeof (type) - 2 <=> (sizeof (type)) - 2 1.3. Multiplication operators: *, /, % They are grouped from the left to the right: * => multiplication / => division % => produces the rest when dividing the first expression to the second one. E.g.: the expression (a/b)*b+a%b is equal to a 1.4. Additive operators: + (add), - (substract) 1.5. Bitwise shift operators: <<, >> The format: expression << expression expression >> expression << => bitwise left shift >> => bitwise right shift Obs.: The operands situated to the left of the operator have to be of the integer type. The operator in the right is converted to the "int" type. The free bytes turn to 0. 1.6. Relational operators: <, >, <=, >= They produce the 1 value if the specified relation is true and 0 if is false. Obs.: i < x - 1 <=> i < (x - 1) 1.7. Comparison operators: == (equality),!= (distinct) E.g.: a < b == c > d <=> 1, if a<b and c>d 0, otherwise

1.8. The byte operator AND: & & 0 1 --------- 0 0 0 1 0 1 1.9. The byte operator EXCLUSIVE OR: ^ ^ 0 1 -------- 0 0 1 1 1 0 1.10. The byte operator INCLUSIVE OR: " " 0 1 -------- 0 0 1 1 1 1 Obs.: It could be used for bits setting. E.g.: x = x mask, where mask is placed on the bits of interest. 1.11. The operators LOGIC AND (&&) and LOGIC OR ( ) They are grouped from left to right and the result is 0 or 1. 1.12. The conditional operator: "? :" The conditional expression: expression1? expression2 : expression3 If the value of expression1 is: 1 then the result is expression2 0 - the result is expression3 Obs.: Pay attention to the syntax. lval = ET1? (ET2? e1 : e2) : e3 lval = ET1? e1 : ET2? e2 : e3 is correct is wrong 1.13. Assignment operators: =, +=, -=, *=, /=, %=, >>=, <<=, &=, ^=, =. They are grouped from right to left. Assignment expression: lvalue operator expression If the operator is "=", the value of the expression replaces the one of the object determined by the lvalue. Expressions such as: E1 op= E2 are equivalent to E1 = E1 op E2 where "op" is one of the operators: +, -, *, /, %, >>, <<, &, ^ or E.g.: x *= y + 1 <=> x = x * (y + 1) not x = x * y + 1 Obs.: Pentru += and -=, the left operator could also be a pointer.

1.14. The comma operator:, The comma expression: expression1, expression2 They are grouped from left to right. 2. Instructions 2.1. The conditional instruction (if...) if(expression) instruction1; instruction2; Obs.: "" is connected to the last encountered "if". E.g.: if (exp1) instr1; if (exp2) instr2; if (exp3) instr3; instr4; 2.2. The "while" instruction while (expression) instruction; The instruction is recurrently executed as long as the expression s value is not 0. The test takes place at the beginning and therefore if from the first check the expression is 0, the instruction from the while body does not take place at all. 2.3. The "do...while" instruction do instruction while (expression); The instruction is recurrently executed until the expression becomes 0. The test takes place after each instruction execution and therefore the instruction will take place at least once. 2.4. The "for" loop: for (exp1; exp2; exp3) instruction; where: exp1 => initialises the loop (optional) exp2 => the end test of the loop (optional) exp3 => updating the index variable(optional) The for loop is equivalent to: exp1; while (exp2) instruction; exp3; 2.5. The "break" instruction: This instruction forcedly terminates any "while", "do...while", "for" or "switch" instruction that contains it. The program then jumps to the next instruction. This instruction is not used within "if..." instructions or directly inside a function.

2.6. The "switch" instruction (for commutation): switch (exp) case exp1 : sir1 case exp2 : sir2 default : string 2.7. The "continue" instruction: Format: continue; Forcedly jumps to the next interation within the "while", "do...while" or "for" instruction it is placed in. 2.8. The "return" instruction: Format: return; or return (expression); 2.9. The "goto" instruction: goto label; Obs.: The term label must start with a letter. 2.10. The instruction label: Any instruction could be preceded by a label, with the following syntax: identifier: 2.11. The null instruction Format: ; E.g.: for (nc=0; getchar ( )!= EOF; ++nc); II. Assignment Workflow Write a C program that receives from the keyboard two operands (two numbers) and an operator, computes the operations between the two operands using the operand and displays the result. The program is described as follows: - two variables (operands) of float type are defined and one int variable is defined for the operator - the operands are read from the keyboard; - the operator is also read and if it is: '+' - the numbers are added together; '-' - the numbers are substracted; '*' - the numbers are multiplied; '/' - if the second operand is not 0, the division is computed, otherwise an error message is displayed any other character - an error message is displayed; - screen visualisation until pressing another key.

In "C" language the program becomes: # include<stdio.h> main() int oper; float x,y; printf("\noperand 1: "); scanf("%f",&x); printf("\noperand 2: "); scanf("%f",&y); printf("\noperator : "); if((oper=getche ( ))== '+') printf("\naddition result: %f\n", x+y); if(oper=='-') printf("\nsubstraction result: %f\n", x-y); if(oper=='*') printf("\nmultiplication result: %f\n", x*y); if (oper=='/') if (y==0) printf("\nerror: Division by 0. \n"); printf("\ndivision result: %f\n", x/y); if (oper=='%' oper=='&' oper==':') printf("\nthe operation has not taken place \n"); printf("\nerror\n"); getch ( ); 1. Write the program from above in the "C" editor. 2. Modify the program so that the reading routine to be performed recurrently until a certain key is pressed. 3. Modify the program so that the selection would be made using a "switch" instruction.

III. SOLUTIONS 2. The program modified with a "while": # include<stdio.h> main() int sfarsit; sfarsit=1; while(sfarsit!='0') int oper; float x,y; printf("\noperand 1: "); scanf("%f",&x); printf("\noperand 2: "); scanf("%f",&y); printf("\noperator : "); if ((oper=getche( ))== '+') printf("\naddition result: %f\n", x+y); if (oper== '-') printf("\nsubstraction result: %f\n", x-y); if (oper== '*') printf("\nmultiplication result: %f\n", x*y); if (oper== '/') if (y==0) printf ("\nwrong operator 2\n"); printf ("\ndivision result: %f\n", x/y); if (oper == '%' oper == '& ' oper == ':') printf("\nthe operation was not computed\n"); printf ("\nerror\n"); printf ("\nhit 0 to leave the program\n"); finish = getch( ); 3. The program rewritten with the "switch" instruction: # include <stdio.h> main( ) int ready; ready = 1; while (ready!= '0') int oper; float x, y; printf("operand 1: "); scanf("%f", &x); printf("operand 2: "); scanf("%f", &y); printf("operator : "); oper = getche (); switch (oper) case '+': printf("\naddition result: %f\n", x+y);

case'-': printf("\nsubstraction result: %f\n", x-y); case'*': printf("\nmultiplication result: %f\n",x*y); case'/': if (y==0) printf ("\n Wrong operator 2\n"); printf("\ndivision result: %f\n", x/y); case'%': case'&': case':': printf ("\nthe operation was not computed\n"); default: printf ("\nerror\n"); printf("\npress 0 to exit the program\n"); ready=getch();