Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types."— Presentation transcript:

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

2 CS-1030 Dr. 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

3 CS-1030 Dr. 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!

4 CS-1030 Dr. Mark L. Hornick 4 C++ selection statements The C++ if() statement Identical to Java! The C++ switch() statement Identical to Java!

5 CS-1030 Dr. 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++ )…

6 CS-1030 Dr. 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

7 CS-1030 Dr. 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

8 CS-1030 Dr. Mark L. Hornick 8 Java Numeric data types

9 CS-1030 Dr. Mark L. Hornick 9

10 CS-1030 Dr. 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 x and 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

11 CS-1030 Dr. 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

12 CS-1030 Dr. 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

13 CS-1030 Dr. 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.

14 CS-1030 Dr. Mark L. Hornick 14 Arithmetic Operators Same in Java and C++

15 CS-1030 Dr. Mark L. Hornick 15 Precedence rules for operators Same in C++ as in Java

16 CS-1030 Dr. Mark L. Hornick 16 Arithmetic Promotions Same in both languages

17 CS-1030 Dr. 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;


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

Similar presentations


Ads by Google