Ch3 selection

1,043 views

Published on

Published in: Education, Technology, Travel
  • Be the first to comment

  • Be the first to like this

Ch3 selection

  1. 1. Chapter 3 CONTROL STRUCTURES: SELECTION
  2. 2. Topic <ul><li>Sequence Structure </li></ul><ul><li>Selection Structures </li></ul><ul><ul><ul><li>if </li></ul></ul></ul><ul><ul><ul><li>if…else </li></ul></ul></ul><ul><ul><ul><li>switch </li></ul></ul></ul><ul><li>Repetition Structures (looping) </li></ul><ul><ul><ul><li>while </li></ul></ul></ul><ul><ul><ul><li>do…while </li></ul></ul></ul><ul><ul><ul><li>for </li></ul></ul></ul><ul><li>break , continue statement & exit() function </li></ul>
  3. 3. Structured Programming <ul><li>Sequence structure </li></ul><ul><ul><li>One command right after another in order </li></ul></ul><ul><li>Selection structure </li></ul><ul><ul><li>One execution branch or another (but not both) </li></ul></ul><ul><li>Iterative (Repetition) structure </li></ul><ul><ul><li>Repeating one section of command(s) multiple times </li></ul></ul>
  4. 4. Sequence Structure Example <ul><li>Algorithm for calculating the area of a circle: </li></ul><ul><li>1. input radius </li></ul><ul><li>2. calculate area </li></ul><ul><li>3. print area </li></ul>
  5. 5. Sequence Structure Example <ul><li>Statements executed one after the other in the order written </li></ul>
  6. 6. Sequence Structure Example <ul><li>#include <stdio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int radius; </li></ul><ul><li>float area; </li></ul><ul><li>printf(&quot;Enter a radius of circle: &quot;); </li></ul><ul><li>scanf (&quot;%d&quot;, &radius); </li></ul><ul><li>area = 3.14 * radius * radius; </li></ul><ul><li>printf(&quot;Area of circle is %.2f&quot;, area); </li></ul><ul><li>} </li></ul>
  7. 7. SELECTION STRUCTURES <ul><li>Diamond symbol (decision symbol) </li></ul><ul><li>– Indicates decision is to be made </li></ul><ul><li>– Contains an expression that can be true or false </li></ul><ul><li>– Test the condition, follow appropriate path </li></ul><ul><li>C provides 3 types of selection structures </li></ul><ul><li>3.2 if selection (single-selection structure) </li></ul><ul><li>3.3 if… else selection (double-selection structure) </li></ul><ul><li>3.5 switch selection (multiple-selection structure) </li></ul>
  8. 8. if Selection <ul><li>- if selection structures is called a single-selection structure because it selects or ignores a single action. </li></ul><ul><li>- either performs (selects) an action if a condition is true or skips the action if the condition is false. </li></ul>Syntax: if (condition) statement ; if selection Flowchart
  9. 9. if Selection Examples <ul><li>Example 3.1: </li></ul><ul><li>if (num1 > num2) </li></ul><ul><li>printf(&quot;%d is greater than %dn&quot;, num1, num2); </li></ul><ul><li>Example 3.2: </li></ul><ul><li>if (x == 100) </li></ul><ul><li>printf(&quot;x is 100&quot;); </li></ul>
  10. 10. if Selection Examples <ul><li>If more than one statement should be executed if the condition is true, then these may be grouped together inside braces. </li></ul><ul><li>Example 3.3: </li></ul><ul><li>if (num1 > num2) </li></ul><ul><li>{ </li></ul><ul><li>printf(&quot;%d is greater than %dn&quot;, num1, num2); </li></ul><ul><li>printf(&quot;%d is less than or equal to %dn&quot;, num2, num1); </li></ul><ul><li>} </li></ul><ul><li>Example 3.4: </li></ul><ul><li>if (x == 100) </li></ul><ul><li>{ </li></ul><ul><li>printf (&quot;x is &quot;); </li></ul><ul><li>printf (“%d”, x); </li></ul><ul><li>} </li></ul>
  11. 11. if…else Selection <ul><li>Specifies an action to be performed both when the condition </li></ul><ul><li> is true and when it is false </li></ul><ul><li>Syntax : </li></ul><ul><ul><li>if (condition) </li></ul></ul><ul><ul><li>statement1; </li></ul></ul><ul><ul><li>else </li></ul></ul><ul><ul><li>statement2; </li></ul></ul>
  12. 12. if…else Selection Examples <ul><li>Example 3.5 </li></ul><ul><li>if (num1>num2) </li></ul><ul><li>printf (&quot;%d is greater than %dn &quot;, num1, num2); </li></ul><ul><li>else </li></ul><ul><li>printf (“%d is greater than %dn &quot;, num2, num1); </li></ul><ul><li>Example 3.6 </li></ul><ul><li>if (x==100) </li></ul><ul><li>printf (&quot;x is 100&quot;); </li></ul><ul><li>else </li></ul><ul><li>printf (“x is not 100&quot;); </li></ul>
  13. 13. if…else Selection Examples <ul><li>Example 3.7 Based on the value of two variables, num1 and num2, the suitable message will be printed. </li></ul><ul><ul><li>if (num1 > num2 ) </li></ul></ul><ul><ul><li>printf(&quot;%d is greater than %dn&quot;, num1, num2); </li></ul></ul><ul><ul><li>else if (num1 < num2) </li></ul></ul><ul><ul><li>printf(&quot;%d is less than %dn&quot;, num1, num2); </li></ul></ul><ul><ul><li>else </li></ul></ul><ul><ul><li>printf(&quot;%d is equal to %dn&quot;, num1, num2); </li></ul></ul>
  14. 14. if…else Selection Examples <ul><li>Example 3.8 Based on the value of x, message either positive, negative or zero will be printed. </li></ul><ul><ul><li>if (x > 0) </li></ul></ul><ul><ul><li>printf(&quot;x is positive&quot;); </li></ul></ul><ul><ul><li>else if (x < 0) </li></ul></ul><ul><ul><li>printf(&quot;x is negative&quot;); </li></ul></ul><ul><ul><li>else </li></ul></ul><ul><ul><li>printf(&quot;x is 0&quot;); </li></ul></ul>
  15. 15. <ul><li>#include <stdio.h> </li></ul><ul><li>#include <time.h> </li></ul><ul><li>main() </li></ul><ul><li>{ int num, guess; </li></ul><ul><li>/* seed & generate a random number */ </li></ul><ul><li>srand(time(0)); </li></ul><ul><li>num = rand() % 10; </li></ul><ul><li>/* get a guess from the user */ </li></ul><ul><li>printf(&quot;Guess the number: &quot;); </li></ul><ul><li>scanf(&quot;%d&quot;, &guess); </li></ul><ul><ul><li>if (guess < num) </li></ul></ul><ul><ul><li>printf(&quot;Too low - the number was %dn&quot;, num); </li></ul></ul><ul><ul><li>else if (guess > num) </li></ul></ul><ul><ul><li>printf(&quot;Too high - the number was %dn&quot;, num); </li></ul></ul><ul><ul><li>else </li></ul></ul><ul><ul><li>printf(&quot;Correct - the number was %dn&quot;, num); </li></ul></ul><ul><li>} </li></ul>
  16. 16. The Conditional Operator (?:) <ul><li>There is another way to write an if…else structure. </li></ul><ul><li>Syntax: </li></ul><ul><li>condition ? statement_1 : statement_2 ; </li></ul><ul><li>Example: </li></ul><ul><li>cost = (cost>10.00) ? 15.00 : 10.00; </li></ul><ul><li>if (cost>10.00) </li></ul><ul><li>cost = 15.00; </li></ul><ul><li>else </li></ul><ul><li>cost = 10.00; </li></ul>
  17. 17. Switch Selection <ul><li>switch (expression) </li></ul><ul><li>{ </li></ul><ul><li>case constant_1 : group_of_statements_1 ; break; </li></ul><ul><li>case constant_2 : group_of_statements_2 ; break; </li></ul><ul><li>: </li></ul><ul><li>: </li></ul><ul><li>default: default_group_of_statements ; </li></ul><ul><li>} </li></ul>
  18. 18. The switch statement <ul><li>Multi-branch alternative to if </li></ul><ul><ul><li>switch ( expression ) { statement-sequence } </li></ul></ul><ul><ul><li>The statement-sequence contains the branches </li></ul></ul><ul><ul><li>Each branch begins with the keyword case </li></ul></ul><ul><ul><li>case integral-value : </li></ul></ul><ul><ul><li>Each branch ends with the keyword break </li></ul></ul><ul><ul><li>The constant must evaluate to some integral data type ( char or int ) </li></ul></ul><ul><ul><li>Floating-point contant are not allowed </li></ul></ul>
  19. 19. <ul><li>Example 3.10 This section of code prints out the roman numeral corresponding to any of the numbers from 0 to 3. </li></ul><ul><ul><li>switch (num) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>case 1: </li></ul></ul><ul><ul><li>printf(&quot;I&quot;); </li></ul></ul><ul><ul><li>break; </li></ul></ul><ul><ul><li>case 2: </li></ul></ul><ul><ul><li>printf(&quot;II&quot;); </li></ul></ul><ul><ul><li>break; </li></ul></ul><ul><ul><li>case 3: </li></ul></ul><ul><ul><li>printf(&quot;III&quot;); </li></ul></ul><ul><ul><li>break; </li></ul></ul><ul><ul><li>default: </li></ul></ul><ul><ul><li>printf(&quot;?&quot;); </li></ul></ul><ul><ul><li>} </li></ul></ul>
  20. 20. <ul><li>Example 3.11 If 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. </li></ul><ul><ul><ul><li>switch (x) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>case 1: </li></ul></ul></ul><ul><ul><ul><li>case 2: </li></ul></ul></ul><ul><ul><ul><li>case 3: </li></ul></ul></ul><ul><ul><ul><li>printf(&quot;x is 1, 2 or 3&quot;); </li></ul></ul></ul><ul><ul><ul><li>break; </li></ul></ul></ul><ul><ul><ul><li>default: </li></ul></ul></ul><ul><ul><ul><li>printf(&quot;x is not 1, 2 nor 3&quot;); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>
  21. 21. Transformation from switch to if…else statement if (x == ‘a’) printf(&quot;x is a&quot;); else if (x == ‘b’) printf(&quot;x is b&quot;); else printf(&quot;value of x unknown&quot;); switch (x) { case ‘a’: printf(&quot;x is a&quot;); break; case ‘b’: printf(&quot;x is b&quot;); break; default: printf(&quot;value of x unknown&quot;); } if…else statement switch statement
  22. 22. TUTORIAL
  23. 23. Exercise <ul><li>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 user enters 2, you have to multiply 2 numbers. </li></ul><ul><li>Sample Execution: </li></ul><ul><ul><li>Input: 1 5 4 Input: 2 5 4 </li></ul></ul><ul><ul><li>Output: 9 Output: 20 </li></ul></ul>

×