Last Updated:

Conditions If - Else in C++

To specify a condition under which certain code will be executed, and if other code is not executed, you will need to resort to the following structures:

#include <iostream>
#include <string>
using namespace std; Namespace

int main()
{
setlocale(0,"");//Cyrillic

int age=120;
string tx;

if (age>0 && age<18)
{
tx="Child";
}
else if(age>=18 and age<=100)
{
tx="Adult";
}
else
{
tx="Error!";
}

cout << "You" << tx << endl;

system("pause");//Return screen
delay command 0;

}

Operands:

  • == - Comparison
  • > - more
  • < - less
  • >= - greater than or equal to
  • <= - less than or equal to
  • ! - Not
  • || - Logical &
  • && - logical OR
  • floor(x) – rounds down
  • round(x) – rounds mathematically
  • ceil(x) – rounds up
  • n++ and n-- - incrementation, decrementation

switch – condition operator

 

#include <iostream>
#include <string>
using namespace std; Namespace

int main()
{
setlocale(0,"");//Cyrillic
int fl=50;

switch(fl)
{
case 1://If fl==1
cout << "fl==1" << endl;
break;

Interrupt case 2://If fl==2
cout << "fl==2" << endl;
break; Interrupt

default://Other cout
<< "Otherwise fl=="<< fl << endl;
break; Interrupt
}

system("pause");//Screen
delay command return 0;

}

Ternary operator - = ? :

The ternary operator has the following structure: variable = (condition) ? if yes : otherwise;

Example:

#include <iostream>
#include <string>
namespace std; Namespace

int main()
{
setlocale(0,"");//Cyrillic

int i=9;
string tx=i>=10?" Greater than or equal to 10":"Less than 10";
cout << tx << endl;

system("pause");//Return screen
delay command 0;

}

Can be used nested as if-elseif-else

... 
int a=9, b=4;
string tx=(a==b)?" a equals b":(a>b)?" a greater than b":"a less than b";//equal, greater than or less than
...

goto operator

 

The goto operator is a program execution flow control operator that causes the CPU to jump from one piece of code to another.

Example:

#include <iostream>
#include <string>
using namespace std; Namespace

int main()
{
setlocale(0,"");//Cyrillic

cout << "Odin" << endl;
goto kuda; From here

cout << "Two" << endl;//Skips

kuda:Jumps here
cout << "Three" << endl;

system("pause");//Return screen
delay command 0;

}

#ifdef and #ifndef Directives

Directives #ifdef and #ifndef ("if defined" and "if not defined") are used in conditional compilation. The standard view is as follows:

#include <iostream>
#include <string>
using namespace std; Namespace

#define GENDefine
#define nGENDefine

int main()
{
setlocale(0,"");//Cyrillic

#ifdef GEN
cout << "GEN defined" << endl;
#else
cout << "GEN NOT defined" << endl;


#endif #ifndef nGEN
cout << "nGEN NOT defined" << endl;
#else
cout << "nGEN defined" << endl;

#endif
system("pause");//Return screen
delay command 0;

}

Try-catch-throw directive

To work comfortably with exceptions in C++, you only need to know three keywords:

  • try - the beginning of the exception block;
  • catch - the beginning of the block "catching" the exception;
  • throw - The keyword that "throws" the exception.

And now an example that demonstrates how to apply what you've learned:

#include <iostream>
#include <string>
using namespace std; Namespace

int main()
{
setlocale(0,"");//Cyrillic

try
{
throw 1;
//throw 'a';
}
catch (long b)
{
cout << "caught type long: " << b << endl;
}
catch (char b)
{
cout << "caught char type: " << b << endl;
}

system("pause");//Return screen
delay command 0;

}