<< Chapter < Page Chapter >> Page >
if(expression){ if(expression){statement }}else{ statement}

Example

#include<conio.h>#include<stdio.h>void main() {// variable declaration float a, b;float max; printf(“ Enter the values of a and b: “);scanf(“%f %f”,&a,&b); if(a<b) //Assign the greater of x and y to the variable max max = b;else max = a;printf(“\n The greater of two numbers %.0f and %.0f is %.0f “,a,b,max); getch();}
Got questions? Get instant answers now!

The switch statement

It is used to select one of a number of alternative actions depending on the value of an expression, and nearly always makes use of another of the lesser statements: the break. It looks like this.

switch (expression){ case const1: statements case const2: statements. . . . default: statements}

The flowchart of switch statement is shown below:

The expression is evaluated and its value is compared with all of the const etc. expressions, which must all evaluate to different constant values (strictly they are integral constant expressions). If any of them has the same value as the expression then the statement following the case label is selected for execution. If the default is present, it will be selected when there is no matching value found. If there is no default and no matching value, the entire switch statement will do nothing and execution will continue at the next statement.

OK=1; switch (OP){ case ‘+’:z=x+y; break;case ‘-’: z=x-y;break; case ‘*’:z=x*y; break;case ’/’: if (y!=0 )z=x/y; else OK=0;default : OK=0;}
Got questions? Get instant answers now!
  • The switch requires an integer-compatible value. This value may be a constant, variable, function call, or expression. The switch statement does not work with floating – point data types.
  • The value after each case label must be a constant.
  • C++ does not support case label with ranges of values. Instead, each value must appear in a separate case label.
  • You need to use a break statement after each set of executable statements. The break statement causes program execution to resume after the end of the current switch statement. If you do not use the break statement, the program execution resumes at subsequent case labels.
  • The default clause is a catch-all clause.
  • The set of statements in each case or grouped case labels need not be enclosed in open and close braces.

The following program writes out the day of the week depending on the value of an integer variable day. It assumes that day 1 is Sunday.

#include<stdio.h>#include<conio.h>void main() {int day;printf(“Enter the value of a weekday”); scanf(“%d”,&day); switch (day){ case 1 : printf( "Sunday");break; case 2 : printf( "Monday");break; case 3 : printf( "Tuesday");break; case 4 : printf("Wednesday");break; case 5 : printf("Thursday");break; case 6 : printf("Friday");break; case 7 : printf("Saturday");break; default : printf("Not an allowable day number");break; }getch(); }

If it has already been ensured that day takes a value between 1 and 7 then the default case may be missed out. It is allowable to associate several case labels with one statement. For example if the above example is amended to write out whether day is a weekday or is part of the weekend:

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Introduction to computer science. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10776/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Introduction to computer science' conversation and receive update notifications?

Ask