ch3 selection

Download Ch3 selection

Post on 13-Jan-2015

619 views

Category:

Education

0 download

Embed Size (px)

DESCRIPTION

 

TRANSCRIPT

  • 1. Chapter 3 CONTROL STRUCTURES: SELECTION

2. Topic

  • Sequence Structure
  • Selection Structures
      • if
      • ifelse
      • switch
  • Repetition Structures (looping)
      • while
      • dowhile
      • for
  • break ,continuestatement&exit() function

3. Structured Programming

  • Sequence structure
    • One command right after another in order
  • Selection structure
    • One execution branch or another (but not both)
  • Iterative (Repetition) structure
    • Repeating one section of command(s) multiple times

4. Sequence Structure Example

  • Algorithm for calculating the area of a circle:
  • 1. inputradius
  • 2. calculatearea
  • 3. printarea

5. Sequence Structure Example

  • Statements executed one after theother in the order written

6. Sequence Structure Example

  • #include
  • void main()
  • {
  • int radius;
  • float area;
  • printf("Enter a radius of circle: ");
  • scanf ("%d", &radius);
  • area = 3.14 * radius * radius;
  • printf("Area of circle is %.2f", area);
  • }

7. SELECTION STRUCTURES

  • Diamond symbol (decision symbol)
  • Indicates decision is to be made
  • Contains an expression that can betrueorfalse
  • Test the condition, follow appropriate path
  • C provides 3 types of selection structures
  • 3.2ifselection (single-selection structure)
  • 3.3 if else selection (double-selection structure)
  • 3.5 switchselection (multiple-selection structure)

8. if Selection

  • -if selection structures is called asingle-selection structurebecause it selects or ignores a single action.
  • - either performs (selects) an action if a condition is true or skips the action if the condition is false.

Syntax: if(condition) statement ; ifselection Flowchart 9. if Selection Examples

  • Example 3.1:
  • if (num1 > num2)
  • printf("%d is greater than %dn", num1, num2);
  • Example 3.2:
  • if (x == 100)
  • printf("x is 100");

10. if Selection Examples

  • If more than one statement should be executed if the condition is true, then these may be grouped together inside braces.
  • Example 3.3:
  • if (num1 > num2)
  • {
  • printf("%d is greater than %dn", num1, num2);
  • printf("%d is less than or equal to %dn", num2, num1);
  • }
  • Example 3.4:
  • if (x == 100)
  • {
  • printf ("x is ");
  • printf (%d, x);
  • }

11. ifelse Selection

  • Specifies an action to be performed both when the condition
  • istrueand when it isfalse
  • Syntax :
    • if (condition)
    • statement1;
    • else
    • statement2;

12. ifelse Selection Examples

  • Example 3.5
  • if (num1>num2)
  • printf ("%d is greater than %dn ", num1, num2);
  • else
  • printf (%d is greater than %dn ", num2, num1);
  • Example 3.6
  • if (x==100)
  • printf ("x is 100");
  • else
  • printf (x is not 100");

13. ifelse Selection Examples

  • Example 3.7 Based on the value of two variables, num1and num2,the suitable message will be printed.
    • if(num1 > num2 )
    • printf("%d is greater than %dn", num1, num2);
    • else if (num1 < num2)
    • printf("%d is less than %dn", num1, num2);
    • else
    • printf("%d is equal to %dn", num1, num2);

14. ifelse Selection Examples

  • Example 3.8Based on the value of x, message either positive, negative or zero will be printed.
    • if (x > 0)
    • printf("x is positive");
    • else if (x < 0)
    • printf("x is negative");
    • else
    • printf("x is 0");

15.

  • #include
  • #include
  • main()
  • { int num, guess;
  • /* seed & generate a random number */
  • srand(time(0));
  • num = rand() % 10;
  • /* get a guess from the user */
  • printf("Guess the number: ");
  • scanf("%d", &guess);
    • if (guess < num)
    • printf("Too low - the number was %dn", num);
    • else if (guess > num)
    • printf("Too high - the number was %dn", num);
    • else
    • printf("Correct - the number was %dn", num);
  • }

16. The Conditional Operator (?:)

  • There is another way to write anifelsestructure.
  • Syntax:
  • condition?statement_1:statement_2 ;
  • Example:
  • cost = (cost>10.00) ? 15.00 : 10.00;
  • if (cost>10.00)
  • cost = 15.00;
  • else
  • cost =10.00;

17. Switch Selection

  • switch(expression)
  • {
  • caseconstant_1 : group_of_statements_1 ; break;
  • caseconstant_2 : group_of_statements_2 ; break;
  • :
  • :
  • default: default_group_of_statements ;
  • }

18. Theswitchstatement

  • Multi-branch alternative toif
    • switch( expression ) { statement-sequence }
    • Thestatement-sequencecontains thebranches
    • Each branch begins with the keywordcase
    • case integral-value :
    • Each branch ends with the keywordbreak
    • Theconstant must evaluate to some integral data type ( charorint )
    • Floating-point contant are not allowed

19.

  • Example 3.10 This section of code prints out the roman numeral corresponding to any of the numbers from 0 to 3.
    • switch (num)
    • {
    • case 1:
    • printf("I");
    • break;
    • case 2:
    • printf("II");
    • break;
    • case 3:
    • printf("III");
    • break;
    • default:
    • printf("?");
    • }

20.

  • Example 3.11If the value of x is 1, 2 or 3, the x is 1, 2 or 3 message will be printed. Else, x is not 1, 2 nor 3 message will be appeared.
      • switch (x)
      • {
      • case 1:
      • case 2:
      • case 3:
      • printf("x is 1, 2 or 3");
      • break;
      • default:
      • printf("x is not 1, 2 nor 3");
      • }

21. Transformation fromswitchtoifelsestatement if (x == a) printf("x is a"); else if (x == b) printf("x is b"); else printf("value of x unknown"); switch (x){ case a: printf("x is a"); break; case b: printf("x is b"); break; default: printf("value of x unknown"); } ifelsestatement switchstatement 22. TUTORIAL 23. Exercise

  • Write a program to add or multiply 2 integers. You have to ask the user to enter 3 values. The first one will be a 1 or 2 and the last two will be the numbers that you want to add/multiply. If the user enters 1, you have to add 2 numbers, if the