Sunday, 26 July 2015

break-statement

When break is encountered inside any loop, control automatically passes to the first statement after the loop.
 A break statement is usually associated with an if.

Example program

//program to print first 6 numbers only
#include<stdio.h>
#include<conio.h>
int main()
{
int i;
for(i=0;i<=10;i++)
{

printf("%d\n",i);
if(i==6)
break;

}
getch();
return 0;

}

output:
0
1
2
3
4
5
6

Though we wrote the program to print 10 numbers, the numbers after 6 are not printed because of the break statement. Here the moment the system encounters the break statement, control automatically passes to the next statement after the loop.

//program to determine whether a number is prime or not
#include<stdio.h>
#include<conio.h>
int main()
{
int num, i;
printf("enter a number\n");
scanf("%d",&num);
i=2;
while(i<=num-1)
{
if(num%i==0)
{
printf("not a prime number");
break;
}
i++;
}
if(i==num)
printf("prime number");
getch();
return 0;
}

output:
enter a number
9
not a prime number

continue statement

If I want to print numbers from 1 to 10 with the exception of 2 how???

continue :When continue is encountered inside any loop, control automatically passes to the beginning of the loop.
A continue is usually associated with an if.

Example
#include<stdio.h>
#include<conio.h>
int main()
{
int i;
for(i=0;i<=10;i++)
{
if(i==2)
continue;
printf("%d\n",i);
}
getch();
return 0;
}

output:
0
1
3
4
5
6
7
8
9
10


//program to print odd numbers

#include<stdio.h>
#include<conio.h>
int main()
{
int i;
for(i=0;i<=10;i++)
{
if(i==2)
continue;
printf("%d\n",i);
}
getch();
return 0;

}

output:
1
3
5
7
9







Tech 2


do-while

Confused with while and do-while?

simple thing- while executes the set of instructions in the loop only if the condition specified is true. On the other hand, do- while executes the set of instructions atleast once what ever the condition may be.

syntax:

while                                                                    do-while

while(i<=10)                                                       do
{                                                                          {
 printf("%d",i);                                                          this;
                                                                                and this;
                                                                           }while(this condition is true);
i++;
}


1.Example program
#include<stdio.h>
#include<conio.h>
int main()
{
while(4<2)
printf("this is while");
return 0;
}


output:
No output will be generated as the condition fails.


#include<stdio.h>
#include<conio.h>
int main()
{
do
   {
      printf("this is do- while");
    }while(5<2);
return 0;
}


output:
this is do-while
since it is do-while the printf statement will be executed atleast once.


Friday, 24 July 2015

Loops-for

for is the most popular looping instruction.The for allows us to specify three things about a loop in a single line:

1.Setting a loop counter to an initial value
2.Testing the loop counter to determine whether its value has reached the number of repetitions desired
3.Increasing the value of loop counter each time the program segment within the loop has been executed.

syntax for for- loop

for(initialization; test counter; increment counter)
{
do this;
do this;
}


//program to print N numbers

#include<stdio.h>
#include<conio.h>
int main()
{
int i,n;
printf("Till which number do you want to print");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
printf("%d\n",i);
}
  getch();
  return 0;
}

output:
Till which number do you want to print 4
0
1
2
3
4


//Calculation of simple interest for 3 sets of p, n, r
























Formats:

 for(i=10;         i;                    i--)
printf("%d",i);

for(i<4;                   j=5;            j=0)
printf("%d",i);


for(i=1;              i<=10 ;             printf("%d",i++))
;


Loops- while

The programs that we have discussed so far used either a sequential or a decision control instruction. In the first one, the calculations were carried out in a fixed order; while in the second one, an appropriate set of instructions were executed depending on the outcome of the condition being tested.

             These programs have a limited scope, because when executed they always perform the same series of actions, in the same way exactly once. But in real life if something is worth doing, it has to be done more than once.

Loops:
           The versatility of the computer lies in its ability to perform a set of instructions repeatedly. This involves repeating some portion of the program either a specified number of times or until a particular condition is being satisfied.This repetitive operation is done through a loop control instruction.


Syntax of while loop:

while(condition)
{


set of statements;
increment statement;


}

//simple programs using while loop

//calculation of simple interest for 3 sets of p, n and r
#include<stdio.h>
#include<conio.h>
int main()
{
int p, n, count;
float r, si;
count=1;
while(count<=3)
{
  printf("enter values of p, n and r\n");
scanf("%d%d%f",&p,&n,&r);
si=p*n*r/100;
printf("simple interest =rs %f\n",si);
count=count+1;
}
return 0;
}

output: 
enter values of p, n and r
1000 5 13.5
simple interest =rs 675.000000
enter values of p, n and r
2000 5 13.5
simple interest =rs 1350.000000
enter values of p, n and r
3500 5 3.5
simple interest =rs 612.500000


//To print numbers

#include<stdio.h>
#include<conio.h>
int main()
{
int i=0,n;
printf("how many numbers do you want to print\n");
scanf("%d",&n);
while(i<=n)
  {    
printf("%d\n",i);
i++;
  }
  getch();
return 0;
}

output:
how many numbers do you want to print
5
0
1
2
3
4
5


//To print even numbers
#include<stdio.h>
#include<conio.h>
int main()
{
int i=1,n;
printf("enter the number\n");
scanf("%d",&n);
printf("0\n");
while(i<=n)
{
if(i%2==0)
printf("%d\n",i);
i++;
}
  getch();
  return 0;
}

output:
enter the number
12
0
2
4
6
8
10
12