CS 31 Discussion, Week 2 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:30-1:30pm (today)

Slides:



Advertisements
Similar presentations
Algorithms Series of mathematical or variable manipulations Integer a,b,c a = 12 b = 22 c = a * b (what is c?) 12 * 22 = 264.
Advertisements

CS 1400 Chapter 2 sections 1, 2, 4 – 6, 8,
If Statements & Relational Operators Programming.
True or false A variable of type char can hold the value 301. ( F )
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Computer Science 1620 Arithmetic. C++ Math we have seen how to use numbers in our program to represent data however, we can also manipulate this data.
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Basic Elements of C++ Chapter 2.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Introduction to C++ Programming
CS 31 Discussion, Week 4 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:30-1:30pm (today)
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
First steps Jordi Cortadella Department of Computer Science.
CSC 107 – Programming For Science. The Week’s Goal.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
CS Jan 2007 Chapter 2 sections 1, 2, 4 – 6, 8,
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
CS 31 Discussion, Week 5 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:00-1:00pm (today)
Fundamental Programming Fundamental Programming More Expressions and Data Types.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
A Sample Program #include using namespace std; int main(void) { cout
Fundamental Programming Fundamental Programming Data Processing and Expressions.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Bill Tucker Austin Community College COSC 1315
Basic concepts of C++ Presented by Prof. Satyajit De
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Chapter Topics The Basics of a C++ Program Data Types
What Actions Do We Have Part 1
Basic Elements of C++.
Chapter 2 Assignment and Interactive Input
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Introduction to C++ October 2, 2017.
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
Programming Funamental slides
Summary Two basic concepts: variables and assignments Basic types:
CS150 Introduction to Computer Science 1
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
CS150 Introduction to Computer Science 1
Programs written in C and C++ can run on many different computers
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Arithmetic Operations
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

CS 31 Discussion, Week 2 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:30-1:30pm (today)

What’s been covered so far? Program Structure Reading input (cin) and producing output (cout) Variables Expressions Types (int, double, string) Operators (arithmetic, boolean) Control Structures (if, for, while, etc.)

Program Structure Program consists of the following in order: – Includes, e.g. “#include ” – Global statements, e.g. “using namespace std;” – Functions Most important one now: int main() { } – As a matter of good practice, main() should always end with “return 0;” even if it’s not strictly required

Input and Output Output: cout << “Hello, world!” << endl; – What is endl? Input: cin >> someVariable; – Depending on the “type” of someVariable, cin will read differently – Why is there a problem with reading a number followed by a string?

Variables Places to put a value – But what’s a “value”? We’ll get to this with expressions and types The rule of thumb is that a variable must be “declared” before it is used – Declaration: “int myVariable;” – Use: “myVariable = 32;” – Otherwise, the compiler produces the familiar “undefined identifier” message

Expressions An expression is a “value” mentioned before Can be as simple as 32, or as complicated as 46+(12/3)*8 “Resolving” an expression means figuring out what its actual value is when it appears in the program – “int myVariable = 80*1000;” – When the above appears, 80*1000 is computed and stored into myVariable Variables can appear in expressions – Resolving the expression uses the value of the variable at the time of resolution

Types The expressions referred to earlier have “types”, which are kinds of values Sample expressions with their types: – Integer (int): 50+12*3 – Decimal (double): – String: “Hello!” Variables also have types, e.g. “int myVar;” is an integer – A variable can only store expressions of its type – In an expression, a variable confers its type to the expression cout and cin are intelligent enough to deal with all the basic types

Operators Two kinds of operators (for now): – Arithmetic (+, -, *, /) Used for computing mathematical expressions – Boolean (>, =, <=, ==, !=) Used for performing comparisons Expressions can be a single value, but are usually composed of many values strung together by operators – (5+3) < 5 – (5+3) and 5 are expressions on their own, but ‘<‘ strings them together into a larger expression

Operator Precedence Just like in math, operators are not simply computed left to right – Ex: 5+3*2, the 3*2 occurs first, then 5+6 Precedence (lowest to highest): – (+,-),(*,/),(>, =,<=,==,!=) Operators take “operands” and produce a value – The final output value of the operator depends on the types of its operands The type of an expression is the value produced by the operator with the highest precedence – (5+3)*0.25 is 2.0, which is a double since the final multiplication had a double as one of its operands

Control Structures Aside from just printing, reading, and storing, we need some way to produce different results for different inputs (beyond just expressions) If: “if something is true, do this (else do this)” We also need a way to repeat something an unknown number of times – While and for are used for this – Will discuss these with examples