Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming In C++ Spring Semester 2013 Lecture 4 Programming In C++, Lecture 3 By Umer Rana.

Similar presentations


Presentation on theme: "Programming In C++ Spring Semester 2013 Lecture 4 Programming In C++, Lecture 3 By Umer Rana."— Presentation transcript:

1 Programming In C++ Spring Semester 2013 Lecture 4 Programming In C++, Lecture 3 By Umer Rana

2 www.oeconsultant.co.uk/preston.htm Programming In C++, Lecture 3 By Umer Rana

3 Decisions n If Statement n If-else Statement n Else-if Statement n Switch Statement n Conditional Operator Programming In C++, Lecture 3 By Umer Rana

4 Decisions If Statement The if statement controls conditional branching. The body of an if statement is executed if the value of the expression is true. Syntax: if ( expression ) statement Example: if(a==b) printf(“I study in Preston University”); Programming In C++, Lecture 3 By Umer Rana

5 Decisions Multiple Statement with if if(a==b) { printf(“My name is Umer Rana”); printf(“I tech in Preston University”); } Programming In C++, Lecture 3 By Umer Rana

6 Decisions Nested if Statements One if statement is part of the body of another if statement. if(a==b) { if(c==d) { printf(“My name is Umer Rana”); printf(“I tech in Preston University”); } Programming In C++, Lecture 3 By Umer Rana

7 Decisions if-else Statement This feature permits the programmer to write a single comparison, and then execute one of the two statements depending upon whether the test expression is true or false. Syntax if(expression)(true) statement1 else(false) statement2 Programming In C++, Lecture 3 By Umer Rana

8 Decisions Example: if(a==b) { printf(“I study in Preston University”); } else { printf(“I study in Iqra University”); } Programming In C++, Lecture 3 By Umer Rana

9 Decisions if-else if Statement This construct is useful where two or more alternatives are available for selection. if(a==b) { printf(“I study in Preston University”); } else { if(a==c) printf(“I study in Iqra University”); else printf(“I study in Bahria University”); } Programming In C++, Lecture 3 By Umer Rana

10 Decisions if Statement with Logical Operators || Logical OR && Logical AND ! Logical NOT Programming In C++, Lecture 3 By Umer Rana

11 Decisions if Statement with Logical Operators || Logical OR The Logical (||) means if either the expression on the right or left side or both sides of the operator it true then the entire expression is true. OR operator represented by the exclamation point (||). Example: if ( (a==b) || (a==c)) printf (“I study in Preston University”); Programming In C++, Lecture 3 By Umer Rana

12 Decisions if Statement with Logical Operators && Logical AND The Logical (&&) means if both expressions on the right and left sides of the operator is true then the entire expression is true. AND operator represented by the exclamation point (&&). Example: if ( (a==b) && (a==c)) printf (“I study in Preston University”); Programming In C++, Lecture 3 By Umer Rana

13 Decisions if Statement with Logical Operators Logical NOT(!) This operator reverses the logical value of the expression it operates on, it makes a true expression false and a false expression true. Not operator takes only one operand and represented by the exclamation point (!). Example: if ( !( x < 5 ) ) or if ( ! (c) ) Programming In C++, Lecture 3 By Umer Rana

14 Decisions Break Statement in if Statement if(a==b) printf(“I study in Preston University”); break; else if(a==c) printf(“I study in Iqra University”); break; else if(a==e) printf(“I study in Bahria University”); break; else printf(“I study in Comsat University”); Programming In C++, Lecture 3 By Umer Rana

15 Decisions The Switch Statement The switch statement is almost the same as an “if statement”, substitute for long if statements. The switch statement selection is based on the value of a single variable or of a simple expression. If the value of the switch expression or variable equals the value of one of the case value, the statements following that case value are processed. If not, the default label statements are processed switch(expression/veriable) { case constant1: statements 1; break; case constant2: statements 2; break; default: statements n; } Programming In C++, Lecture 3 By Umer Rana

16 Decisions Example int input; printf( "1. BSCS-1\n" ); printf( "2. BSCS-2\n" ); printf( "3. BSCS-3\n" ); scanf( "%d", &input ); switch ( input ) { case 1: printf(“I study in BSCS-1\n"); break; case 2: printf(" I study in BSCS-2\n"); break; case 3: printf(" I study in BSCS-3\n "); break; default: printf( "Bad input!\n" ); break; } Programming In C++, Lecture 3 By Umer Rana

17 Decisions The Conditional Operator The conditional operator is also known as ternary operator. It is called ternary operator because it takes three arguments. The conditional operator evaluates an expression returning a value if that expression is true and different one if the expression is evaluated as false. Syntax: condition ? True-case Statement : False-case Statement If the condition is true, True-case Statement is returned else False-case Statement is returned. Programming In C++, Lecture 3 By Umer Rana

18 Decisions The Conditional Operator input=1 ? printf(“I study in BSCS-1\n") : printf(“I study in BSCS-2\n"); Programming In C++, Lecture 3 By Umer Rana


Download ppt "Programming In C++ Spring Semester 2013 Lecture 4 Programming In C++, Lecture 3 By Umer Rana."

Similar presentations


Ads by Google