C++/Java. C++: Hello World. Java : Hello World. Expression Statement. Compound Statement. Declaration Statement

Similar documents
INTRODUCTION TO COMPUTER SCIENCE - LAB

CS2141 Software Development using C/C++ C++ Basics

C++ Programming: From Problem Analysis to Program Design, Third Edition

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

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

Programming. C++ Basics

Programming Fundamentals and Methodology. COMP104: C++ Basics

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Introduction to Programming

Tokens, Expressions and Control Structures

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Programming, numerics and optimization

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Pointers and Strings Prentice Hall, Inc. All rights reserved.

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

The New C Standard (Excerpted material)

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

Programming. Elementary Concepts

EEE145 Computer Programming

6.096 Introduction to C++ January (IAP) 2009

Variables. Data Types.

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Chapter 7. Additional Control Structures

Integer Data Types. Data Type. Data Types. int, short int, long int

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Basic Types, Variables, Literals, Constants

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Introduction to Programming using C++

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

LECTURE 02 INTRODUCTION TO C++

Computer Science II Lecture 1 Introduction and Background

Chapter 8 Statement-Level Control Structure

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

1. C++ Overview. C++ Program Structure. Data Types. Assignment Statements. Input/Output Operations. Arithmetic Expressions.

Objectives. In this chapter, you will:

Outline. 1 About the course

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

Variables and Constants

INFO-I308 Laboratory Session #2

3. Java - Language Constructs I

Chapter 2: Introduction to C++

Chapter 2. Procedural Programming

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Lecture 02 C FUNDAMENTALS

Pointers and Strings Chapters 10, Pointers and Arrays (10.3) 3.2 Pointers and Arrays (10.3) An array of ints can be declared as

Enumerated Types. Mr. Dave Clausen La Cañada High School

Increment and the While. Class 15

The C++ Language. Arizona State University 1

THE FUNDAMENTAL DATA TYPES

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Introduction and basic C++ programming

Tutorial-2a: First steps with C++ programming

CS 61C: Great Ideas in Computer Architecture Introduction to C

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Types, Operators and Expressions

CSCE 110 PROGRAMMING FUNDAMENTALS

Input And Output of C++

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

CS 376b Computer Vision

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

A Fast Review of C Essentials Part I

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

C++ Programming: From Problem Analysis to Program Design, Third Edition

Types, Operators and Expressions

Chapter 15 - C++ As A "Better C"

Chapter 2 - Control Structures

OBJECT ORIENTED PROGRAMMING

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

Chapter 8 - Notes User-Defined Simple Data Types, Namespaces, and the string Type

CSE030 Fall 2012 Final Exam Friday, December 14, PM

Introduction to C++ Systems Programming

Definition: Data Type A data type is a collection of values and the definition of one or more operations on those values.

This tutorial adopts a simple and practical approach to describe the concepts of C++.

Computer Programming : C++

Programming with C++ Language

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Outline. 1 Types. 2 union. 3 Bit operations. 4 The comma operator. 5 Multi-dimensional arrays. 6 Templates. 7 Most vexing parse

From Pseudcode Algorithms directly to C++ programs

CSE 230 Intermediate Programming in C and C++

CS Spring 05 - MidTerm

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

Transcription:

C++/Java HB/C2/KERNELC++/1 Statements HB/C2/KERNELC++/2 Java: a pure object-oriented language. C++: a hybrid language. Allows multiple programming styles. Kernel C++/ a better C: the subset of C++ without OO-features. C++: Hello World //Hello world in C++ #include <iostream> //IO library #include <string> //string type using namespace std; void pr_message(string s = Hello world! ) { cout << s << endl; int main() { pr_message(); pr_message( Hello C++/Java programmers! ); A Kernel C++ program. No classes/objects C++ statements: almost like Java Statements: p.375, BS p.132 Expression Statement int j; ++ j; expression ; j= j + 2; j+= 2; cout << Hello\n ; j + 2 ; Compound Statement {tmp=a; a= b; b= tmp; { statement statement... statement Declaration Statement Java : Hello World // Hello world in Java public class hello{ static void pr_message(string s) {System.out.println(s); public static void main(string[] args) { pr_message( Hello world! ); pr_message( Hello C++/Java programmers! ); Every thing is a Class/object A declaration is a statement (we will consider declarations later). { int i=0; i= i+1; double b; cin >> b;

if/if-else Statements #include <iostream> using namespace std; void check_temp(int temperature) { if (temperature >= 32) cout << Above Freezing!\n ; cout << Fahrenheit is << temperature << endl; void check_grade (int grade) { char letter_gr = F ; if (grade > 70 && grade < 80) { cout << you passed ; letter_gr = C ; cout << Letter grade is << letter_gr << endl; void get_min(int x, int y) { int min = 0; if (x < y) min = x; else min = y; cout << min = << min << endl int main() { check_temp(2); check_temp(32); check_temp(45); check_grade(10); check_grade(75); get_min(10,2); get_min(2, 10001); HB/C2/KERNELC++/3 if (condition) statement if (condition) statement if (condition) statement1 else statement2 Fahrenheit is 2 Above Freezing! Fahrenheit is 32 Above Freezing! Fahrenheit is 45 Letter grade is F you passed Letter grade is C min = 2 min = 2 while/for/do statements while: #include <iostream> #include <string> using namespace std; int main() { int i = 0, sum = 0; while (i <= 10) { sum += i; ++i; do: cout << \nsum for i << i << is << sum << endl; int n = 0; do { cout << \nenter a positive integer: ; cin >> n; while (n <= 0); HB/C2/KERNELC++/4 for: for (for-init-statement condition; expression) statement int i = 0, sum = 0; for (i = 1; i <= 10; ++i) sum += i; cout << Sum= << sum << endl;... int sum = 0; cout << Enter five numbers:\n for (int i = 1; i <= 5; ++i) {int tmp; cin >> tmp; sum += tmp; cout << Sum= << sum << endl; while (condition) statement1 do statement while (condition)

break/continue/goto statements double x; for (int i = 0; i < 10; ++i) { cout << \nenter a positive number for square root: ; cin >> x; if (x < 0.0) { cout << All done << endl; break; //exit loop if value is negative cout << sqrt(x) << endl; cout << \n Number of square roots << i << endl; char c; for (int i = 0; i < MAX; ++i) { cin.get(c); if (isdigit(c)) continue; /*proces non-digits*/... //continue jumps to here break/continue interrupts nearest surrounding iterative statement. HB/C2/KERNELC++/5 Illegal: i isn t known here! Java has labeled break, C++ has goto Java: int A[100][100]; searchforkey: // a Java search of a double nested loop for (int i= 0; i< 100; i++) for (int j=0; j<100; ++j) if (A[i][j] == Key) break searchforkey; not in C++ C++ : goto-statements often used for breaking out of multiple levels of looping: for(int i; i<100; ++i) for(int j=0; j<100; ++j) if(a[i][j]== Key) goto FoundKey; FoundKey: //execution comes here on found key goto not in Java!! Use of goto usually considered bad programming. switch statement int a_grades = 0, b_grades = 0, c_grades = 2, fails = 3, score; switch (score) { case 9: case 10: ++a_grades; break; case 8: ++b_grades; break; case 7: ++c_grades; break; default: ++fails; goto label; HB/C2/KERNELC++/6

Expressions BS p. 120 p. 345: operator precedence and associativity p. 366: Arthmetic Expressions, Relational, Equality and Logical Expressions p. 365: Autoincrement/decrement, sizeof operator p. 366: Bit-Manipulation Expressions Conditonal Expression x= (y<z)? y : z; expr1? expr2 : expr3 HB/C2/KERNELC++/7 Simple Types Java type values boolean true, false char 16-bit unicode byte signed 8 bit short signed 16-bit int signed 32 bit long signed 64 bit float 32 bit floating point double 64 bit floating point HB/C2/KERNELC++/8 Comma Expression Evaluate expr1 then expr2. Has value and type of expr2. x= (++n, n+2 ) int i,j; for (i=0, j=10; i+j<=20; ++i,++j) {cout << i << " " << j << endl; C++ : Some Expressions Platform Dependent >> RightShift Bits operator: Java: >> shift bits right, fills with sign bit >>> shift bits right, fills with zero NOT in C++!! C++: >> shift bits right, fills with 0/1, platform dependent! integer division / : Java: integer division truncates toward zero: -23/4 yields -5 C++: the result of integer division involving negative numbers is platform dependent. -23/4 yields -5, -6? C++ standard requires : a= = (a / b) b + a%b expr1, expr2 not in Java 0 10 1 11 2 12 3 13 4 14 5 15 C++ type char wchar_t int float double char int short long modifiers signed, unsigned short, long, signed, unsigned long signed char signed int signed short int signed long int sizeof(expression), sizeof(typename): gives the number of bytes used to represent values of the given type. sizeof(typename): platform dependent!! Ansi C++ requires: 1=sizeof(char) sizeof(short) sizeof(int) sizeof(long) 1 sizeof(bool) sizeof(long) sizeof(char) sizeof(wchar_t) sizeof(long) sizeof(float) sizeof(double) sizeof(long double) sizeof(n)=sizeof(signed N)= sizeof(unsigned N), N {char,short int,long int

Simple Types sizeof(typename): platform dependent!! Typical 32 bit platform type values unsigned char 0 to 255 8 bit char -128 to 127 8 bit (signed) int -32 768 to 32 767 16 bit unsigned int 65 535 16 bit (signed) short int -32 768 to 32 767 16 bit unsigned short int 65 535 16 bit (signed) long int -2147483648 to 2147483647 32 bit unsigned long int 0 to 4 294 967 295 32 bit float 6 digits of precision 32 bit double 10 digits of precision 64 bit long double 10 digits of precision 64 bit HB/C2/KERNELC++/9 The header file <limits> (or <limits.h>) defines constants defining the integral values representable on a given system: #define CHAR_BIT 8 /* number of bits in a char */ #define SCHAR_MIN (-128) /* minimum signed char value */ #define SCHAR_MAX 127 /* maximum signed char value */ #define UCHAR_MAX 255 /* maximum unsigned char value */ #define SHRT_MIN (-32767-1) /* minimum signed short value */ #define SHRT_MAX 32767 /* maximum signed short value */ #define USHRT_MAX 65535U /* maximum unsigned short value */ #define LONG_MIN (-2147483647L-1) /* minimum signed long value #define LONG_MAX 2147483647L /* maximum signed long value #define ULONG_MAX 4294967295UL /* maximum unsigned long value The range of floating point values are defined in header file <float> Declarations and initialisation void f(int n) { int n; // value of n undefined int i= 5; // i is initialised to 5 char c1, c2= B ; // c1 is uninitialised double x= 0.777, y= x+i; Constants/Read-Only Variables HB/C2/KERNELC++/10 const int c1= 5, c2=10; // constants MUST be initialised const double Cr= my_f(3.2); // don t know the value of Cr at compile time c2= 20; // illegal!! Typedef-declarations double miles; // miles a variable typedef double miles; // miles a typename, miles double miles distance= 5.2; //a variable of type double type id = expression Initialisation can involve an arbitrary expression, provided that all of the variables and functions used in the expression are defined

Enumeration Types An enumeration type declaration: enum suit { clubs, diamonds, hearts, spades; // range: 0-3 suit: a new integer type (a tag name) clubs, diamonds, hearts, spades : integer constants club=0, diamonds=1, hearts= 2, spades= 3 HB/C2/KERNELC++/11 Not in Java A Quick view of C-arrays and C-strings HB/C2/KERNELC++/12 double A [100]; // A an array of 100 elements of type double, 0-99 Java notation: double sum= 0.0; double[] A= new... for ( int i=0; i<100; ++i) {sum+= A[ i ]; Illegal C++ int a; suit S= diamonds; S= clubs; a= hearts; //legal, hearts converted to integer 2 S= 2 // illegal S= static_cast<suit>(2); //explicit cast, ok Enumerators can be initialised to arbitrary integer constants: enum Myconsts {A, B=3,C,D,E=10,F,G // range 0-15 Myconsts: a new integer type A,B,C,D,E,F,G: integer constants A=0,B=3,C=4,D=5,E=10,F=11,G=12 (a tag name) Myconsts X= F; X= C; X= static_cast<myconsts>(7); // legal, within range X= static_cast<myconsts>(20); // result undefined, outside range enum pet {cat, dog mypet= cat; //Define enumtype and variable typedef enum {cat, dog pet; //Define enumtype pet int B[5]= {10,11,12,13,14; // array definition with initialisation, 0-4 double R [ ]= {3.2, 4.5, 5.6, 7.8; // array with 4 double s, 0-3 Two Dimensional Array int M[ ][ 3 ]= {{1,2,3,{4,5,6,{7,8,9,{10,11,12; //~ int M[4][3] for(int i=0; i<4; ++i) for(int j=0; j<3; ++j){cout << M[i][j] << ", ; C-string: a null terminated array of char s char S [ ]= {' a',' b',' \0' c' ;,' d',' char T [ ]= "abcd"; cout<< S << endl; // print: abcd cout << T << endl; // print: abcd C-array's, No Index Check int A[10]; for (int i=0; i<20; ++i){a[i]= 2 i; // no index-check!!, disaster!