1 / 18

Programming In C++

Programming In C++. Spring Semester 2013 Lecture 4. www.oeconsultant.co.uk/preston.htm. Decisions. If Statement If-else Statement Else-if Statement Switch Statement Conditional Operator. Decisions. If Statement

evette
Download Presentation

Programming In C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming In C++ Spring Semester 2013 Lecture 4 Programming In C++, Lecture 3 By UmerRana

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

  3. Decisions • If Statement • If-else Statement • Else-if Statement • Switch Statement • Conditional Operator Programming In C++, Lecture 3 By UmerRana

  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 UmerRana

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

  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 UmerRana”); printf(“I tech in Preston University”); } } Programming In C++, Lecture 3 By UmerRana

  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)          statement1else (false)          statement2 Programming In C++, Lecture 3 By UmerRana

  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 UmerRana

  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 UmerRana

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

  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 UmerRana

  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 UmerRana

  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 UmerRana

  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 BahriaUniversity”); break; else printf(“I study in Comsat University”); Programming In C++, Lecture 3 By UmerRana

  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 processedswitch(expression/veriable)  {case constant1:                       statements 1;           break;case constant2:                       statements 2;           break;          default:                       statements n;          } Programming In C++, Lecture 3 By UmerRana

  16. Decisions Example intinput; 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 UmerRana

  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 StatementIf the condition is true, True-case Statementis returned else False-case Statementis returned. Programming In C++, Lecture 3 By UmerRana

  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 UmerRana

More Related