Thursday, July 30, 2009

In turbo_c.What is a switch command?

I'm searching the switch command,because this is our assignment in Major subject IT102.I think you know this command.ok..

In turbo_c.What is a switch command?
switch is a standard C instruction.


A switch statement is like an if else statement with a couple of BIG differences.





The format is


switch (value) {


case 1:


dosomething();


break;


case 2:


do_a();


case 3:


do_b();


break;


case 4:


case 5:


do_c();


break;


default:


break;


}





This routine will execute the statements between the matching case and the next break instruction depending on the value of value.


If value is 1 then dosomething will run and nothing else.


if value is 2 then do_a will run then do_b will run then it will dop out of the switch statement.


value = 3 then do_b will run.


value = 4 or 5 then do_c will run.


If no match is found then it will run what ever is after default.





You don't have to call a function you can do


case 'A':


myval = 1;


printf("The key A was pressed\n");


break;





NOTE that you need a break instruction to prevent it falling through to the next case statement.


ALSO NOTE this funactionality changes with C# or .net and it NEVER falls through to the next case even if there is NOT a break instruction!
Reply:A command-line switch or simply switch (also known as a flag, an option, or a command-line parameter) is an indication by a user that a computer program should change its default behaviour.





For example, in the OpenVMS operating system, the command directory is used to list the files inside a directory. By default—that is, when the user simply types directory—it will list only the names of the files. By adding the switch /owner (to form the command directory/owner), the user can instruct the directory command to also display the ownership of the files.


No comments:

Post a Comment