Overview of C++ language
Lecture’s outline

 What is C++?
 Identifiers.
 Constant
 Semicolons & Blocks in C++
 Data type
 Comments
 Operator
 Declaration of variables
 Giving value to variable
 C++ statements

                prepared by Taif.A.S.G
What is C++?
• Developed by Bjarne Stroustrup (in 1979 at Bell Labs)
• C++ is regarded as a middle-level language, as it comprises a
  combination of both high-level and low-level language features.
• C++ is a statically typed, compiled, general purpose, case-
  sensitive, free-form programming language that supports
  procedural, object-oriented, and generic programming.
• C++ is a superset of C that enhancement to the C language and
  originally named C with Classes but later it was renamed C++.

                         prepared by Taif.A.S.G
identifiers
They are used for naming classes
function, module, variables, object in a program.
They follow the following rules:
1) They can have alphabets , digits, and the underscore(_).
2) They must not begin with a digit.
3) Uppercase and lowercase are distinct.
4) They can be of any length.
5) It should not be a keyword.
6) White space is not allowed.

                      prepared by Taif.A.S.G
Constant
Constant is one whose value cannot be changed after it has
been initialized-any attempt to assign to field will produce a
compile-error .So, we can declare the constant by using
const field and use all uppercase letter.
Syntax of declare the constant is:
  const type name=value

Example:

const int STRENGTH = 100;
const float PI = 3.14;
                         prepared by Taif.A.S.G
Semicolons & Blocks in C++:
• In C++, the semicolon is a statement terminator.
  That is, each individual statement must be ended
  with a semicolon. It indicates the end of one logical
  entity.
• For example: following are two different
  statements:
  x = y;
  y = y+1;


                       prepared by Taif.A.S.G             6
Data type




prepared by Taif.A.S.G
Comments
C++ can use both the single line comments and multi-line
comments .
single line comments begin with // and end at the end of line.
For longer comments we can create multi-line comments by
starting with /* and ending with */




                        prepared by Taif.A.S.G
Operator
* / % + - are the mathematical operators
* / % have a higher precedence than + or –           mathematical
                                                     operators
 ++ Increment operator         –– Decrement operator
 ==    Equal (careful)
 !=            Not equal
 >=    Greater than or equal          Relational
 <=    Less than or equal             operators
 >     Greater than
 <     Less than
 &&    logical (sequential) and
                                       Logical
 ||    logical (sequential) or         operators
 =     assignment
                      prepared by Taif.A.S.G
Declaration of variables

The general form of declaration of variables:

           Type variable1, variable2,...., variableN;
Example:

int count;
float x,y;
double pi;
char c1,c2,c3;
byte b;
                         prepared by Taif.A.S.G
Giving value to variable

The general form of giving value to variables:

                  variableName= value;
Example:

finalValue=100;
x=y=z=0;
Yes = ‘x’;



                         prepared by Taif.A.S.G
Simple programs
 Write C++ Program to find Sum and Average of two
  numbers?

Write C++ Program to find area of a circle?




                    prepared by Taif.A.S.G
C++ statement
                                Control
                              statement
     Selection                     iteration                   jump
     statement                    statement                  statement



If    if-else   switch      for      while   do      break   continue   return


                            prepared by Taif.A.S.G
Decision making
• C++ supports the following statements Known
  as control or decision making statements.
1. if statement.
2. switch statement.
3. Conditional operator ? : statement.




                  prepared by Taif.A.S.G    14
Decision making with if
                statement
The general form of simple if statement is :
                if (<conditional expression>)
                <statement action>

Example:

       if (a > b)
       cout<<"a > b";




                            prepared by Taif.A.S.G   15
Decision making with if
            statement cont..
The general form of simple if else statement is :
                if (<conditional expression>)
                <statement action>
                else
Example:        <statement action>

       if (a > b)
       cout<<" a > b";
       else
       cout<<"a<b";

                            prepared by Taif.A.S.G   16
Decision making with if
            statement cont..
The general form of Multiple if else statement is :
                  if (<conditional expression>)
                  <statement action>
                  else if (<conditional expression>)
                  <statement action>
                  else
Example:          <statement action>
if (a > b)
cout<<" a > b";
 else if (a< b)
 cout<<"b > a";
else
 cout<<" a=b";
                              prepared by Taif.A.S.G   17
Decision making with switch
          statement
• The if statement allows you to select one of
  two sections of code to execute based on a
  boolean value (only two possible values). The
  switch statement allows you to choose from
  many statements.




                    prepared by Taif.A.S.G        18
Decision making with switch
         statement cont..
switch (expr) {
case c1:
statements // do these if expr == c1
 break;
case c2:
statements // do these if expr == c2
 break;
 case c3:
case c4: // Cases can simply fall thru.
statements // do these if expr == any of c's
 break; . . .
default:      // OPTIONAL
statements // do these if expr != any above
 }

                                   prepared by Taif.A.S.G   19
The ? : Operator:
• can be used to replace if...else statements. It has
  the following general form:
  Exp1 ? Exp2 : Exp3;
  Where Exp1, Exp2, and Exp3 are expressions.
• The value of a ? expression is determined like
  this: Exp1 is condition evaluated. If it is true, then
  Exp2 is evaluated and becomes the value of the
  entire ? expression. If Exp1 is false, then Exp3 is
  evaluated and its value becomes the value of the
  expression.
                       prepared by Taif.A.S.G         20
Simple programs
 Write C++ Program to find Number is Positive or
  Negative?

Write C++ Program to find the Grade of student ?

 Write C++ Program to check the day of week by using
  SWITCH-CASE?




                   prepared by Taif.A.S.G
Looping
The C++ language provides three constructs for
  performing loop operations, there are:
   1. The for statement.
   2. The while statement.
   3. The do statement.




                   prepared by Taif.A.S.G        22
Looping cont..
The general form of for statement:

for (initialization; test condition; increment)
 {
    Body of loop
 }


Example:
for( x=0; x<=10; x++)
{
cout<<x;
}                          prepared by Taif.A.S.G   23
Looping cont..
The general form of while statement:
       initialization;
      while(test condition)
     {
    Body of loop
    }

Example:
x=0;
while(x<=10)
{
cout<<x;
x++; }                     prepared by Taif.A.S.G   24
Looping cont..
The general form of do statement:
       initialization;
      do
     {
    Body of loop
    }
   while(test condition);

Example:
x=0;
do
{
cout<<x;
x++; }
while(x<=10) ;            prepared by Taif.A.S.G   25
Int num2        Int num1
                                                             0               0
  Nested Looping                                                             1
for(num2 = 0; num2 <= 3; num2++)                                             2
{                                                                       3 end of loop
   for(num1 = 0; num1 <= 2; num1++)                          1               0
   {                                                                         1
       cout<<num2 << " " <<num1;
                                                                             2
   }
}                                                                       3 end of loop
                                                             2               0
                                                                             1
                                                                             2
                                                                        3 end of loop
                                                             3               0
                                                                             1
                                                                             2
                                                                        3 end of loop
                               prepared by Taif.A.S.G                               26
                                                        4 End of loop
Functions
• A complex problem is often easier to solve by
  dividing it into several smaller
  parts(Modules), each of which can be solved by
  itself. This is called structured programming.
• In C++ Modules Known as Functions & Classes
• main() then uses these functions to solve the
  original problem.


                   prepared by Taif.A.S.G     27
Functions (cont..)
• C++ allows the use of both internal (user-
  defined) and external functions.

• External functions (e.g., abs, ceil, rand,
  sqrt, etc.) are usually grouped into specialized
  libraries (e.g., iostream, stdlib, math,
  etc.)


                     prepared by Taif.A.S.G          28
Function prototype
• The function prototype declares the input and output
  parameters of the function.

• The function prototype has the following syntax:
     <type> <function name>(<type list>);

• Example: A function that returns the absolute value of
  an integer is: int absolute(int);
• If a function definition is placed in front of main(),
  there is no need to include its function prototype.


                       prepared by Taif.A.S.G            29
Function Definition
• A function definition has the following syntax:
  <type> <function name>(<parameter list>){
       <local declarations>
       <sequence of statements>
  }

• For example: Definition of a function that computes the absolute
  value of an integer:

int absolute(int x){
     if (x >= 0) return x;
     else        return -x;
}

• The function definition can be placed anywhere in the program.

                            prepared by Taif.A.S.G                   30
Function call

• A function call has the following syntax:
  <function name>(<argument list>)


  Example: int distance = absolute(-5);




                      prepared by Taif.A.S.G   31

Overview of c++ language

  • 1.
  • 2.
    Lecture’s outline Whatis C++? Identifiers. Constant Semicolons & Blocks in C++ Data type Comments Operator Declaration of variables Giving value to variable C++ statements prepared by Taif.A.S.G
  • 3.
    What is C++? •Developed by Bjarne Stroustrup (in 1979 at Bell Labs) • C++ is regarded as a middle-level language, as it comprises a combination of both high-level and low-level language features. • C++ is a statically typed, compiled, general purpose, case- sensitive, free-form programming language that supports procedural, object-oriented, and generic programming. • C++ is a superset of C that enhancement to the C language and originally named C with Classes but later it was renamed C++. prepared by Taif.A.S.G
  • 4.
    identifiers They are usedfor naming classes function, module, variables, object in a program. They follow the following rules: 1) They can have alphabets , digits, and the underscore(_). 2) They must not begin with a digit. 3) Uppercase and lowercase are distinct. 4) They can be of any length. 5) It should not be a keyword. 6) White space is not allowed. prepared by Taif.A.S.G
  • 5.
    Constant Constant is onewhose value cannot be changed after it has been initialized-any attempt to assign to field will produce a compile-error .So, we can declare the constant by using const field and use all uppercase letter. Syntax of declare the constant is: const type name=value Example: const int STRENGTH = 100; const float PI = 3.14; prepared by Taif.A.S.G
  • 6.
    Semicolons & Blocksin C++: • In C++, the semicolon is a statement terminator. That is, each individual statement must be ended with a semicolon. It indicates the end of one logical entity. • For example: following are two different statements: x = y; y = y+1; prepared by Taif.A.S.G 6
  • 7.
  • 8.
    Comments C++ can useboth the single line comments and multi-line comments . single line comments begin with // and end at the end of line. For longer comments we can create multi-line comments by starting with /* and ending with */ prepared by Taif.A.S.G
  • 9.
    Operator * / %+ - are the mathematical operators * / % have a higher precedence than + or – mathematical operators ++ Increment operator –– Decrement operator == Equal (careful) != Not equal >= Greater than or equal Relational <= Less than or equal operators > Greater than < Less than && logical (sequential) and Logical || logical (sequential) or operators = assignment prepared by Taif.A.S.G
  • 10.
    Declaration of variables Thegeneral form of declaration of variables: Type variable1, variable2,...., variableN; Example: int count; float x,y; double pi; char c1,c2,c3; byte b; prepared by Taif.A.S.G
  • 11.
    Giving value tovariable The general form of giving value to variables: variableName= value; Example: finalValue=100; x=y=z=0; Yes = ‘x’; prepared by Taif.A.S.G
  • 12.
    Simple programs  WriteC++ Program to find Sum and Average of two numbers? Write C++ Program to find area of a circle? prepared by Taif.A.S.G
  • 13.
    C++ statement Control statement Selection iteration jump statement statement statement If if-else switch for while do break continue return prepared by Taif.A.S.G
  • 14.
    Decision making • C++supports the following statements Known as control or decision making statements. 1. if statement. 2. switch statement. 3. Conditional operator ? : statement. prepared by Taif.A.S.G 14
  • 15.
    Decision making withif statement The general form of simple if statement is : if (<conditional expression>) <statement action> Example: if (a > b) cout<<"a > b"; prepared by Taif.A.S.G 15
  • 16.
    Decision making withif statement cont.. The general form of simple if else statement is : if (<conditional expression>) <statement action> else Example: <statement action> if (a > b) cout<<" a > b"; else cout<<"a<b"; prepared by Taif.A.S.G 16
  • 17.
    Decision making withif statement cont.. The general form of Multiple if else statement is : if (<conditional expression>) <statement action> else if (<conditional expression>) <statement action> else Example: <statement action> if (a > b) cout<<" a > b"; else if (a< b) cout<<"b > a"; else cout<<" a=b"; prepared by Taif.A.S.G 17
  • 18.
    Decision making withswitch statement • The if statement allows you to select one of two sections of code to execute based on a boolean value (only two possible values). The switch statement allows you to choose from many statements. prepared by Taif.A.S.G 18
  • 19.
    Decision making withswitch statement cont.. switch (expr) { case c1: statements // do these if expr == c1 break; case c2: statements // do these if expr == c2 break; case c3: case c4: // Cases can simply fall thru. statements // do these if expr == any of c's break; . . . default: // OPTIONAL statements // do these if expr != any above } prepared by Taif.A.S.G 19
  • 20.
    The ? :Operator: • can be used to replace if...else statements. It has the following general form: Exp1 ? Exp2 : Exp3; Where Exp1, Exp2, and Exp3 are expressions. • The value of a ? expression is determined like this: Exp1 is condition evaluated. If it is true, then Exp2 is evaluated and becomes the value of the entire ? expression. If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the expression. prepared by Taif.A.S.G 20
  • 21.
    Simple programs  WriteC++ Program to find Number is Positive or Negative? Write C++ Program to find the Grade of student ?  Write C++ Program to check the day of week by using SWITCH-CASE? prepared by Taif.A.S.G
  • 22.
    Looping The C++ languageprovides three constructs for performing loop operations, there are: 1. The for statement. 2. The while statement. 3. The do statement. prepared by Taif.A.S.G 22
  • 23.
    Looping cont.. The generalform of for statement: for (initialization; test condition; increment) { Body of loop } Example: for( x=0; x<=10; x++) { cout<<x; } prepared by Taif.A.S.G 23
  • 24.
    Looping cont.. The generalform of while statement: initialization; while(test condition) { Body of loop } Example: x=0; while(x<=10) { cout<<x; x++; } prepared by Taif.A.S.G 24
  • 25.
    Looping cont.. The generalform of do statement: initialization; do { Body of loop } while(test condition); Example: x=0; do { cout<<x; x++; } while(x<=10) ; prepared by Taif.A.S.G 25
  • 26.
    Int num2 Int num1 0 0 Nested Looping 1 for(num2 = 0; num2 <= 3; num2++) 2 { 3 end of loop for(num1 = 0; num1 <= 2; num1++) 1 0 { 1 cout<<num2 << " " <<num1; 2 } } 3 end of loop 2 0 1 2 3 end of loop 3 0 1 2 3 end of loop prepared by Taif.A.S.G 26 4 End of loop
  • 27.
    Functions • A complexproblem is often easier to solve by dividing it into several smaller parts(Modules), each of which can be solved by itself. This is called structured programming. • In C++ Modules Known as Functions & Classes • main() then uses these functions to solve the original problem. prepared by Taif.A.S.G 27
  • 28.
    Functions (cont..) • C++allows the use of both internal (user- defined) and external functions. • External functions (e.g., abs, ceil, rand, sqrt, etc.) are usually grouped into specialized libraries (e.g., iostream, stdlib, math, etc.) prepared by Taif.A.S.G 28
  • 29.
    Function prototype • Thefunction prototype declares the input and output parameters of the function. • The function prototype has the following syntax: <type> <function name>(<type list>); • Example: A function that returns the absolute value of an integer is: int absolute(int); • If a function definition is placed in front of main(), there is no need to include its function prototype. prepared by Taif.A.S.G 29
  • 30.
    Function Definition • Afunction definition has the following syntax: <type> <function name>(<parameter list>){ <local declarations> <sequence of statements> } • For example: Definition of a function that computes the absolute value of an integer: int absolute(int x){ if (x >= 0) return x; else return -x; } • The function definition can be placed anywhere in the program. prepared by Taif.A.S.G 30
  • 31.
    Function call • Afunction call has the following syntax: <function name>(<argument list>) Example: int distance = absolute(-5); prepared by Taif.A.S.G 31