switch

#include<stdio.h>
#include<conio.h>

main()
{
int i=1;
switch(i)
{
case 1:
printf("hello ");
case 2:
printf("bye ");
default:
printf("see ya");
}

getch();
}

output
hello bye see ya

why so????
You forgot about "break;".
It should be something similar to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
	int i = 1;
	switch (i)
	{
	case 1:
		std::cout << "hello ";
		break;
	case 2:
		std::cout << "bye ";
		break;
	default:
		std::cout << "see ya";
		break;
	}
}
Last edited on
oh yes..i had forgot that without break, all following cases are executed..thnx
Topic archived. No new replies allowed.