Operators and Expressions

Slides:



Advertisements
Similar presentations
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Advertisements

1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
CS150 Introduction to Computer Science 1
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 3 Control Statements.
1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators.
1  Ex: Declare a variable to store user’s age: int age; Prompt the user to enter his/her age: printf (“ How old are you? “); Read / scan the entered value.
CSCI 130 Chapter 4 Statements, Expressions, and Operators.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
2440: 211 Interactive Web Programming Expressions & Operators.
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Chapter 2: Using Data.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Lesson - 7. Operators There are three types of operators: Arithmetic Operators Relational and Equality Operators Logical Operators.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
CPU Control Unit Main memory ALU INPUTINPUT OUTPUTOUTPUT ฯ OS Compiler OS   Compiler.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Do Now 9/7/10 Copy HW in your planner. –Text p.7-8, #12-30 evens, #42-48 evens –Get Letter Signed Open your notebook or binder to a new page and put the.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
Input, Output and Variables GCSE Computer Science – Python.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Arithmetic Expressions
Using the Console.
Operators And Expressions
Section 3 Review Mr. Crone.
INSPIRING CREATIVE AND INNOVATIVE MINDS
for Repetition Structures
2.5 Another Java Application: Adding Integers
Chapter 2 Assignment and Interactive Input
Nahla Abuel-ola / WIT.
Chapter 5: Loops and Files.
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Java Programming: From Problem Analysis to Program Design, 4e
Learning About the Loop Structure (continued)
Operators and Expressions
Chapter 2 - Introduction to C Programming
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 2: Basic Elements of Java
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Conversion Check your class notes and given examples at class.
Objective - To add and subtract decimals.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 2 - Introduction to C Programming
OBJECT ORIENTED PROGRAMMING I LECTURE 4 PART 2 GEORGE KOUTSOGIANNAKIS
More Loops Topics Relational Operators Logical Operators for Loops.
Using C++ Arithmetic Operators and Control Structures
Operator and Expression
Input, Variables, and Mathematical Expressions
DATA TYPES There are four basic data types associated with variables:
HNDIT11034 More Operators.
Introduction to C Programming
Chapter 3 The New Math.
Presentation transcript:

Operators and Expressions Chapter 2 Operators and Expressions

Save program on floppy Select all (highlight whole program from edit) Copy Open word Paste save

Loading from floppy into C# Start C#, new project Start Word, load program text Select all Copy Go to C# paste

Operators Operation Operator Rank Addition + 2 Subtraction - 2 Multiplication * 1 Division / 1

More on operators Unary – one operand Binary – two operands Ternary – one of two possible results

Addition When you want to add 5 and 10 5+10 using variables: num1 = 5; total = num1 + num2;

Addition How to use: int student1 = 100; int student2 = 95; int sum; sum = student1+student2+student3; Console.WriteLine(“s1 = {0}, s2 = {1}, s3 = {2}”, student1, student2, student3); Console.Writeline(“sum = “ + sum);

Program style // declare section int student1; int student2; int sum; // input section student1=100; student2=95; student3=96; // calculate section sum = student1+student2+student3; // results section Console.WriteLine(“s1 = {0}, s2 = {1}, s3 = {2}”, student1, student2, student3); Console.Writeline(“sum = “ + sum);

Subtraction double salary = 255.32; double benefits = 26.66; double netsal; netsal = salary – benefits; Console.WriteLine(“Sal={0}, Ben={1}, Net={2}”, salary, benefits, netsal);

Multiplication double grade=4.0; double credits=3.0; double qualpts; Console.WriteLine(“With a grade of {0}, and {1} credits, you earned {2} QP”, grade, credits, grade * credits);

Division As with the other operators, merely place it between two numeric variables int hrsofsleep; int hrsofstudy; double grade = hrsofstudy / hrsofsleep;

Allowing User Input double grade; grade = double.Parse(Console.ReadLine()); (Or int or decimal ) Console.Write(“Please enter numeric Grade ”);

Formatting Output When you want to print money without $: netsal = 2600.4566666 Console.WriteLine(“Net Salary {0:N2}”, netsal) Result: Net Salary 2,600.45 Console.WriteLine(“Net Salary {0:C}”, netsal) Result: Net Salary $2,600.45

Increment and Decrement int age = 2; age = age + 2; what is the value of age? old = age++; (called postfix) same as old=age; and then age=age+1 old=++age; Same as age=age+1; and then old=age;

Ternary Operator exp1 ? exp2 : exp3 If exp1 is true, do exp2 If exp1 is false, do exp3