Monday, 5 September 2016

break, continue, switch, goto

break statement

When we writing programming code, we often come across situations where we want to jump out of a loop instantly, without waiting to get back to the conditional test. The keyword break allows us to do this. When break is encountered inside any loop, control automatically passes to the first statement after the loop. A break is usually associated with an if.
The keyword break, breaks the control only from the while in which it is placed.
Let's understand break with c program example:

/*demonstration of break statement through c program*/
#include<stdio.h>
#include<conio.h>
void main()
{
 int i=5;
 clrscr();
 while(i>=1)
 {
  if(i==2)
  {
   printf("\nIt is equal to two!!");
   break;
  }
  printf("%d\n",i); 
  i--;
 }
 getch();
}

Output:
5
4
3
It is equal to two!!

Thus, it is clear that a break statement takes the execution control out of the loop.
In above program, if we omitted break statement then what will be output? The answer is
5
4
3
It is equal to two!!
2
1


because when i's value is 2, condition will be satisfy and executed printf statement, after that compiler goes to next statement i.e. printf("%d",i); because loop do not terminate and it is run till the i value is not less than 1.

search prime number

Q. Write a C program to find whether a number is prime or not.
Definition of prime number:A prime number is one,which is divisible only by one or itself. And its must greater than 1.
And if number is not prime, it is called composite number and vice versa.

Ans.

#include<stdio.h>
#include<stdio.h>
int main()
{
 int x,num;
 printf("Enter number : ");
 scanf("%d",&num);
 x=2;
 while(x<=num-1)
 {
   if(num%x==0)
   {
      printf("Number is not prime!!");
      break;
   }
   x++;
 }
 if(x==num)
    printf("Number is prime!!");
 getch();
 return 0;
}

    output of above program :
Enter number 7
Number is prime

Generate first n Prime number

Q. Write a C program to generate first n prime number C program.

Ans.

/*c program for generate first n prime number*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int n,num,t,div,count;
 printf("How many prime number you want to print: ");
 scanf("%d"&n);
 printf("\n%d\t",2); /*2 is first prime number*/
 count=1;
 num=3;
 while(count<n)
 {
  t=sqrt(num);
  div=2;
  while(div<=t)
  {
    if(num%div==0)
       break;
    div++;
  }
  if(div>t)
  {
     printf("%d\t",num);
     count++;
  }
  num=num+2;
 }
 getch();
 return 0;
}

/************Output************/

Output of generate first n prime number C program
Screen shot for generate first n prime number
C program

continue statement

when we write program, if we want to take control to the beginning of the loop, bypassing the statements inside the loop, which have not yet been executed, in this situation we uses continuecontinue is c reserved keyword in C. When continue is encountered inside any loop, control automatically passes to the beginning to the loop.
continue is usually associated with an if.
Let's understand continue with example:

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

Output:
 1 2
 2 1
 
Explanation: When the value of i equals that of j, the continue statement takes the control to the for loop(inner) bypassing the rest of the statements execution in the for loop(inner).

Let's understand continue statement with another example:
 
/*demonstration of continue statement*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int i;
 for(i=1; i<=10; i++)
 {
   if(i==6)
     continue;
   printf("%d ",i);
 }
 printf("\nNow we are out of for loop");
 getch();
 return 0;
}


Output:-

 1 2 3 4 5 7 8 9 10
 Now we are out of for loop

Explanation: As when the value of i will becomes 6 it will move for the next iteration by skipping the iteration i=6.

switch-case statement

In some programming situation, when there are number of choices and we want to choose only appropriate choice, in that situation C provided switch statement. Thus switch statement allows us to make a decision from the number of choices.
The switch statement also known as switch-case-default.
Since, the switch statement known for decision maker.
Syntax of switch statement:

switch(integer expression)
{
 case 1 :
   do this;
   break;
 case 2 :
   do this;
   break;
 case 3 :
   do this;
   break;
 case 4 :
   do this;
   break;
 default :
   and this;
   break
}

Example of switch-case-default statement:

/*program to demonstration of switch statement*/
 #include<stdio.h>
 #include<conio.h>
 int main()
 {

  int r=4;
  switch(r)
  {
   case 1 :
     printf("\nI am in case 1");
     break;
   case 2 :
     printf("\nI am in case 2");
     break;
   case 3 :
     printf("\nI am in case 3");
     break;
   case 4 :
     printf("\nI am in case 4");
     break;
   case 5 :
     printf("\nI am in case 5");
     break;
   default :
     printf("\nI am in default");
     break;
  }
  getch();
 return 0;
 }


Output: I am in case 4

Different types of switch statement

(a) In switch statement case arrangement may beascending orderdescending orderscrambled order. So the following program should be valid and run:

 #include<stdio.h>
 #include<conio.h>
 int main()
 {
  int r=44;
  switch(r)
  {
   case 210 :
     printf("\nI am in case 210");
     break;
   case 10 :
     printf("\nI am in case 10");
     break;
   case 1 :
     printf("\nI am in case 1");
     break;
   case 44 :
     printf("\nI am in case 44");
     break;
   default :
     printf("\nI am in defalut");
     break;
  }
  getch();
 return 0;
 }
Output: I am in case 44

(b) C also allowed to use char values in case andswitch statement as:

 #include<stdio.h>
 #include<conio.h>
 int main()
 {
  char a='n';
  switch(a)
  {
    case 'm' :
      printf("\nI am in case m"); 
    case 'n' :
      printf("\nI am in case n");
    case 'o' :
      printf("\nI am in case o");
    case 'p' :
      printf("\nI am in case p");
    default :
      printf("\nI am in default");
  }
  getch();
  return 0;
 }

Output: I am in case n

Note: When above program run, 'm', 'n', 'o', 'p' are actually replaced by subsequent ASCII(American Standard Code for Information Interchange) code.

(c) C language allows us to check the value of any expression in a switch statement like as:
   switch(r+c*i)
   switch(10+20%2*r) 
   switch(r+10*c)
   switch(r>10 || c<20) 
Above all switch statement are valid.
And expression can also be used in cases provided they are constant expression.
Thus case 10+15 is correct,
and however case r+c is incorrect.

(d) The switch statement is very useful while writing menu driven program.

(e) The break statement when used in a switch takes the control outside the switch. However, use ofcontinue will not take the control to the beginning of switch.

(f) The case keyword is followed by an integer or a character constant.

goto statement

The goto statement is used to alter the normal sequence of program instructions by transferring the control to some other portion of the program.
The syntax is as follows:
goto label;
Here, label is an identifier that is used to label the statement to which control will be transferred. The targeted statement must be preceded by the unique label followed by colon.
label: statement;
It is recommended that as possible as skip the gotostatement because when we use goto then we can never be sure how we got to a certain point in our code. They obscure the flow of control.

Note:- goto can never be used to jump into the loop from outside and it should be preferably used for forward jump.

Let us consider a program to illustrate goto and labelstatement:
/*program to demonstration of goto statement*/
#include<stdio.h>
#include<conio.h>
void main()
{
 int n;
 clrscr();
 printf("Enter one digit number: ");
 scanf("%d",&n);
 if(n<=6)
   goto mylabel;
 else
 {
   printf("Now control in main funcion.");
   exit();
 }
 mylabel:
   printf("Now control in mylabel.");
}

Output:-

Enter one digit number:9
Now control in main function.
Enter one digit number:4
Now control in mylabel. 

Flowchart for switch-case

How to write switch case flowchart?

switch case statement:

switch( choice )
{
  case 1:
      statement 1;
      break;

  case 2:
      statement 2;
      break;

  case 3:
      statement 3;
      break;

  case n:
      statement n;
}

(in above module n = number of cases.)


switch case flowchart:



switch case statement flowchart in C
Figure: flowchart of switch case statement in C


Flowchart for prime number

Q. Draw the flowchart diagram for check a number is prime number or not.

Ans.

Flowchart for check a number is prime or not as following:

flowchart of check a number is prime number or not
Figure: Flowchart for check given number is
prime number or not

Factorial C program,Algorithm,Flowchart

Q. Write a C program to find the factorial value of a number. Also write the algorithm and draw flowchart.

Ans.

/*c program to find out factorial value of a number*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int n,i,fact=1;
 printf("Enter any number : ");
 scanf("%d"&n);
 for(i=1; i<=n; i++)
    fact = fact * i;
 printf("Factorial value of %d = %d",n,fact);
 return 0;
}

The output of above program would be:
Output of calculate factorial value  of a number C program
Screen shot for calculate factorial value
of a number C program



Algorithm for calculate factorial value of a number:

[algorithm to calculate the factorial of a number]
step 1. Start
step 2. Read the number n
step 3. [Initialize]
        i=1, fact=1
step 4. Repeat step 4 through 6 until i=n
step 5. fact=fact*i
step 6. i=i+1
step 7. Print fact
step 8. Stop
[process finish of calculate the factorial value of a number]

Flowchart for calculate factorial value of a number:

flowchart for calculate factorial value of a number
Figure: Flowchart for calculate factorial value of
a number C program

No comments:

Post a Comment