2. Second Step for Learning C++ Programming • Data Type • Char • Float • Double • #define • Comments
[Practice 01 Hello World]
[Explain 01 Hello World]
[Practice 02 Basic C++ Programming]
[Explain 02 Basic C++ Programming] a Preprocessor directive function header start of function body make definitions visible message insert a new line more output terminate main() end of function body
[Practice 03 comments]
[Practice 04 #define]
[Explain 04 #define]
Const You can add the const modifier to the declaration of a variable to tell the compiler that the value cannot be changed: const double factor = 5.0/9.0; const double offset = 32.0; celcius = (fahr - offset)*factor; 09/2014
What if you try to change a const? The compiler will complain if your code tries to modify a const variable: const foo = 100; … foo = 21; Error: l-value specifies const object 09/2014
Const Why use const? 1. const tells the compiler that a variable should never be changed. 2. Typesafe (partial) replacement for #define in C++ 3. May be used for increasing code quality 09/2014
Mathematical expressions have numeric values when evaluated. Math Expressions Mathematical expressions have numeric values when evaluated. Some examples: 1+2 (fahr - 32)*(5/9) 1*(2*(3*(4*5))) 09/2014
[Practice 05 Math Operator Rules]
[Explain 05 Math Operator Rules]
[Practice 06 Addition of integers]
[Explain 06 Addition of integers]
a single byte, capable of holding one character Basic Data Types in C++ char a single byte, capable of holding one character int an integer, typically reflecting the natural size of integer on the host machine float single precision floating-point double double precision floating-point bool a boolean value (true or false) 09/2014
Keyword auto enum restrict unsigned break extern return void case float short volatile char for signed while const goto sizeof _bool continue if static _Complex default inline struct _Imaginary do int switch double long typedef else register union
Simple Variables Name in C++ Ο int poodle; //valid Ο int Poodle; //valid and distinct from poodle Ο int POODLE; //valid and even more distinct Χ Int terrier; //invalid – has to be int, not Int Ο int my_stars3; //valid Ο int _Mystars3; //valid – starts with underscore Χ int 4ever; //invalid because starts with a digit Χ int double; //invalid – double is a c++ keyword Ο int begin; //valid – begin is a Pascal keyword Ο int __fools; //valid – starts with two underscores Ο int the_very_best_variable_i_can_be_version_112; //valid Χ int apple-4; //invalid – no hyphens allowed
[Practice 07 Data Type]
[Explain 07 Data Type]
ASCII CODE
[Practice 08 char]
[Explain 08 char]
[Practice 09 float]
[Explain 09 float]
Repetition Control Structures 3. Third step for Learning C++ Programming ㅎㅎ logical operator if if else switch while do while for Repetition Control Structures
&& Operator (logical AND opterator) && is a boolean operator, so the value of an expression is true or false. ( cond1 && cond2 ) is true only if both cond1 and cond2 are true. 09/2014
|| Operator (logical OR operator) || is a boolean operator, so the value of an expression is true or false. ( cond1 || cond2 ) is true if either of cond1 or cond2 is true. 09/2014
The ! operator (logical NOT operator) The ! operator is a unary boolean operator unary means it has only 1 operand. ! negates it's operand. ! means "not". (! condition) is true only when condition is false 09/2014
[ Practice 01 logical operator ]
[ Explain 01 logical operator ]
if (condition ) action; if structure The if control structure allows us to state that an action (sequence of statements) should happen only when some condition is true: if (condition ) action; 09/2014
[ Practice 02 if ]
[ Explain 02 if ]
if else structure The if else control structure allows you to specify an alternative action: if ( condition ) action if true else action if false 09/2014
[ Practice 03 if else ]
[ Explain 03 if else ]
switch statement Syntax switch (expression) { case const-expr : statements; break; case const-expr : statements; break; … default statements; break; } 09/2014
[ Practice 04 switch ]
[ Explain 04 switch ] 0 ≦ code ≦ 4
while Control Structure The while control structure supports repetition - the same statement (or compound statement) is repeated until the condition is false. while (condition) do something; the inside is called the "body of the loop" 09/2014
[ Practice 05 while ]
[ Explain 05 while ]
do while The do while control structure also provides repetition, this time the condition is at the bottom of the loop. the body is always executed at least once do somestuff ; while ( condition ); 09/2014
[ Practice 06 do while ]
[ Explain 06 do while ]
You can write any for loop as a while (and any while as a for). for loops The for control structure is often used for loops that involve counting. You can write any for loop as a while (and any while as a for). for (initialization; condition; update) do something; 09/2014
[ Practice 01]