Presentation is loading. Please wait.

Presentation is loading. Please wait.

Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR www.msc-it-m.wapka.mobi/index.xhtml.

Similar presentations


Presentation on theme: "Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR www.msc-it-m.wapka.mobi/index.xhtml."— Presentation transcript:

1 Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR www.msc-it-m.wapka.mobi/index.xhtml

2 Objectives The Contents we are going to covers: Conditional Structures If – Structure If – else Structure If – else – if Structure Nested if Structure Compound Condition Switch Structure Conditional Operator 2

3 Conditional Structures In previous chapters we talked about sequential statements or flow of execution. Sequential statement(s) are executed in the same order in which they are written. In Sequential order flow of control is automatically is transferred to the next instruction one by one. In sequential order; all statements are executed only once. Neither any statement is skipped nor any statement is executed more than once. But what if we were to decide on the basis of some data? [Decision-Making] Conditional Structures C++ Provides Conditional Structures to handle these situations. A statement or set of statements that is executed when a particular condition is true and ignored when the condition is false is known is conditional statement(s). Suppose: a student got 43 marks and to be eligible for admission 40 marks are required and If marks are greater than or equal to 40 then the program should say Congratulations! (In C++ Scenario this process is called decision making or selection) C++ Provides branching or condition structures to make decisions. These structures checks the given condition with the help of Relational Operators. What are relational Operators ? CONTENTS Conditional Structures If – Structure If – else Structure If – else – if Structure Nested if Structure Compound Condition Switch Structure Conditional Operator 3

4 Relational Operators Relational Operator are used to specify the conditions in the program. They specify the condition between two expressions or values. Relational operators compares two expressions or values and always returns either true or false. (true or false are bool ean values of 1 byte.) o True – when the condition is valid. o False – when the condition is invalid Relational operators are sometimes called the conditional operators or comparison operators. C++ provides the following relational operators:  >(greater than)  <(less than)  ==(equals)  >=(greater than or equals)  <=(less than or equals)  !=(Not equals) Example Suppose variable a = 10 and variable b = 5, we apply some conditions. If a is ( greater than or equals ) b then print Hello World on console. Now substitute variables with values and condition with relational operator. If 10 is >= 5 then print “Hello World!” on console. [TRUE] Hello World! CONTENTS Conditional Structures If – Structure If – else Structure If – else – if Structure Nested if Structure Compound Condition Switch Structure Conditional Operator 4

5 Relational Operators Relational Expression An expression or statement that uses relational operators to compare two values is called Relational expression. The result of an expression is either True or False. Both side of a relational or conditional expression can be a constant, variable or arithmetic expression. Following table shows some key examples: CONTENTS Conditional Structures If – Structure If – else Structure If – else – if Structure Nested if Structure Compound Condition Switch Structure Conditional Operator 5

6 if – Structure If – Structure In C++ Language – the word if is a keyword. If statement is a decision making statement and is the simplest form of selection constructs. The If structure is used to execute or skip a statement or set of statements on the basis of a condition. Syntax: If (condition) statement; Here the condition is actually a relational expression which evaluates either true or false. Obviously if the condition is true then the statements under the if structure will be executed, skipped otherwise and the control will move to the statement after if structure. The syntax used above is for only one statement. Compound Statements An if structure can contain more than one statement. A set of statements can also be made as conditional. In this case, these statement are written in curly braces { }. This set of statements is called Compound Statements. CONTENTS Conditional Structures If – Structure If – else Structure If – else – if Structure Nested if Structure Compound Condition Switch Structure Conditional Operator 6

7 if – Structure If – Structure Syntax of if structure when there are compound statements. if(condition) { statement 1; statement 2; statement 3; … statement N; } Example: This program inputs an integer & evaluates a condition whether the marks are greater than or equals 50 using simple If-Structure. If so – it should print “Congratulations!” #include int main() { int marks = 0;// intializes an integer variable cout <<"Enter Your Marks: ";// prompt to user cin >> marks;// input marks if( marks >= 40)// condition cout <<"Congratulations!"<<endl; // conditional statement return 0; } CONTENTS Conditional Structures If – Structure If – else Structure If – else – if Structure Nested if Structure Compound Condition Switch Structure Conditional Operator 7

8 if – Structure Example of if structure with compound statements Example #include int main() { int marks = 0; cout <<"Enter Your Marks: "; cin >> marks; if( marks >= 40) { cout <<"Congratulations!"<<endl; cout <<"Admission granted!"<<endl; cout <<"*** Best of Luck ***"<<endl; } return 0; } CONTENTS Conditional Structures If – Structure If – else Structure If – else – if Structure Nested if Structure Compound Condition Switch Structure Conditional Operator 8

9 if-else Structure If-else Structure If-else statement or structure is another type of if-statement but it has two blocks of code. It executes one block of code when the particular condition is true and the other when the condition is false. In any case – one block is executed and the other is skipped. o Both blocks of code can never be executed o Both blocks of code can never be skipped Syntax if(condition) { statement 1; statement 2; … statement n; } else { statement 1; statement 2; … statement n; } CONTENTS Conditional Structures If – Structure If – else Structure If – else – if Structure Nested if Structure Compound Condition Switch Structure Conditional Operator 9 Simultaneously If Part Executes only when the condition is TRUE else Part Executes only when the condition is FALSE

10 if-else Structure If-else Structure Example of if else structure is given as: #include int main() { int num1, num2; cout << "Enter First Number: "; cin >> num1; cout << "Enter Second Number: "; cin >> num2; if(num1 >= num2) cout <<num1<<" is greater than or equals "<<num2<<endl; else cout <<num1<<" is less than "<<num2<<endl; return 0; } CONTENTS Conditional Structures If – Structure If – else Structure If – else – if Structure Nested if Structure Compound Condition Switch Structure Conditional Operator 10 Enter First Number: 25 Enter Second Number: 23 25 is greater than or equals 23 Press any key to continue... Enter First Number: 10 Enter Second Number: 15 10 is less than 15 Press any key to continue...

11 if-else-if Structure If-else-if Structure If-else-if statement is further another type of selection structure. It can be used to choose one block of code from many blocks of code. It is used when many options are available and we have to choose only one option on the basis of condition. Syntax of this structure: if(condition) { block 1; } else if (condition) { block 2; } else if (condition) { block 3; } … else { block N; } CONTENTS Conditional Structures If – Structure If – else Structure If – else – if Structure Nested if Structure Compound Condition Switch Structure Conditional Operator 11 Condition 1 Condition 2 Condition 3 Condition N Block 1 Block 2 Block 3 Block N T F F F T T T Rest of Code

12 if-else-if Structure If-else-if Structure Example: this program inputs test score of a student & displays his grade on the following criteria: Test ScoreGrade Above 90A 80 ~ 89B 70 ~ 79C 60 ~ 69D Below 60F #include int main() { int score; cout > score; if(score >= 90) cout <<"A Grade."<<endl; else if(score >= 80 ) cout <<"B Grade."<<endl; else if(score >= 70 ) cout <<"C Grade."<<endl; else if(score >= 60 ) cout <<"D Grade."<<endl; else cout <<"F Grade."<<endl; return 0; } CONTENTS Conditional Structures If – Structure If – else Structure If – else – if Structure Nested if Structure Compound Condition Switch Structure Conditional Operator 12 Enter your Score: 78 C Grade. Press any key to continue... Enter your Score: 91 A Grade. Press any key to continue... Enter your Score: 49 F Grade. Press any key to continue...

13 Nested if-else Structure CONTENTS Conditional Structures If – Structure If – else Structure If – else – if Structure Nested if Structure Compound Condition Switch Structure Conditional Operator 13 Nested if-else Structure An if statement within another if statement – this situation is called nested if-Structure. In nested structure, control enters into the inner section only when the outer condition is true. A programmer can use as many if-statements within another if-statement as needed by the program nature. The increase in the level of nesting increases the level of complexity. Syntax: if(condition) { if(condition) { } else { } else { } Working of Nested if Structure In nested if statement, the condition of the outer if is evaluated first. if outer if is true then control enters in the inner if statement. The inner if evaluated according to simple if- statement. Working of Nested if Structure In nested if statement, the condition of the outer if is evaluated first. if outer if is true then control enters in the inner if statement. The inner if evaluated according to simple if- statement.

14 Nested if-else Structure CONTENTS Conditional Structures If – Structure If – else Structure If – else – if Structure Nested if Structure Compound Condition Switch Structure Conditional Operator 14 Smallest Number From Three Integers #include int main() { int var1, var2, var3; cout << "Enter three integers: " <<endl; cin >> var1 >> var2 >> var3 ; if(var1 <= var2) { if(var1 < var3) cout <<"Smallest: "<<var1<<endl; else cout <<"Smallest: "<<var3<<endl; } else { if(var2 < var3) cout <<"Smallest: "<<var2<<endl; else cout <<"Smallest : " <<var3<<endl; } cout << endl; return 0; } Enter three integers: 263823 Smallest: 23 Press any key to continue...

15 Compound Condition CONTENTS Conditional Structures If – Structure If – else Structure If – else – if Structure Nested if Structure Compound Condition Switch Structure Conditional Operator 15 A type of comparison in which more than one condition is evaluated to make a decision is called Compound Condition. It is used to execute a statement or block by testing many conditions. Compound Condition are always comes with Logical Operators. C++ Provides Following Logical Operators AND operator ( && ) The Symbol used for AND operator is && (double ampersand). It is used to evaluate two or more conditions and returns true result only if all the conditions are true. False otherwise. OR Operator ( || ) The Symbol used for OR operator is || (double pipe Sign). It is used to evaluate two or more conditions and returns true result if either conditions is true. It produces false only when the all conditions becomes false. NOT Operator ( ! ) The Symbol used for NOT operator is ! (Sign of exclamation). It is used to reverse the result of a condition. It returns true results when the condition is false. It returns false result when the condition is true.

16 Compound Condition CONTENTS Conditional Structures If – Structure If – else Structure If – else – if Structure Nested if Structure Compound Condition Switch Structure Conditional Operator 16 This program inputs an positive integer and with the help of compound condition it tells whether the integer is even and divisible by 5 or not. #include int main() { int var = 0; cout > var; if( (var >= 0) && (var%2 == 0) && (var%5 == 0) ) cout<<var<<" is even and is divisible by 5"<<endl; else { if(var < 0 ) cout <<"Negatives are not asked! bye..."<<endl; else if(var%2 == 0) cout<<var<<" is even but not divisible by 5"<<endl; else if(var%5 == 0) cout<<var<<" is divisible by 5 but not an even."<<endl; else cout<<var<<" is neither even nor divisible by 5."<<endl; } cout << endl; return 0; } PositiveEven Divisible by 5 Enter an positive integer: 5 5 is divisible by 5 but not an even. Press any key to continue... ------------------------------------ Enter an positive integer: 50 50 is even and is divisible by 5 Press any key to continue... ------------------------------------ Enter an positive integer: 7 7 is neither even nor divisible by 5. Press any key to continue... ------------------------------------ Enter an positive integer: -1 Negatives are not asked! bye... Press any key to continue...

17 Switch Structure CONTENTS Conditional Structures If – Structure If – else Structure If – else – if Structure Nested if Structure Compound Condition Switch Structure Conditional Operator 17 Switch Structure The switch statement is another conditional statement. It is an alternative structure of nested if-else structure. It can be used more easily when many option are available and only one option has to be selected. Syntax switch(condition) { case constant 1: { block of code; break; } case constant 2: { block of code; break; }... case constant N: { block of code; break; } default: { block of code; }

18 Switch Structure CONTENTS Conditional Structures If – Structure If – else Structure If – else – if Structure Nested if Structure Compound Condition Switch Structure Conditional Operator 18 Switch Structure Difference between switch and nested if statements Both of the structures are same in the sense they are used for multiple selections but following table differentiate these statements: Switch Statement – use only when the decision is based on an INTEGER instead of range of values like less than this or greater than that etc…

19 Switch Structure CONTENTS Conditional Structures If – Structure If – else Structure If – else – if Structure Nested if Structure Compound Condition Switch Structure Conditional Operator 19 Switch Structure #include int main() { int choice; cout <<"Where you want to go?\nPlease Select your destination..."; cout <<"\n\nEnter 1 for Sweden"; cout <<"\nEnter 2 for Denmark"; cout <<"\nEnter 3 for Norway"; cout <<"\nEnter 4 for Finland"<<endl; cin >> choice ; switch (choice) { case 1: cout <<"\nYou decided to visit Sweden."<<endl; break; case 2: cout <<"\nYou decided to visit Denmark."<<endl; break; case 3: cout <<"\nYou decided to visit Norway."<<endl; break; case 4: cout <<"\nYou decided to visit Finland."<<endl; break; default: cout <<"Invalid Option!!!\nYou should stay in home..."; } cout <<endl; return 0; } Where you want to go? Please Select your destination... Enter 1 for GUJRANWALA Enter 2 for LAHORE Enter 3 for GUJRAT Enter 4 for FAISALABAD 4 You decided to visit FAISALABAD. Press any key to continue... ------------------------------------ Where you want to go? Please Select your destination... Enter 1 for GUJRANWALA Enter 2 for LAHORE Enter 3 for GUJRAT Enter 4 for FAISALABAD 5 Invalid Option!!! You should stay in home, Baby... Press any key to continue...

20 Conditional Operator CONTENTS Conditional Structures If – Structure If – else Structure If – else – if Structure Nested if Structure Compound Condition Switch Structure Conditional Operator 20 Conditional Operator Conditional operator is also an decision making structure. It can be used in place of simple if-else structure. It is also called ternary operator. As it always uses three operand. Syntax: (condition ) ? True-Case-Statement : False-Case-Statement ; Example of conditional operator is as follows: #include int main() { int var1, var2; int maximum; cout > var1 ; cout > var2 ; maximum = (var1 > var2) ? var1 : var2 ; cout<<"\nMmaximum number: "<<maximum<<endl; return 0; } Enter first number: 10 Enter second number: 9 Maximum number: 10 Press any key to continue...


Download ppt "Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR www.msc-it-m.wapka.mobi/index.xhtml."

Similar presentations


Ads by Google