Switch

Switch statement is used for checking conditions , which are far more in no. than those which can be checked in 'If' . These conditions are put in the various 'Cases' (or the default\error condition in 'Default'), which can check for integers , characters and strings .




Syntax for 'Switch'


switch(character) 

{

 case (character1) : (programming block)

 case (character2) : (programming block)

}

  




C Example


#include<stdio.h>

int main() {

	int i=2;

	switch(i) {

		case 1:

		printf("hello");

		break;

		case 2:

		printf("welcome");

		break;

		default:

		printf("bye");

		break;

	}

	printf("\nuser");

	return 0;

}


Output:

welcome

user




C Example


#include<stdio.h>

int main() {

	char i='a';

	switch(i) {

		case 'b':

		printf("hello");

		break;

		case 'a':

		printf("welcome");

		break;

		default:

		printf("bye");

		break;

	}

	printf("\nuser");

	return 0;

}


Quantitative Aptitude
Reasoning
Programming
Interview