Understand Decision structures in c++ (cplusplus)
-
Upload
tahir-bashir -
Category
Education
-
view
134 -
download
6
Transcript of Understand Decision structures in c++ (cplusplus)
DECISION STRUCTURESTHE IF-ELSE IF STATEMENT AND NESTED IF STATEMENTS IN C++
Presentators:M. Tahir BashirFahad IftikharBilal Ahmad
C++
@UMT 8-Apr-2016
CONDITIONAL STATEMENTS
• The C++ conditional statements are the:
• Therefore they are sometimes called selection statements
• Conditional statements give us the power to make basic decisions
• if statement
• if-else statement
• switch statement
• A conditional statement lets us choose which statement will be executed next
FLOW OF CONTROL
• Unless specified otherwise, the order of statement execution through a function is linear: one statement after another in sequence
• Some programming statements allow us to:• decide whether or not to execute a particular statement• execute a statement over and over, repetitively
• These decisions are based on Boolean expressions (or conditions) that evaluate to true or false
• The order of statement execution is called the flow of control
THE IF STATEMENT
• The if statement has the following syntax:
if ( condition ) statement;
if is a C++reserved word
The condition must be aBoolean expression. It mustevaluate to either true or false.
If the condition is true, the statement is executed.If it is false, the statement is skipped.
LOGIC OF AN IF STATEMENT
conditionevaluated
statement
truefalse
THE IF STATEMENT
• An example of an if statement:if (FirstNo > SecondNo)result = FirstNo - SecondNo;cout<<"The resultant is"<< result;
• First the condition is evaluated -- the value of FirstNo is either greater than the value of SecondNo, or it is not
• If the condition is true, the assignment statement is executed -- if it isn’t, it is skipped.
• Either way, the call to cout is executed next
RELATIONAL OPERATORS
• A condition often uses one of C++'s equality operators or relational operators
== equal to
< less than
!= not equal to
• Note the difference between the equality operator (==) and the assignment operator (=)
> greater than
<= less than or equal to
>= greater than or equal to
LOGICAL OPERATORS
• C++ provides logical operators.• The binary logical operators combine two Boolean expressions into one.• The unary logical operator switches the value of a Boolean expression.
• Binary logical operators have lower precedence than relational operators • NOT has the same precedence as negation.
Operator
Meaning Kind
&& AND Binary|| OR Binary! NOT Unary
LOGICAL NOT
• The logical NOT operation is also called logical negation or logical complement
• If some condition a is true, then !a is false; if a is false, then !a is true• Logical expressions can be shown using a truth table
a !aTrue FalseFalse True
LOGICAL AND & LOGICAL OR
• The logical AND expressiona && b
is true if both a and b are true, and false otherwise
• The logical OR expressiona || b
is true if a or b or both are true, and false otherwise
IF-ELSE STATEMENT:
General form of an if-else statement:if(BooleanExpression)
statement or block 1else
statement or block 2
LOGIC OF AN IF-ELSE STATEMENT
conditionevaluated
statement1
true false
statement2
THE CONDITIONAL OPERATOR
• C++ provides and operator to create short expressions that work like if-else statements.
BooleanExpression ? Value1 : Value2;• If BooleanExpression is true, Value1 is returned• If BooleanExpression is false, Value2 is returned
• Example:if (score < 50)cout<<“Sorry! You Have Failed…";elsecout<<"You Have Successfully Passed! ";
THE IF-ELSE IF STATEMENT
• Sometimes you need to be able to test a series of conditions• You can do this with the if-else if statement
• General form:if (BooleanExpression1)
statement or block 1else if (BooleanExpression2)
statement or block 2else
statement or block 3• If BooleanExpression1 is true, then statement or block 1 is executed.• If BooleanExpression1 is false, then BooleanExpression2 is tested.
• If BooleanExpression2 is true, then statement or block 2 ais executed.• If BooleanExpression2 is false, then statement or block 3 is executed.
Note: You can have as many if else clauses as is needed.
NESTED IF STATEMENTS
• Nesting is enclosing one structure inside of another.• A block in C++ can contain any valid C++ code, this includes other if statements:
if(BooleanExpression1) {if(BooleanExpression2) {
statement1;statement2;
}statement3;statement4;
}• If BooleanExpression1 is true and BooleanExpression2 is true , what is executed?
• statement1 , statement2 , statement3 , statement4• If BooleanExpression1 is true and BooleanExpression2 is false , what is executed?
• statement3 , statement4
THE END
ANY QUESTION