CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.

17
CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types

Transcript of CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.

Page 1: CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.

CS-1030Dr. Mark L. Hornick

1

C++ Language

Basic control statementsand data types

Page 2: CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.

CS-1030Dr. Mark L. Hornick

2

Reuse a lot of what you already know from Java

Java was designed to “feel familiar” to an experienced C++ developer

Most of the control statements and primitive numeric types are very similar

“Improvements” over the C++ language were introduced as warranted

Page 3: CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.

CS-1030Dr. Mark L. Hornick

3

C++ iteration statements

The C++ for() statement Identical to Java!

The C++ while() statement Identical to Java!

The C++ do() statement Identical to Java!

Page 4: CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.

CS-1030Dr. Mark L. Hornick

4

C++ selection statements

The C++ if() statement Identical to Java!

The C++ switch() statement Identical to Java!

Page 5: CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.

CS-1030Dr. Mark L. Hornick

5

Wait…

I lied a little bit switch(x) can use long datatype for x

for() expressions can be more complex than those of Java Java: for(int i=0; i<5; i++ )… C++: for(int i=0, int j=0; i<5; i++,j++ )…

Page 6: CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.

CS-1030Dr. Mark L. Hornick

6

How to get an “F”

Use the C/C++ “goto” statement in your programs

It’s required usage is so rare that you may never encounter it Most cases of usage can be

restructured to use switch or nested if

Page 7: CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.

CS-1030Dr. Mark L. Hornick

7

Numeric Variables represent numeric values

int x; // a integer variable named ‘x’

int y; // and one named ‘y’Or

int x1, y1; // multiple variables created//within one statement; each variable //separated by a comma

Page 8: CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.

CS-1030Dr. Mark L. Hornick

8

Java Numeric data types

Page 9: CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.

CS-1030Dr. Mark L. Hornick

9

Page 10: CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.

CS-1030Dr. Mark L. Hornick

10

Initialization of Numeric Variables When a declaration, such as

int x, y;is made, memory locations to store data values for xand y are allocated.

But the variables are NOT initialized to any value

UNLESS, at the time the variable is declared, it is also explicitly initialized:

int count = 10, height = 34;

Exception: the debugger assigns a specific value to variables not explicitly initialized

Page 11: CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.

CS-1030Dr. Mark L. Hornick

11

Other primitive data types

bool Represents logical true or false In Java, the equivalent type is boolean

char holds a single character

In Java, chars are Unicode (multi-byte) In C++, chars are single-byte

Must use other types for wide characters in C++; i.e. wchar

Page 12: CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.

CS-1030Dr. Mark L. Hornick

12

The string datatype

Similar to the built-in (intrinsic) String type in Java

string is a class from the C++ standard library (not a built-in type)

Both are classes rather than primitive types

Page 13: CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.

CS-1030Dr. Mark L. Hornick

13

string class vs. C-style strings The string class only exists for C++, not for C (there were no

classes in C)

In C, character strings could only be handled as arrays of characters, e.g.

char someText[] = “Hello”;

This is referred to as a “C-style string”

You can also do this in Java, but why bother when you have a String class?

C++ programmers still use C-style strings at times, mainly due to interoperability requirements with legacy applications and libraries.

Page 14: CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.

CS-1030Dr. Mark L. Hornick

14

Arithmetic Operators Same in Java and C++

Page 15: CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.

CS-1030Dr. Mark L. Hornick

15

Precedence rules for operators Same in C++ as in Java

Page 16: CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.

CS-1030Dr. Mark L. Hornick

16

Arithmetic Promotions Same in both languages

Page 17: CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.

CS-1030Dr. Mark L. Hornick

17

Constant identifiers Sometimes we want to declare

identifiers whose value cannot be changed; that is, constant.

A constant is declared in a manner similar to a variable, but with the additional reserved word const. Similar to the Java final keyword

const double PI = 3.14159;

const int MONTHS_IN_YEAR = 12;