Showing posts with label fundamentals. Show all posts
Showing posts with label fundamentals. Show all posts

Friday, September 4, 2020

 

Elements of programming-6




If-else if ladder.

Checking more than one else if condition is known as if-else if ladder. First a condition is checked if it is true then a block of statements are executed. If it is false an another condition is checked with the help else if construct. If it also false then another condition is checked. Checking more than one else if construct is known as else if ladder.


Example:

#include <stdio.h>

int main()
{
    int dayno;
    printf("enter day no:");
    scanf("%d",&dayno);
    if(dayno==1)
    {
        printf("sunday");
    }
   else  if(dayno==2)
    {
        printf("monday");
    }
   else if(dayno==3)
    {
        printf("tuesday");
    }
   else if(dayno==4)
    {
        printf("wednesday");
    }
    else if(dayno==5)
    {
        printf("thursday");
    }
   else if(dayno==6)
    {
        printf("friday");
    }
   else if(dayno==7)
    {
        printf("saturday");
    }
    else
    {
        printf("invalid day number");
    }


    return 0;
}

                                       In the above program day number is get as input . the block statements which will be executed is depending upon the variable dayno we entered. If all the condition is false then  else part will be executed.

                                       Thanking you,
                                                                    Muthu karthikeyan,Madurai.
contact:919629329142
email:muthu.vaelai@gmail.com

Sunday, August 30, 2020

 

Elements of programming -5.




Nested if statement.

First a condition is checked if it is true then another condition checked. If it is true another condition is checked. If it also true then a block of statement is executed. If the first condition is true, then else part will be executed. we can also check another condition in else part.

Example.

We are going to find big number among three numbers. The program for it.
#include <stdio.h> 
int main()
{
     int a,b,c;
     printf("enter a:");
     scanf("%d",&a);
     printf("enter b");
     scanf("%d",&b);
     printf("enter c");
     scanf("%d",&c);
     if(a>b)
     {
         if(a>c)
         {
             printf("a is big");

         }
         else
         {
             printf("c is big");

         }
     }
     else
     {
         if(b>c)
         {
             printf("b is big");
         }
         else
         {
             printf("c is big");
         }
     }
    return 0;
}
In the above program has been written in c language. It uses the method of nested if statement.  First a is greater than b is executed if it is true then it check for the second condition whether the second condition is true or not false. If it also true a is declared as big number. If the second condition is false then c is declared as big number .if the first condition is false then it comes to else part.  It also checks another condition. If it is true then b is declared as big number. If it is false then c is declared as big number.
                    Will continue
                                                                                Muthu karthikeyan,Madurai.
                                                     contact:919629329142
                                                          Email:muthu.vaelai@gmail.com. 

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

Saturday, August 29, 2020

Elements of programming ----3

 




            

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

Muthu karthikeyan, Madurai

Elements of programming-2

 







constants:
constants are also memory locations like variables but their values that can not be changed through out the program. only initial assignment is considered. most of the programming languages use const keyword to define constant.
Example:
const float PI=3.14;
some language also # define keyword to define a constant
Datatypes:
when we declare a variable at first time we should tell the compiler what type of data we are going to store.
it may be integer, floating point numbers or char. advanced language also have string type. following are thre primarydata types:
int
float
double
char:


int:
int means integer which indirectly means whole numbers. usually c,c++ int data type takes 2 bytes of memory. but modern c,c++ and alsoc#,java ,vb.net take 4 bytes of memory. modified long int data type take 8 bytes of memory in all modern languages.
float & double:
both are used to store floating point numbers.float data types takes only 4 bytes. double data type takes 8 bytes memory.if you want accuracy you are recommended to choose double data type.
char:
it takes 2 bytes of memory in java & c# because it follows unicode method to store data. it can store only one character .some basic languages uses array of characters to store more than one character.char data type is actually treated internally as char data type.
String:
modern languages like java and c# uses string data type to store more than one character. while char type data are put in to single quotes, string datas are put into double quotes.


contact :919629329142






Friday, August 28, 2020

Elements of programming-1:

 





This series is going to contain fundamentals of programming. it applies to all the major programming languages.
Variables

Variables are name of memory location. If we want to store any kind of data in memory it has to be  named.  Before using a variable it has to declared that what kind of data we are going store in that variable(int, float or char). In c language any variable declarations must be the first statement in main function.  Advanced languages(c++, java,c#) allow variables to be declared at any line .

Compiler:

In a program what we write are called as high level languages. It  can not be understandable by a computer. It has to be translated in to machine language(0 or 1). That job is done by compiler. So compilers are the software which translate high level language into machine level language.

Interpreters

Interpreters do the same work as compiler. The primary difference between compiler and interpreter is interpreters compile the program line by line where as compiler compiles the whole program at once.

Comments:
Comments are used for documentation purposes. It will be ignored by the compiler.
Single line  comments starts with //.
Multi line comment start with /* and ends with */.
Examples:
//this is a single line comment.
/*this is a
Multiline comments*/

IDENTIFIER

An identifier is the name given to the programming elements such as variable,method,class and objects.
There are some naming rules:
It can contain alphabets, digits and underscore(_)
Special characters other than underscore are not allowed.
Spaces not allowed in a identifier.
It should not be a keyword.

Key words:

Every language contain keywords which has a special meaning to the compiler. We can not use it for another purposes.for example we can not use it for naming an identifier
Example for keywords:
Int, do, while,for,if
     To be continued(MUTHUKARTHIKEYAN,MADURAI)