Operators are used to make expressions and conditional statements works with expressions so we need to know about operators.
We have five kinds of operators in
C++:
§
Assignment operator :
§ Arithmetic operators :
§
( + ) 2+2=4
§
( - ) 2-2=0
§
( * ) 2*2=4
§
( / ) 2/2=1
§
( % ) 2%2=0
§
Increment/Decrement:
§
( ++ ) VarName++
§
( -- ) VarName--
§ Relational operators :
§ ( < ) 2<2=0
§ ( > ) 2>2=0
§
( <= ) 2<=2=1
§
( >= ) 2>=2=1
§
( == ) 2==2=1
§
Logical operators:
§
( && ) 1&&1 = 1 and
§
( || ) 0||0 =0 or
§
( ! ) !0 = 1 not
§ :Increment and decrement:
§ ( ++ ) 1++ = 2
§ ( -- ) 2-- = 1
§ Compound assignment operators:
§ ( ++ ) 1++ = 2
§ ( -- ) 2-- = 1
§ Compound assignment operators:
§
( += ) x += y (x
= x + y)
§
( -= ) x -= y (x
= x – y)
§
( *= ) x *= y (x
= x * y)
§
( /= ) x /= y (x
= x / y)
§
( %= ) x %=y (x
= x % y)
Conditional statements
Conditional statements have very simple rules if their
condition was true the statements will be executed otherwise compiler will skip
that statement and execute the default statement.
We have two kinds of conditional
statement in C++:
if statement:
structure of if statement:
if
( condition ) { codes; } else
if ( condition ) { codes; } … else { codes; }
if key word:
in order to use if key word we have
to write if keyword at the beginning of the statement so compiler will threat
it like if statement. We can use if statement alone without any else if or else
statement after it.
Condition:
A condition is made of operators
will be used in here inside prentices ( ( ) ) their result may be one of these
two values true or false. Conditions are two type simple conditions (
conditions that don’t have any logical operator in them ) and compound
conditions ( conditions that have
logical operator in them , these logical operators connect two simple
conditions ) which both can be used in if condition. We can use true or false
directly like a condition.
Codes:
In here codes can be our commands that
will be executed after the condition was true. Each curly braces in C++ makes a
scope so variables inside inner curly braces can’t be called from the outer
scopes.
else if:
else if can only be used after if
statement and it depends on if statement, I mean if statement always will be
checked first if the condition was false it will come to check else if
statements and if all else if statements was false it will execute the else but
if the if statement was true compiler will not execute else if condition even
if they are true because else if condition are depending into if condition.
else:
else statements are used as default
, they will be executed if the if
condition and all else if conditions was false. Of course, if there was not any
else if statement it will not complain.
Nested if :
We can write if condition (with or
without else if and else) inside another if, else if and else statements to
create nested if:
If
( condition ) { if ( condition ) { codes; } }
switch statement:
structure of switch statement in
C++:
switch( value )
{
case value1 : { codes; break; }
case value2 :
case value3 : { codes; break; }
.
.
.
default : { codes; }
}
switch keyword:
in order to use switch statement,
we have to write switch keyword at the start of the statement.
Value:
switch statements are using value
inside prentices ( ( ) ) to check them if it has any case with that value if
that was true the case with same value will be executed otherwise the default
will be executed.
Case and break:
Cases have values with them switch
statement will compare that value with the value of itself if that matches the
case codes will be executed but the problem is this which it will continue
executing next cases in order to avoid it we need to put a break key word
before ending of each case.
We can put more than one case back
to back to make the same code for those values like example above.
Default:
If all cases values were deferent
from switch value the default will be executed.
Ternary operator:
Syntax for the ternary operator in
C++.
( condition )?
codes : codes ;
Condition: It
is explained in if condition.
( ? ) : codes after this symbol will be executed if the condition
was true
( : ) : codes after this symbol will
be executed if the condition was false
We can
write values instead of codes, it will return value if the condition was true
or false:
DataType
VarName = ( condition ) ? value : value ;
Note:
other commands like cout can be used instead of value in ternary operators but in that case we cannot assign that to a value because it will not return any value.
No comments