Page 42 - DCAP404 _Object Oriented Programming
P. 42

Unit 2: Beginning of OOP Language




          The operator, !=, is an equivalent of not equal to, and is written without any space between !  Notes
          and =.
          The general form of an if statement which includes the else clause is:
          If  (expression)  statement1  else  statement2;
          If the expression has a nonzero value (i.e., if expression is true), then statement1 will be executed.
          Otherwise (i.e., if expression is false), statement2 will be executed.


                 Example:
          #include  <stdio.h>
          int  main()  {
             int x = 0;
             if ( 1 )   // if statement #1

             // {
                if ( !x )   // if statement #2
                           printf_s(“!x\n”);
                      else      //  paired  with  if  statement  #2
                       printf_s(“x\n”);
             // }
          }
          The following, for example, program uses a nested if...else construct to check if the input character
          is uppercase or lowercase.
          #include  <iostream.h>
          int  main()

          {
                 char  inp;
                 cout  <<  “Please  enter  a  character:  “;
                 cin  >>  inp;
                 if  (inp  >=  ‘A’)
                         if  (inp  <=  ‘Z’)
                                cout  <<  end1  <<  “Uppercase”;
                         else  if  (inp  >=  ‘a’)
                         {

                                if  (inp  <=  ‘z’)
                                       cout  <<  end1  <<  “Lowercase”;
                                else
                                       cout  <<  end1  <<  “Input  character  >  z”;
                         }





                                           LOVELY PROFESSIONAL UNIVERSITY                                   35
   37   38   39   40   41   42   43   44   45   46   47