Thursday, April 4, 2013

Loop in C++


Meaning

A loop may be defined as a smaller program the can be executed several times in a main program.

3 ways to do loop in C++
i.while
ii.do....while
iii.for

a)'while' loop
Format  'while':
                                while (condition)
                                {
                                                statement (s);
                                }
 explanation
With the while statement, the condition is first tested and if the condition is TRUE then the statement 
or statements within the loop will be executed and if the condition is first tested is FALSE the loop will 
not be executed at all.    The condition will be tested repeatedly until the condition is false.




b)'do...while' loop

 Format 'do....while':
                        do
                           {
                                        statement (s);
                            }          while (condition);

EXPLANATION
The do while loop checks the condition at the end of the loop.  This means that the statement or statements inside the do while loop will be executed again if the condition is TRUE.   If the condition is FALSE,  the do while loop will not be executed.



c)'for' loop

 



Format:

for (start expression ; test expression; count expression)
{
        statements;
}


EXPLANATION:
C evaluates the start expression before the loop begins.  Typically, the start expression is an assignment statement ( such as ctr=1;)  but it can be legal expression you specify.  C looks at and evaluates the start expressions only once, at the top of the loop.



Wednesday, April 3, 2013

Selection in C++

Selection is program that allow program to choose different action

In C++ Selection use are:
a)if
b)if...else
c)switch


a)format for Selection 'if':


                 if (condition)
                {                                                                                                             
                                statement(s);        
                               
                                body executes only once if test is TRUE
                }

EXPLANATION
If the parentheses or condition after the word ‘if’ is TRUE then the statement that follows after the condition 
will be executed.  If the condition is FALSE then the statement block will ignored.

Example

#include <stdio.h>
  void main()
   {             
                int subject_no;
                printf("Please enter a whole number\n");
                scanf("%d", &subject_no);
               
                if (subject_no==1)           
                {
                                printf("Right number !\n");           
                                printf("Well Done.");;     
                }

}


        b)format Selection for 'if....else':


                              if (condition)
                                  {            statements 1;   }
                                else
                                  {            statements 2;   }
   
    EXPLANATION
     If the condition expression is TRUE then the following statements after the if will be executed.   If it is FALSE, then the statements after else will be executed.


    c)format Selection for 'switch':

switch (expression)
                {
                                case (expression1) : { one or more C statements; }
                                case (expression2) : { one or more C statements; }
                                case (expression3) : { one or more C statements; }
                                .              
                                .
                                .
                                default : { one or more C statements; }
                }

 ·                     expression
Can be integer expression. a character, a constant or a variable.
·                     Sub-expression     (expression1, expression2, expression3.......)
Can be any other integer expression , character, constant or variable. The number of sub-expression is 
determined by your application. 
·                 The one or more C statements
Can be any block of C code.  If the block is only one statements long,  you don’t need the braces ,  bur 
they are recommended.
·                 The default line

    It is optional and does not have to be the last line of the switch body.

   EXAMPLE

   #include <stdio.h>

     void main()
    {
                int user_entry;   
                printf("Please enter an integer number between 1 and 3:");
                scanf("%d",&user_entry);
               
                switch (user_entry)
                {
                                case 1: printf("Number One\n");
                                                                break;
                               
                                case 2: printf("Number Two\n");
                                                                break;
                               
                                case 3: printf("Number Three\n");
                                                                break;
                               
                                case 4: printf("Number Four\n");
                                                                break;
                               
                                default: printf("Number is out of range.\n");
                                                                break;      
                    }

   }





Sunday, March 31, 2013

Contoh yang digunakan semasa membuat pengiraan dlam programming...


1. Arithmetic Operators

All the basic arithmetic operations can be carried out in C. All the operators have almost the same meaning as in other languages. Both unary and binary operations are available in C language. Unary operations operate on a singe operand, therefore the number 5 when operated by unary – will have the value –5. 

Arithmetic Operators

Operator

Meaning

+

Addition or Unary Plus


Subtraction or Unary Minus

*

Multiplication

/

Division

%

Modulus Operator (remainder)


Integer Arithmetic

When an arithmetic operation is performed on two whole numbers or integers than such an operation is called as integer arithmetic. It always gives an integer as the result. Letx = 27 and y = 5 be 2 integer numbers. Then the integer operation leads to the following results.x + y = 32
x – y = 22
x * y = 115
x % y = 2
x / y = 5 

In integer division the fractional part is truncated. 


Floating point arithmetic

When an arithmetic operation is preformed on two real numbers or fraction numbers such an operation is called floating point arithmetic. The floating point results can be truncated according to the properties requirement. The remainder operator is not applicable for floating point arithmetic operands.

Let 
x = 14.0 and y = 4.0 thenx + y = 18.0
x – y = 10.0
x * y = 56.0
x / y = 3.50 


Mixed mode arithmetic

When one of the operand is real and other is an integer and if the arithmetic operation is carried out on these 2 operands then it is called as mixed mode arithmetic. If any one operand is of real type then the result will always be real thus15/10.0 = 1.5
                   PROGRAM LIFE CYCLE



Ini adalah kitaran bagi programming, iaitu apa yang kita nak nyata kn disitu ia akan keluar di dalam sistem programming kita.

Thursday, March 14, 2013

BASIC STRUCTURE OF C++ PROGRAM

All C programs consists of the following element :
Preprocessor Directives

-Preprocessing refer to the first step in translating.  All preprocesssor directives begin with #.  Preprocessor directives are not C statements,  so they do not end in (;).

Examples:



                #include <stdio.h> 


main()
-program must have a main() function and it is a first function that will be called.
The main() function is the one to which control is passes when the program is executed.

{ }
-These are known as braces or curly bracketsThe statements  of the functions are enclosed in these curly brackets. The opening curly brackets indicates that a block of statements is about to begin. the closing curly brackets terminates the block of the statements.










Escape Sequence

An escape sequences always begins with the backslash \ and is followed by one or more special characters.

Escape Sequence
Description
\n
Newline
\t
Horizontal tab
\a
Alert. Sound the system bell
\\
Backslash. Used to print backslash character.
\”
Double quote. Used to print double quote character.





Tuesday, March 5, 2013

Opening of this blog

this blog as our assignment

The issue will be soon.
-The issue is about Programming in C++