17
DECISION STRUCTURES THE IF-ELSE IF STATEMENT AND NESTED IF STATEMENTS IN C++ Presentators: M. Tahir Bashir Fahad Iftikhar Bilal Ahmad C++ @UMT 8-Apr- 2016

Understand Decision structures in c++ (cplusplus)

Embed Size (px)

Citation preview

Page 1: 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

Page 2: Understand Decision structures in c++ (cplusplus)

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

Page 3: Understand Decision structures in c++ (cplusplus)

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

Page 4: Understand Decision structures in c++ (cplusplus)

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.

Page 5: Understand Decision structures in c++ (cplusplus)

LOGIC OF AN IF STATEMENT

conditionevaluated

statement

truefalse

Page 6: Understand Decision structures in c++ (cplusplus)

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

Page 7: Understand Decision structures in c++ (cplusplus)

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

Page 8: Understand Decision structures in c++ (cplusplus)

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

Page 9: Understand Decision structures in c++ (cplusplus)

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

Page 10: Understand Decision structures in c++ (cplusplus)

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

Page 11: Understand Decision structures in c++ (cplusplus)

IF-ELSE STATEMENT:

General form of an if-else statement:if(BooleanExpression)

statement or block 1else

statement or block 2

Page 12: Understand Decision structures in c++ (cplusplus)

LOGIC OF AN IF-ELSE STATEMENT

conditionevaluated

statement1

true false

statement2

Page 13: Understand Decision structures in c++ (cplusplus)

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! ";

Page 14: Understand Decision structures in c++ (cplusplus)

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.

Page 15: Understand Decision structures in c++ (cplusplus)

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

Page 16: Understand Decision structures in c++ (cplusplus)

THE END

Page 17: Understand Decision structures in c++ (cplusplus)

ANY QUESTION