Condition - C++
Hello everyone!
Today we are continue basics topic from C++. I know that it is not interesting for all, but still everyone should know about possibilities. So without tones of text we have few statements first and the most "famous" is "if". If you made something then you do that.
If( is now is after 8pm? ) { // go sleep }
you can ask about teeth wash and sleeping
If( have you washed your teeth? ) {
// yes! you can sleep
} else { // go wash your teeth
}
Or if condition is difficult
If( is now is after 8pm and have you washed your teeth? ) {
// go sleep
} else if( is now is after 8pm? ) { // wash your teeth and go sleep } else { // oh no? So go where you want }
For simple condition we can use special statements "? :". Example:
(is now after 8mp) ? sleep() : do_anything_else();
condition ? if true we do this : if false we do that ;
Sometimes we need to made something which have 20 or more conditions i saw it in customer android system and then if, else is not effective - code looks badly. So what then? We have more readable switch state which make code clear.
Example 1 switch ( number ) { case 1:
// do think 1 and end
break;
case 2:
// do think 2 and go to case 3
case 3:
// do think 3 and end
break;
default:
// print ("that option is not correct") and end
break; }
Example 2
switch ( word ) {
case "word 1":
// do think 1 and end
break;
case "word 2":
// do think 2 and end
break;
}
As you see switch is more complex than if. As you see in this 2 examples you can make it more or less complicated. You can add default state which can be used when option never came into any case. You can make few case in row like example 1 case 2, everything which is in this case (2) will be run case 3, because there is not break statement. That mean you can do correlated states.
This is all for now. Thank you for reading and have a nice day!