1 / 30

Chapter#3 Structured Program Development in C++

King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited By: Ghadah R. Hadba. Chapter#3 Structured Program Development in C++. 2 nd semester 1432-1433. Control structure.

boyd
Download Presentation

Chapter#3 Structured Program Development 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. King Saud University College of Applied studies and Community Service Csc 1101 By: AsmaAlosaimi Edited By: Ghadah R. Hadba Chapter#3Structured Program Development in C++ 2nd semester 1432-1433

  2. Control structure • Normally, statements in a program are executed one after the other in the order in which they are written. This is called sequential execution. • Various C++ statements will enable you to specify the next statement to be executed which might be other than the next one in sequence. This is called transfer of control.

  3. Types of Control structure • could be written in terms of only three control structures: • Sequence structure • Selection structure • Repetition structure.

  4. Types of Control Structures

  5. Types of Control structure • could be written in terms of only three control structures: • Sequence structure • Selection structure • Repetition structure.

  6. Selection structure • C++ provides three types of selection structures in the form of statements: • The if selection statement either performs (selects) an action if a condition is true or skips the action if the condition is false. • The if…else selection statement performs an action if a condition is true and performs a different action if the condition is false. • The switch selection statement performs one of many different actions depending on the value of an expression.

  7. selection structure: if • The if statement is called a single-selection statement because it selects or ignores a single action. • Syntax: if (expression) statement Expression referred to as decision maker. Statement referred to as action statement.

  8. selection structure: if-Example • Write a program that reads a student’s grade and prints “passed” if the grade is greater than or equal 60. • Flowchart: • Code: ... if ( grade >= 60 ) cout<<"Passed”; ... Note that, these statements Represents the selection part Of the code (i.e. not the whole Code)

  9. selection structure: if … else • The if…else statement is called a double-selection statement because it selects between two different actions. • Syntax: if (expression) statement1 else statement2

  10. selection structure: if … else -Example • For example, • pseudocode statement • If student’s grade is greater than or equal to 60Print “Passed”elsePrint “Failed” • Code: … if ( grade >= 60 ) cout<<"Passed";else cout<<"Failed"; … • Flowchart

  11. conditional Operator (? :) • C++ provides the conditional operator (?:) which is closely related to the if…elsestatement. • The conditional operator is C++’s only ternary operator i.e. it takes three operands. • Syntax: expression1 ? expression2 : expression3 • If expression1 = true, then the result of the condition is expression2. • Otherwise, the result of the condition is expression3.

  12. Conditional (? :) Operator- example1 Example: int x = 5 , y =3 , min ; if (x <= y) min = x ; else min = y ; The above statement can be written using the conditional operator as following: min = ( x <= y ? x : y);

  13. Conditional (? :) Operator- example2 … if ( grade >= 60 ) cout<<"Passed";else cout<<"Failed"; … The above statement can be written using the conditional operator as following: Cout << ( grade >= 60 ? “Passed” : “Failed”);

  14. Compound (Block of) Statements Syntax: { statement1 statement2 . . . statementn }

  15. Compound (Block of) Statements: Example if ( grade >= 60 ) cout<<"Passed\n";else {cout<<"Failed\n" ; cout<<“you have to take this course again\n"; } Java Programming: From Problem Analysis to Program Design, D.S. Malik

  16. Multiple Selection: Nested if syntax if (expression1) statement1 else if(expression2) statement2 else statement3 • Multiple if statements can be used if there is more than two alternatives • else is associated with the most recent if that does not have an else

  17. Multiple Selection: Nested if (Example-1) • Many C++ programmers prefer to write the preceding if statement as • if ( grade >= 90 )cout"A\n" ;else if ( grade >= 80 )cout<< "B\n" ;else if ( grade >= 70 )cout<<"C\n" ;else if ( grade >= 60 )cout<<"D\n" ;else cout<<"F\n" ;

  18. Multiple Selection: Nested if (Example-2) • Remember: else is associated with the most recent ifthat does not have an else if( tempreture >= 50 ) if (tempreture >= 80) cout<<“Good swimming day”; else cout<<“Good golfing day”; else cout<<“Good tennis day”;

  19. Multiple Selection: Nested if (Example-3) • Remember: else is associated with the most recent ifthat does not have an else if( tempreture >= 50 ) if (tempreture >= 80) cout<<“Good swimming day”; else cout<<“Good golfing day”;

  20. Multiple Selection: Nested if (Example-4) //The following code does not work as intended. For example if GPA=3.8 if ( GPA >= 2.0 ) if (GPA >= 3.9) cout<<“Dean Honor list\n”; else cout<<“GPA below graduation requirement\n”; ---------------------------------------------------------- //This is the correct way of writing the above code if ( GPA >= 2.0 ) { if (GPA >= 3.9) cout<<“Dean Honor list”; } else cout<<“GPA below graduation requirement\n”; • To force the nested if..else statement to execute as it was originally intended we must use the braces {} as following:

  21. selection structure: Switch • The switch statement is called a multiple-selection statement because it selects among many different actions.

  22. selection structure: Switch • Syntax: • switch(expression) • { • casevalue1: statements1 • break; • casevalue2: statements2 • break; • ... • casevaluen: statementsn • break; • default: statements • } • Expression: • is also known as selector. • Expression can be an identifier. • Value can only be integral.

  23. Type of values in Switch Cases • In Switch statement each action is associated with a value of constant integral expression(i.e. any combination of character constants and integer constant that evaluates to a constant integer value) • Thus, switch selector must be either : • An variable of integer or char data type, OR • An expression that evaluates to a constant integer value.

  24. true N == 1 ? x = 10; false break; true N == 2 ? switch(N) { case 1: x = 10; break; case 2: x = 20; break; case 3: x = 30; break; } x = 20; false break; true N == 3 ? x = 30; false break; Switch With break Statements

  25. true N == 1 ? x = 10; false switch(N) { case 1: x = 10; case 2: x = 20; case 3: x = 30; } true N == 2 ? x = 20; false true N == 3 ? x = 30; false Switch With no break Statements

  26. Switch With break Statements Example switch (grade) { case 'A': cout<<"The grade is A."; break; case 'B': cout<<"The grade is B."; break; case 'C': cout<<"The grade is C."; break; case 'D': cout<<"The grade is D."; break; case 'F': cout<<"The grade is F."; break; default: cout<<"The grade is invalid."; }

  27. Previous Example - With Nested If • if (grade == 'A')printf("The grade is A.");elseif (grade == 'B')printf("The grade is B.");elseif (grade == 'C')printf("The grade is C.");elseif (grade == 'D')printf("The grade is D.");elseif (grade == 'F')printf("The grade is F.");elseprintf("The grade isinvalid.");

  28. Switch With break Statements Example 2 switch(number1+number2) { case0: cout<<"The sum is 0"; break; case1: cout<<"The sum is 1"; break; case2: cout<<"The sum is 2"; break; case3: cout<<"The sum is 3"; break; case4: cout<<"The sum is 4"; break; default: cout<<“ illegal expression"; }

  29. Switch With break Statements Example 3 switch(number%2) { case0: cout<<number<<“is an even number”; break; case1: cout<< number<<“is an odd number”; break; default: cout<<number<<"is invalid."; }

  30. Exercise • Write a program that reads the price and the production date ( month and year of an item ) then the program prints the price after applying the discount rate which equals to : • 50% if it is produced before 2011. • 30% if it is produced in 2011 but before the 4th month. • 10% if it is produced after 4 ,2011 and before 8,2011

More Related