Showing posts with label switch. Show all posts
Showing posts with label switch. Show all posts

Thursday, 29 May 2014

Switch-Case Statement in C

While if is good for choosing between two alternatives, it quickly becomes typical when several alternatives are needed. C's solution to this problem is the switch statement. The switch statement is C's multiple selection statement. It is used to select one of several alternative paths in program execution and works like this: A variable is successively tested against a list of integer or character constants. When a match is found, the statement sequence associated with the match is executed. The general form of the switch statement is:
Syntax:
switch(expression)
{
 case constant1: statement sequence; break;
 case constant2: statement sequence; break;
 case constant3: statement sequence; break;
 .
 .
 .
 default: statement sequence; break;
}

Expression is always a constant(or integer-valued constants). If expression is character then it will take as ascii value of that character. Each case is labelled by one, or more, constant expressions (or integer-valued constants). The default statement sequence is performed if no matches are found. The default is optional. If all matches fail and default is absent, no action takes place. When a match is found, the statement sequence associated with that case are executed until break is encountered. An example program follows:
#include
int main()
{
 int i;
 printf("Enter a number between 1 and 4");
 scanf("%d",&i);
 switch (i)
 {
  case 1:
   printf("one");
   break;
  case 2:
   printf("two");
   break;
  case 3:
   printf("three");
   break;
  case 4:
   printf("four");
   break;
  default:
   printf("unrecognized number");
} /* end of switch */ 
Output:
Enter a number between 1 and 4:
2
two
This simple program recognizes the numbers 1 to 4 and prints the name of the one you enter. The switch statement differs from if, in that switch can only test for equality, whereas the if conditional expression can be of any type. Also switch will work with only int and char types. You cannot for example, use floating-point numbers. If the statement sequence includes more than one statement they will have to be enclosed with {} to form a compound statement.

The break statement at the end of each case cause switch statement to exit. If break statement is not used, all statements below that case statement are also executed. In above example ,in case 2 break statement is not there then output will be : two three  , because in case 3 ,break statement is there then it will be break after printing the three.
Output:
Enter a number between 1 and 4:
2
twothree

Decision Making In C

Decision making is about deciding the order of execution of statements based on certain conditions or repeat a group of statements until certain specified conditions are met. C language handles decision-making by supporting the following statements,

This also we known as control statement.It is also divided into following category:
1. Selection statements.(if..else and switch-case statement)
2. Iteration or looping statements.(for loop and do-while loop)
3. Jump statements.(break, continue and goto statement)

Tuesday, 27 May 2014

Switch In Java

The switch statement in  Java is used to execute the code from multiple conditions. It provides an easy way to dispatch execution to different parts of code based on the value of expression.It is a better alternative than a large series of if-else-if statements.

The syntax of switch statement :

switch(expression)
{
      case value1:
              //statements
        break;
     case value2:
              //statements
        break;
       .
       .
       .
       .
    case valueN:
              //statements
        break;
    default:
        //default statement
}
Flow Chart of switch Statement:



Rules for switch statement in Java language

1.  The switch expression must be of  byte,short,int or char type.
2.  The case value must be of type compatible with the expression.
3. Each case value must be a unique literal (means it must be a constant , not a variable).
4. Duplicate case values are not allowed.

How switch statement works ?
The value of the expression is compared with each of the literal values in the case statement.If match is found ,the code sequence following that case statement is executed.If none of the constants matches the value of the expression then the default statement is executed.Default statement is optional.
The break statement inside the switch is used for terminate a statement sequence.When a break statement is encountered , the execution comes out of the switch block to first line after the switch block.
If we are not keeping the break statement then , if  any case matched then all the case after that case will execute.

Simple Switch statement program

package org.modi;
import java.util.Scanner;
public class Test
{
public static void main(String[] args) 
{
int i;
Scanner sc=new Scanner(System.in);
System.out.println("Enter your choice");
i=sc.nextInt();
switch(i)
{
case 0:
System.out.println("This is 0");
break;
case 1:
System.out.println("This is 1");
break;
case 2:
System.out.println("This is 2");
break;
case 3:
System.out.println("This is 3");
break;
case 4:
System.out.println("This is 4");
break;
default:
System.out.println("Default statement");
}
}
}
OUTPUT

Enter your choice                  //1st time execution
1
This is 1
Enter your choice              //2nd time execution
8
Default statement

Switch statement without break statement:
package org.modi;
import java.util.Scanner;
public class Test
{
 public static void main(String[] args) 
 {
  int i;
  Scanner sc=new Scanner(System.in);
  System.out.println("Enter your choice");
  i=sc.nextInt();
  switch(i)
  {
   case 0:
    System.out.println("This is 0");
   
   case 1:
    System.out.println("This is 1");
    
   case 2:
    System.out.println("This is 2");
    
   case 3:
    System.out.println("This is 3");
    break;
   case 4:
    System.out.println("This is 4");
    break;
   default:
    System.out.println("Default statement");
  
  }
  
 }
}

OUTPUT
Enter your choice
0
This is 0
This is 1
This is 2
This is 3


Friday, 23 May 2014

Java Control Statements

In this post we are going to discuss the control statements.
The control statements are used to control the flow of execution of the program.
Different type of control statements are:
1. Selection statements.
2. Iteration or looping statements.
3. Jump statements.

1.Selection statements:
 Java supports two selection statements if and switch. These  statements allows you to control the flow of your program's execution based upon conditions known only during run time.
For details information on control statements click on below links.

1. If
2. Switch

2. Iteration or Looping statements:
Java supports three Iteration or Looping statements for , while and do while.It executes a block of code or statements till the given condition is true. 
For details information on Iteration or Looping statements click on below links.

1. For loop
2. While loop
3. Do-While loop

3. Jump statements:
Java supports three Jump statements break , continue and return .These statements transfer control to another part of your program.
For details information on Jump statements click on below links.

1. break 
2. continue
3. return