Date post: | 13-Jan-2015 |
Category: |
Education |
Upload: | hattori-sidek |
View: | 638 times |
Download: | 0 times |
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
Begin
Input radius
Area = 3.14 * radius * radius
Output area
End
Statements executed one after the other in the order written
Sequence Structure Example
#include <stdio.h>
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);
}
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("%d is greater than %d\n", num1, num2);
Example 3.2:
if (x == 100)
printf("x is 100");
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 %d\n", num1, num2);printf("%d is less than or equal to %d\n", num2, num1);
}
Example 3.4:if (x == 100){
printf ("x is ");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.5if (num1>num2)
printf ("%d is greater than %d\n ", num1, num2);else
printf (“%d is greater than %d\n ", num2, num1);
Example 3.6if (x==100)
printf ("x is 100");else
printf (“x is not 100");
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("%d is greater than %d\n", num1, num2);
else if (num1 < num2)
printf("%d is less than %d\n", num1, num2);
else
printf("%d is equal to %d\n", 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("x is positive");
else if (x < 0)printf("x is negative");
elseprintf("x is 0");
#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("Guess the number: ");scanf("%d", &guess);
if (guess < num)printf("Too low - the number was %d\n", num);
else if (guess > num)printf("Too high - the number was %d\n", num);
elseprintf("Correct - the number was %d\n", 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;
elsecost = 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;
}
Condition 1 Statement 1True
False
Condition 2 Statement 2True
False
Condition n Statement nTrue
False
Default statement
::
::
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("I");break;
case 2:printf("II");break;
case 3:printf("III");break;
default:printf("?");
}
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("x is 1, 2 or 3");
break;
default:
printf("x is not 1, 2 nor 3");
}
Transformation from switch to if…else
statement
switch statement if…else statement
switch (x) {
case ‘a’: printf("x is a"); break; case ‘b’: printf("x is b"); break; default: printf("value of x unknown");
}
if (x == ‘a’) printf("x is a");
else if (x == ‘b’) printf("x is b");
else printf("value of x unknown");
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 4Output: 9 Output: 20