<< 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:

Questions & Answers

if three forces F1.f2 .f3 act at a point on a Cartesian plane in the daigram .....so if the question says write down the x and y components ..... I really don't understand
Syamthanda Reply
hey , can you please explain oxidation reaction & redox ?
Boitumelo Reply
hey , can you please explain oxidation reaction and redox ?
Boitumelo
for grade 12 or grade 11?
Sibulele
the value of V1 and V2
Tumelo Reply
advantages of electrons in a circuit
Rethabile Reply
we're do you find electromagnetism past papers
Ntombifuthi
what a normal force
Tholulwazi Reply
it is the force or component of the force that the surface exert on an object incontact with it and which acts perpendicular to the surface
Sihle
what is physics?
Petrus Reply
what is the half reaction of Potassium and chlorine
Anna Reply
how to calculate coefficient of static friction
Lisa Reply
how to calculate static friction
Lisa
How to calculate a current
Tumelo
how to calculate the magnitude of horizontal component of the applied force
Mogano
How to calculate force
Monambi
a structure of a thermocouple used to measure inner temperature
Anna Reply
a fixed gas of a mass is held at standard pressure temperature of 15 degrees Celsius .Calculate the temperature of the gas in Celsius if the pressure is changed to 2×10 to the power 4
Amahle Reply
How is energy being used in bonding?
Raymond Reply
what is acceleration
Syamthanda Reply
a rate of change in velocity of an object whith respect to time
Khuthadzo
how can we find the moment of torque of a circular object
Kidist
Acceleration is a rate of change in velocity.
Justice
t =r×f
Khuthadzo
how to calculate tension by substitution
Precious Reply
hi
Shongi
hi
Leago
use fnet method. how many obects are being calculated ?
Khuthadzo
khuthadzo hii
Hulisani
how to calculate acceleration and tension force
Lungile Reply
you use Fnet equals ma , newtoms second law formula
Masego
please help me with vectors in two dimensions
Mulaudzi Reply
how to calculate normal force
Mulaudzi
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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