Friday 30 September 2016

Elements of programming-4




If statement.
It is a conditional statement. It conditionally executing a branch of statements .There are different types of if statement.
1.       simple if
2.       if-else
3.       if-else if-else
4.       switch statement.
Simple If statement:
A relational expression is checked. If the condition is true a block of coding will be executed.if  it returns false the block of coding will not get executed.


For example take the following c program
.
#include<stdio.h>
#include<conio.h>
void main()
{
int mark;
Clrscr();
printf(“enter your score”);
scanf(“%d”,&mark);
printf(“your score is %d”,mark);
if(mark>=90)
{
printf(“excellent”);
}
getch();
}
Program explanation:

The above program asks the score as input. It prints your  mark.if your mark>=90 then it additionally prints the output excellent.

If—else
In this case a relational expression is tested .if the result is true then a block of coding will be executed.if it returns false another block of coding will be executed.

#include<stdio.h>
#include<conio.h>
void main()
{
int mark;
Clrscr();
printf(“enter your score”);
scanf(“%d”,&mark);
printf(“your score is %d”,mark);
if(mark>=40)
{
printf(“pass”);
}
Else
{
Printf(“fail”);
}
getch();
}

Program explanation:
In the above c program mark is input. If it is greater than or equal  to 40 result is printed as false.
If the mark<40 else part will be executed  and printed as fail


                                                --------to be continued
                                                                muthu karthikeyan madurai

Thursday 29 September 2016

Elements of programming ----3



                             Muthu karthikeyan, Madurai

Operators:

 

Types of operator:


Arithmetic operator
Short hand assignment operator
Increment /decrement operator
Relational operators
Logic operator
Ternary operator

Arithmetic operator:

+, -, *, /, %
Plus, minus, multiplication, division         
First three are same as mathmetical  operations.
Fourth division operation is different. i.e if we divide a int by another int result is integer only
For example
13/5=2 is the result. Decimal portions were removed
If at least any one is float or double the result is float
For example:
13.0/5=2.6
13/5.0=2.6
13.0/5.0=2.6
o.k then what about % operator? do you think that it is percentage calculating operator?
No
It is called as modulus operator.
That is one integer is divided by another integer .it returns remainder value
Example : 13/5=3.
In c , c++   modulus  operations are performed on Integer type only. In advanced languages like java, c sharp,vb.net modulus operator is also can be used also on float and double.

Short hand Assignment operator:


a=a+5 can be also written as a+=5
a=a-5 can be also written as a-=5
It totally applied for all 5 types of arithmetic operator.
Increment/decrement operator:
++,--
a=10;
a++;
now a value will be eleven because ++ operator increases the value by one
also ++a will increment the values by one.
Next:
--
It is called as decrement operator.
a=10;
a--;
now q value be only nine. because decrement operator wil decrement by . one
and  --a  may also decrement the value by one.
But in order of execution ++a(pre increment operator )has order of process high in a mathematical expression is high. but a++ (post decrement operator).has low precedence. It also applies to decrement operator.

Relational operator:


It is used for comparing one value with other value. it returns only true or false value
<  less  than
<=less  than or equal to
> greater than
>=greater than or equal to
==equals to(note there are two = symbols
!=not equal to
Examples:
a=10;
b=5;
c=15;
d=10;
a>b   ----true
a>c-----false
a==d—true
a!=d---false
logical operators:
compare two or more relational expression. It also returns true or false value
&&,||,!
&&---(and) both side of relational expression must be true.
||-(or)at least one side of expression must be true.
!-it inverts the results (if the result is true  then the result is false and vice-versa
Examples:
(a>b)&&(a>c)
(a>b)||(a>c)
!(a>b)

Ternary operator:

Make a relational expression execution . if the expression returns true a value will be returned , if the result is false b value will be returned.

Syntax:

(a>b)?a : b;
a=10;b=5;
big=(a>b)?a:b;
First the comparison(a>b) returns true so a value(i.e 10) will be returned to big.

 contact:  91 96293 29142