Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Fundamentals

Similar presentations


Presentation on theme: "Programming Fundamentals"— Presentation transcript:

1 Programming Fundamentals
Lecture 6 Ms. Farah Younas

2 Agenda of Lecture The if Selection The if/Else Selection
Nested if/else Selection

3 Relational Operators

4 Relational Operators

5 Relational Operators

6 Confusing Equality (==) and Assignment (=) Operators
Dangerous error Does not ordinarily cause syntax errors Any expression that produces a value can be used in control structures Nonzero values are true, zero values are false Example using ==: if ( payCode == 4 ) cout<<"You get a bonus!\n"; Checks paycode, if it is 4 then a bonus is awarded Example, replacing == with =: if ( payCode = 4 ) cout<<"You get a bonus!\n" ; This sets paycode to 4 4 is nonzero, so expression is true, and bonus awarded no matter what the paycode was Logic error, not a syntax error

7 The Logical Operators && ( logical AND ) || ( logical OR )
Returns true if both conditions are true || ( logical OR ) Returns true if either of its conditions are true ! ( logical NOT, logical negation ) Reverses the truth/falsity of its condition Unary operator, has one operand Useful as conditions in loops Expression Result true && false false true || false true !false true

8 Decision Making in C++ Decision making is about deciding the order of execution of statements based on certain conditions or repeat a group of statements until certain specified conditions are met. C++ handles decision-making by supporting the following statements, If statement switch statement conditional operator statement goto statement

9 Decision Making with If statement
The if statement may be implemented in different forms depending on the complexity of conditions to be tested. The different forms are: Simple if statement If…else statement Nested if…else statement else if statement

10 Simple If statement The general form of a simple if statement is,
If (expression) { Statement-inside; } Statement-outside; If expression is true, then statement-inside is executed, other wise only statement- outside will be executed.

11 Simple If statement The general form of a simple if statement is,
If (expression) { Statement-inside; } Statement-outside; If expression is true, then statement-inside is executed, other wise only statement- outside will be executed.

12

13 The if Selection Structure
Used to choose among alternative courses of action Pseudocode: If student’s grade is greater than or equal to 60 Print “Passed” If condition true Print statement executed and program goes on to next statement If false, print statement is ignored and the program goes onto the next statement Indenting makes programs easier to read C++ ignores whitespace characters

14 The if Selection Structure
Pseudocode statement in C++: If grade >= 60 Print “Passed” if ( grade >= 60 ) cout<<"Passed\n"; C++ code corresponds closely to the pseudocode Diamond symbol (decision symbol) Indicates decision is to be made Contains an expression that can be true or false Test the condition, follow appropriate path

15 The if Selection Structure

16 The if Selection Structure
if structure is a single-entry/single-exit structure true false grade >= 60 print “Passed” A decision can be made on any expression. zero - false nonzero – true Example: 3 - 4 is true

17 Simple If statement - Example

18 Simple If statement - Example
#include<iostream.h> int main() { int x,y; x=15; y=13; If(x > y) cout<<“x is greater than y”; } return 0;

19 The if/else Selection Structure
Test for multiple cases by placing if/else selection structures inside if/else selection structures

20 The if/else Selection Structure
Pseudocode: If student’s grade is greater than or equal to 60 Print “Passed” Else Print “Failed” C++ code: if ( grade >= 60 ) cout <<"Passed\n"; else cout << "Failed\n"; Ternary conditional operator (?:) cout << ( grade >= 60 ? "Passed" : "Failed" ); grade >= 60 ? cout << "Passed" : cout << "Failed";

21 The if/else Selection Structure
The general form of a simple if/else statement is, If (expression) { Statement-block1; } else Statement-block2; If expression is true, then statement-block1 is executed, other wise only statement- block2 will be executed.

22 The if/else Selection -Example
#include<iostream.h> int main() { int x,y; x=15; y=13; if(x > y){ cout<<“x is greater than y”; } else{ cout<<“y is greater than x”; return 0;}

23 Nested if…else Selection - Statement(general-form)
if (expression) { if (expression1) Statement-block1; } else Statement-block2; Statement-block3; If ‘expression’ is false the ‘statement- block3’ will be executed, otherwise it continues to perform the test for ‘expression 1’ . If the ‘expression 1’ is true the ‘statement block1’ is executed otherwise ‘statement-block2’ is executed

24 Else…if Selection The expression is tested from the top (of the ladder) downwards. As soon as the true condition is found, the statement associated with it is executed.

25 Else…if Selection

26 Dangling - else Problem
The C++ compiler always associates an else with the immediately preceding if unless told to do otherwise by the placement of braces ({ and }). This behavior can lead to what’s referred to as the dangling-else problem. (int x = 35; int y = 3; )

27 Dangling - else Problem

28 Blocks The if selection statement expects only one statement in its body. Similarly, the if and else parts of an if…else statement each expect only one body statement. To include several statements in the body of an if or in either part of an if…else, enclose the statements in braces ({ and }). A set of statements contained within a pair of braces is called a compound statement or a block

29 Questions??


Download ppt "Programming Fundamentals"

Similar presentations


Ads by Google