This is a Clilstore unit. You can link all words to dictionaries.

Branch and selection statements

In language С++ branching can be organized by three methods:

operation“condition”?

branch or conditional statement

selection statement

General view of recording of conditional statement:

if (condition) operator1; else operator2;

or

if (condition) operator;

This statement operates as follows: first the expression in bracketsis calculated. If the value is true, i.e., the value is 1, statement1 is executed, otherwise statement2.

Example 4.The program raises to the square the greater of two numbers.

#include <iostream.h>

#include <conio.h>

#include <math.h>

main()

{ float x,y;

cout<<"enter х,у"; cin>>x>>y;

if (x>y) x=pow(x,2); else y=pow(y,2);

cout<<"x= "<<x<<"\n y= "<<y;

getch();

}

More than one statement can be executed in the conditional statement and statement sequence. It is enclosed in braces. In this case, the conditional statement will be written in the following form:

if (condition) { statements}; else { statements};

Example 5.The program raises to the square the greater of two numbers and extracts the root from the smallest.

#include <iostream.h>

#include <conio.h>

#include <math.h>

main()

{ float x,y;

cout<<" enter х,у "; cin>>x>>y;

if (x>y) {x=pow(x,2); y=sqrt(y);} else {y=pow(y,2); x=sqrt(x);}

cout<<"x= "<<x<<"\n y= "<<y;

getch();

}

Example 6. Will the point fall into the ring with center at the origin of coordinates, inner radius = 2 and outer radius =3?

#include <iostream.h>

#include <conio.h>

main()

{const float r1=2, r2=3;

float x,y;

cout<<"Enter coordinates of the point"; cin>>x>>y;

if (x*x+y*y>=r1*r1 &&x*x+y*y<=r2*r2) cout<<"\nwill fall"; elsecout<<"\nwill not fall";

getch();

}

Selection statement

Selection statement allows to organize multiple selection in the program. Recording format of the selection statement in C++ language:

switch (expression)

{ case constant1: statement1; break;

case constant2: statement2; break;

default: statement;

}

This statement operates as follows:

The value of expression is calculated. The obtained value is sequentially compared with constants placed after the service word case; upon first coincidence of the value the statement is executed standing after a colon, and selection statement ceases to operate. If no coincidence occurred with one of constants, then the statement is executed after the word default.

Example 8. The program by the day of the week display its name in Russian and English languages.

main()

{ int n;

cout<<”enter number from 1 to 7”;

cin>>n;

switch (n)

{case 1: cout<<”Понедельник- Monday”; break;

case 2: cout<<” Вторник- Tuesday”; break;

case 3: cout<<” Среда- Wednesday”; break;

case 4: cout<<” Четверг- Thursday”; break;

case 5: cout<<” Пятница- Friday”; break;

case 6: cout<<” Суббота- Saturday”; break;

case 7: cout<<” Воскресенье- Sunday”; break;

default: cout<<” Error”;}

getch();

}

Example 9. The program displays names of months according to the entered number of the season from 1 to 4.

main()

{ int n;

cout<<”Enter number from 1 to 4”;

cin>>n;

switch (n)

{case 1: cout<<”December, January, February”; break;

case 2: cout<<”March, April, May”; break;

case 3: cout<<”June, July, August”; break;

case 4: cout<<”September, October, November”; break;

default: cout<<” Error”;}

getch();

}

Example 10. Draw up a program to select one of the three roads, like in a fairy tale: "Go to the right - lose the horse, go to the left – lose a life, go straight – find a friend" (enter the symbol: right - "r" to the left - "l" right - " c ").

#include <iostream.h>

#include <conio.h>

main()

{ charx;

cout<<"Select one of the three roads: r,l,c";

cin>>x;

switch (x)

{

case 'r': cout<<"lose the horse ";break;

case 'l': cout<<"lose a life ";break;

case 'c': cout<<"find a friend ";}

getch();

}

Short url:   https://multidict.net/cs/6246