Chapter 3 CONTROL STRUCTURES: SELECTION
Topic Sequence Structure Selection Structures if  if…else switch Repetition Structures (looping) while do…while for break ,  continue  statement  &  exit() function
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
Sequence Structure Example Algorithm for calculating the area of a circle: 1. input  radius 2. calculate  area 3. print  area
Sequence Structure Example Statements executed one after the  other in the order written
Sequence Structure Example #include <stdio.h> void main() { int radius; float area; printf(&quot;Enter a radius of circle: &quot;); scanf (&quot;%d&quot;, &radius); area = 3.14 * radius * radius; printf(&quot;Area of circle is %.2f&quot;, area); }
SELECTION STRUCTURES Diamond symbol (decision symbol) –  Indicates decision is to be made –  Contains an expression that can be  true  or  false –  Test the condition, follow appropriate path C provides 3 types of selection structures 3.2  if  selection (single-selection structure) 3.3 if… else selection (double-selection structure) 3.5 switch  selection (multiple-selection structure)
if Selection -  if   selection structures is called a  single-selection structure  because 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 ; if  selection Flowchart
if Selection Examples Example 3.1: if (num1 > num2)   printf(&quot;%d is greater than %d\n&quot;, num1, num2);  Example 3.2: if (x == 100) printf(&quot;x is 100&quot;);
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(&quot;%d is greater than %d\n&quot;, num1, num2); printf(&quot;%d is less than or equal to %d\n&quot;, num2, num1); }  Example 3.4: if (x == 100) { printf (&quot;x is &quot;); printf (“%d”, x); }
if…else Selection Specifies an action to be performed both when the condition   is  true  and when it is  false Syntax : if (condition) statement1;  else statement2;
if…else Selection Examples Example 3.5 if (num1>num2) printf (&quot;%d is greater than %d\n &quot;, num1, num2); else printf (“%d is greater than %d\n &quot;, num2, num1);  Example 3.6 if (x==100) printf (&quot;x is 100&quot;); else printf (“x is not 100&quot;);
if…else Selection Examples Example 3.7 Based on the value of two variables, num1  and num2,  the suitable message will be printed.  if  (num1 > num2 ) printf(&quot;%d is greater than %d\n&quot;, num1, num2); else if (num1 < num2) printf(&quot;%d is less than %d\n&quot;, num1, num2); else printf(&quot;%d is equal to %d\n&quot;, num1, num2);
if…else Selection Examples Example 3.8  Based on the value of x, message either positive, negative or zero will be printed.   if (x > 0) printf(&quot;x is positive&quot;); else if (x < 0) printf(&quot;x is negative&quot;); else printf(&quot;x is 0&quot;);
#include <stdio.h> #include <time.h> main() { int num, guess;   /* seed & generate a random number */ srand(time(0)); num = rand() % 10;  /* get a guess from the user */  printf(&quot;Guess the number: &quot;); scanf(&quot;%d&quot;, &guess); if (guess < num) printf(&quot;Too low - the number was %d\n&quot;, num); else if (guess > num) printf(&quot;Too high - the number was %d\n&quot;, num); else printf(&quot;Correct - the number was %d\n&quot;, num); }
The Conditional Operator (?:) There is another way to write an  if…else  structure.  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;
Switch Selection switch  (expression) { case  constant_1 : group_of_statements_1 ; break; case  constant_2 : group_of_statements_2 ; break; : : default: default_group_of_statements ; }
The  switch  statement Multi-branch alternative to  if switch  ( expression ) { statement-sequence } The  statement-sequence  contains the  branches Each branch begins with the keyword  case case   integral-value : Each branch ends with the keyword  break The  constant must evaluate to some integral data type ( char  or  int ) Floating-point contant are not allowed
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(&quot;I&quot;); break; case 2: printf(&quot;II&quot;); break; case 3: printf(&quot;III&quot;); break; default: printf(&quot;?&quot;); }
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. switch (x)  { case 1: case 2: case 3: printf(&quot;x is 1, 2 or 3&quot;); break; default: printf(&quot;x is not 1, 2 nor 3&quot;); }
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
TUTORIAL
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 user enters 2, you have to multiply 2 numbers. Sample Execution: Input: 1 5 4 Input: 2 5 4 Output: 9   Output: 20

Ch3 selection

  • 1.
    Chapter 3 CONTROLSTRUCTURES: SELECTION
  • 2.
    Topic Sequence StructureSelection Structures if if…else switch Repetition Structures (looping) while do…while for break , continue statement & exit() function
  • 3.
    Structured Programming Sequencestructure 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 ExampleAlgorithm for calculating the area of a circle: 1. input radius 2. calculate area 3. print area
  • 5.
    Sequence Structure ExampleStatements executed one after the other in the order written
  • 6.
    Sequence Structure Example#include <stdio.h> void main() { int radius; float area; printf(&quot;Enter a radius of circle: &quot;); scanf (&quot;%d&quot;, &radius); area = 3.14 * radius * radius; printf(&quot;Area of circle is %.2f&quot;, area); }
  • 7.
    SELECTION STRUCTURES Diamondsymbol (decision symbol) – Indicates decision is to be made – Contains an expression that can be true or false – Test the condition, follow appropriate path C provides 3 types of selection structures 3.2 if selection (single-selection structure) 3.3 if… else selection (double-selection structure) 3.5 switch selection (multiple-selection structure)
  • 8.
    if Selection - if selection structures is called a single-selection structure because 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 ; if selection Flowchart
  • 9.
    if Selection ExamplesExample 3.1: if (num1 > num2) printf(&quot;%d is greater than %d\n&quot;, num1, num2); Example 3.2: if (x == 100) printf(&quot;x is 100&quot;);
  • 10.
    if Selection ExamplesIf 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(&quot;%d is greater than %d\n&quot;, num1, num2); printf(&quot;%d is less than or equal to %d\n&quot;, num2, num1); } Example 3.4: if (x == 100) { printf (&quot;x is &quot;); printf (“%d”, x); }
  • 11.
    if…else Selection Specifiesan action to be performed both when the condition is true and when it is false Syntax : if (condition) statement1; else statement2;
  • 12.
    if…else Selection ExamplesExample 3.5 if (num1>num2) printf (&quot;%d is greater than %d\n &quot;, num1, num2); else printf (“%d is greater than %d\n &quot;, num2, num1); Example 3.6 if (x==100) printf (&quot;x is 100&quot;); else printf (“x is not 100&quot;);
  • 13.
    if…else Selection ExamplesExample 3.7 Based on the value of two variables, num1 and num2, the suitable message will be printed. if (num1 > num2 ) printf(&quot;%d is greater than %d\n&quot;, num1, num2); else if (num1 < num2) printf(&quot;%d is less than %d\n&quot;, num1, num2); else printf(&quot;%d is equal to %d\n&quot;, num1, num2);
  • 14.
    if…else Selection ExamplesExample 3.8 Based on the value of x, message either positive, negative or zero will be printed. if (x > 0) printf(&quot;x is positive&quot;); else if (x < 0) printf(&quot;x is negative&quot;); else printf(&quot;x is 0&quot;);
  • 15.
    #include <stdio.h> #include<time.h> main() { int num, guess; /* seed & generate a random number */ srand(time(0)); num = rand() % 10; /* get a guess from the user */ printf(&quot;Guess the number: &quot;); scanf(&quot;%d&quot;, &guess); if (guess < num) printf(&quot;Too low - the number was %d\n&quot;, num); else if (guess > num) printf(&quot;Too high - the number was %d\n&quot;, num); else printf(&quot;Correct - the number was %d\n&quot;, num); }
  • 16.
    The Conditional Operator(?:) There is another way to write an if…else structure. 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) { case constant_1 : group_of_statements_1 ; break; case constant_2 : group_of_statements_2 ; break; : : default: default_group_of_statements ; }
  • 18.
    The switch statement Multi-branch alternative to if switch ( expression ) { statement-sequence } The statement-sequence contains the branches Each branch begins with the keyword case case integral-value : Each branch ends with the keyword break The constant must evaluate to some integral data type ( char or int ) Floating-point contant are not allowed
  • 19.
    Example 3.10 Thissection of code prints out the roman numeral corresponding to any of the numbers from 0 to 3. switch (num) { case 1: printf(&quot;I&quot;); break; case 2: printf(&quot;II&quot;); break; case 3: printf(&quot;III&quot;); break; default: printf(&quot;?&quot;); }
  • 20.
    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. switch (x) { case 1: case 2: case 3: printf(&quot;x is 1, 2 or 3&quot;); break; default: printf(&quot;x is not 1, 2 nor 3&quot;); }
  • 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.
  • 23.
    Exercise Write aprogram 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. Sample Execution: Input: 1 5 4 Input: 2 5 4 Output: 9 Output: 20