Dr. Sajib Datta Jan 21, 2014.  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =

Slides:



Advertisements
Similar presentations
© 2007 Lawrenceville Press Slide 1 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5;
Advertisements

Chapter 4 Computation Bjarne Stroustrup
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
Computer Programming w/ Eng. Applications
L5:CSC © Dr. Basheer M. Nasef Lecture #5 By Dr. Basheer M. Nasef.
Lecture 2 Introduction to C Programming
Introduction to C Programming
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Conditionals – if - else Dr. Sajib Datta
True or false A variable of type char can hold the value 301. ( F )
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Operator. Assignation (=). The assignation operator serves to assign a value to a variable. a = 5; It is necessary to emphasize that the assignation operation.
1 Introduction to Computers and Programming Class 3 Introduction to C Professor Avi Rosenfeld.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
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.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
CSCI 130 Chapter 4 Statements, Expressions, and Operators.
Exercises A-Declare a variable of double type with initial value=0.0; B- Declare a constant of String type with initial value=“Good” C- Declare a variable.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CPS120: Introduction to Computer Science Operations Lecture 9.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Khalid Rasheed Shaikh Computer Programming Theory 1.
1 Expressions. 2 Variables and constants linked with operators  Arithmetic expressions Uses arithmetic operators Can evaluate to any value  Logical.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
4. EXPRESSIONS. Display the value of pi, to 5 decimal places, right justified, in 9 columns Read in someone’s height in feet and inches using.
Doing math In java.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CSE 1320 Basics Dr. Sajib Datta
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Dr. Sajib Datta Jan 23,  A precedence for each operator ◦ Multiplication and division have a higher precedence than addition and subtraction.
From Algorithms to Programs Both are sets of instructions on how to do a task Algorithm: –talking to humans, easy to understand –in plain (English) language.
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.
Dr. Sajib Datta Sep 3,  A new operator used in C is modulus operator: %  % only used for integers, not floating-point  Gives the integer.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
CSE 220 – C Programming Expressions.
Expressions.
Arithmetic operations & assignment statement
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
CSE1320 INTERMEDIATE PROGRAMMING Operators+Conditionals+Loop
Structure of a C Program
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
Expressions and Assignment
Data Types and Expressions
Introduction to C Programming
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

Dr. Sajib Datta Jan 21, 2014

 Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height = 5;  Define - Declare & Initialize ◦ int height = 5; ◦ char name = ‘a’; ◦ double marks= 90.3;

 Print a variable ◦ printf(“%d”, num); - format specifier, variable ◦ Number matching - the number of specifiers is equal to the number of variables  Type matching ◦ %d for int ◦ %lf for double ◦ %c for char  printf can be used just to print text ◦ printf(“this is just a text”);

 A C program consists of statements  Some types of statements: ◦ declaration statements ◦ Assignment statements ◦ Function calls ◦ Control statements  Each statement is terminated by a semicolon.  Control statement can change the program flow.

 Declaration statements are when we declare variables for use. void main() { int a; …… }

 Have a left side and a right side.  The right side: ◦ a single value, a complicated expression, or a function call ◦ ultimately reduce to a single value, which is then assigned to the variable named on the left side.  Example: ◦ int num; [declaration] ◦ num = 1; ◦ num = num + 10; ◦ num = num + 2; [what is the final value of num?]

 The basic operators that you have in math are also available in C: ◦ +, ◦ -, ◦ *, ◦ /, ◦ =  WARNING: Difference between operators in C and their math use is integer division.  The fraction resulting is truncated in integer division ◦ integer = integer / integer  Example: ◦ int a = 7, b = 5; ◦ int answer; ◦ answer = a / b;

float a = 7.1, b = 5.2; int answer; answer = a / b;

 A new operator used in C is modulus operator: %  % only used for integers, not floating-point  Gives the integer remainder from integer division.  Example: ◦ int a = 7, b = 3; ◦ int answer; ◦ answer = a % b;

The following assignment operators are available in C:  += addition  -= subtraction  *= multiplication  /= division  num = num + 2 same as num+=2;  num = num - 1 same as num-=1;

 What is the output of ‘x’? ◦ int x; ◦ x = 10; ◦ x += x; ◦ printf(“%d”, x);

 We discussed: ◦ Omega ◦ Variable  Declaration, assigning a value, initialization ◦ Statements  Conditional statements can change the flow of the code ◦ Operators  +,-,*,% ◦ Data type  What happens when you assign a float/double to a variable of type int

 A precedence for each operator ◦ Multiplication and division have a higher precedence than addition and subtraction  For operators with the same precedence, if they share an operand, they are executed according to the order they occur in the statement. (In most cases, from the left to the right, except assignment operation)  Examples: ◦ int num, x; ◦ num = 2; ◦ x = 1 + 3*num/4; What is the value of x?

 Want an addition operation to take place before division? ◦ int num, x; ◦ num = 2; ◦ x = (1 + 3)*num/4;  What is x now?  int a = 1, b = 4;  b += a+2; is equivalent to b = b + (a + 2)

 < : is less than  <= : is less than or equal to  == : is equal to  >= : is greater than or equal to  > : is greater than  != : is not equal to  Example: ◦ Relational expression: a > 10  If the relation is true, the value of the expression is 1, otherwise 0.

 A combination of operators and operands, where operands can be constants, variables, or combinations of the two  4  4+21  a = (b+c)/2  q>4

 A statement is a complete instruction to the computer  In C, it is indicated by semicolon  An statement is consists of expressions