condition in C++

 what is the condition?

It is a way to know the information that I have set or who enters by a user if it is true or wrong,more conditions for use:

  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b
  • Equal to a == b
  • Not Equal to: a != b

and there are two ways:





1-if :

  • Use if to specify a block of code to be executed, if a specified condition is true for example:

int x=10;
if (x > 8)
  cout << "x is greater than 8";


  • Use else to specify a block of code to be executed if the same; condition is false for example:

int x=13;
if (x > 18)
  cout << "x is greater than 18";
else
    cout<<"x is lower than 18";


  • Use else if to specify a new condition to test, if the first condition is false for example:

int x=13,y=20;
if (x > 18)
  cout << "x is greater than 18";
else if(y==20)
    cout<<y is equal to 20";



2-switch: Use switch to specify many alternative blocks of code to be executed for example:



int x=2;

switch(x){

case 1:cout<<"x is equal 1";break;

case 2:cout<<"x is equal 2";break;

case 3:cout<<"x is equal 3";break;

default:cout<<"not found";

}


Previous Post Next Post