Ekt150 Introduction To Computer Programming: Control Structure: Selection Part II
Ekt150 Introduction To Computer Programming: Control Structure: Selection Part II
COMPUTER PROGRAMMING
Control Structure:
Selection Part II
Exercise:
• Draw flowchart for the above statement
Example of Nested if –
Syntax
YES NO
Is it warm?
Drink some
lemonade
Nested if – Exercise 1
#include <stdio.h>
int main()
{
int numb1, numb2;
printf("Enter two integers to check\n");
scanf("%d %d",&numb1,&numb2);
NO
YES If numb1 ==
numb2
Display
YES Is numb1> NO
numb1==numb2
numb2?
End
Exercise 1 : Nested if Sample Output
NO
YES
mark <=10
End
Exercise 2 : Nested if Sample Output
Enter mark:
75
You passed!
Enter mark:
9
You did not study. You failed!
YES Temperature NO
<= 25
End
Nested if – Exercise 3
Write a C program according to the following flow chart. Include one sample output of the
program.
#include<stdio.h>
int main()
Enter temperature:
{
int mark; 9
printf(“Enter temperature:\n”); Cold
scanf("%d",&temperature);
if (temperature <= 25 )
Enter temperature:
{
if(temperature<=10) 24
printf(“Cold\n"); Mild
else
printf(“Mild\n”);
}
Enter temperature:
else { 34
if (temperature <=35 ) Warm
printf(“Warm\n");
else
Enter temperature:
printf(“Hot\n"); 40
} Hot
return 0;
}
Past Year Questions ( Midterm 2014/2015)
Predict and write the output of the program segment in Figure 2(a), given totalprice=1200 and
itemnumber=10.
if(totalprice>=1000)
{
if(itemnumber>10)
{
//calculate new total price with discount 50%
totalprice=totalprice*0.5;
printf(“Get 50% discount\n”);
}
else
{
//calculate new total price with discount 30%
totalprice=totalprice*0.7;
printf(“Get 30% discount\n”);
}
}
else
{
//calculate new total price with discount 10%
totalprice=totalprice*0.9;
printf(“Get 10% discount\n”);
}
printf(“Total Price = %.2f”,totalprice);
[4 marks]
Conditional Operator ( ? : )
• The conditional operator in C is also
known as ternary operator – because it
has three arguments.
• Evaluates an expression returning a value
if that expression is true and different one
if the expression is evaluated as false
Conditional Operator - Syntax
c =( c > 0 ) ? 10 : - 10
Exercise:
• Predict and display the output for above
conditional operator
Conditional Operator –
Example 2
#include <stdio.h>
int main() {
int a , b;
a = 10;
printf( "Value of b is %d\n", (a == 1) ? 20: 30 );
printf( "Value of b is %d\n", (a == 10) ? 20: 30 );
}
Exercise:
• Predict and display the output for above conditional operator
Conditional Operator –
Example 3
#include <stdio.h>
int main()
{
char feb;
int days;
printf("Enter 1 if the year is leap year otherwise enter 0: ");
scanf("%c",&feb);
days = (feb==‘1') ? 29:28;
/*If test condition (feb==‘1’) is true, days will be equal to 29. */
/*If test condition (feb==‘1') is false, days will be equal to 28. */
printf("Number of days in February = %d",days);
return 0;
}
Exercise:
• Predict and display the output for above conditional operator
Switch Structures
• Similar to if-else if control structure
• Allows a variable to be tested for equality
against a list of values
• Each value is called a case, and the
variable being switched on is checked for
each switch case.
Switch Structures : Syntax
switch (expression) {
case constant-expression 1:
statement (s);
break;
case constant-expression 2:
statement (s);
break;
.
.
.
default:
statements;
}
Switch Structures : Flow Chart
Is switch
expression == True
Statement for
case first case
constant1?
False
Is switch True
expression == Statement for second
case constant2? case
False
Default statements
Switch Structures : Rules (1)
• The expression used in a switch statement must have
an integral or enumerated type, or be of a class type in
which the class has a single conversion function to an
integral or enumerated type
• Can have any number of case statements within a
switch. Each case is followed by the value to be
compared to and a colon.
• The constant-expression for a case must be the same
data type as the variable in the switch, and it much be
constant or a literal.
Switch Structures : Rules (2)
• When the variable being switched on is equal to a case, the
statements following that case will execute until a break
statement is reached.
• When a break statement is reached, the switch terminates,
and the flow of control jumps to the next line following the
switch statement
• Not every case needs to contain a break. If no break appears,
the flow of control will fall through to subsequent cases until a
break is reached
• Can have an optional default case, which must appear at the
end of the switch. The default case can be used for performing
a task when none of the cases is true. No break is needed in
the default case.
Switch Structures : Example 1
#include <stdio.h>
int main ()
{
char grade = 'B'; case 'D' :
switch(grade) printf("You passed\n" );
{ break;
case 'A' : case 'F' :
printf("Better try again\n" );
printf("Excellent!\n" );
break;
break;
default :
case 'B' : printf("Invalid grade\n" );
case 'C' : }
printf("Well done\n" ); printf("Your grade is %c\n", grade );
break; return 0;
}
Switch Structures : Example 2
int main ()
{ case 3:
int choice; printf (“Deleting…\n”);
printf (“1. Create a new database\n”); break;
printf (“2. Edit a database\n”); case 4:
printf (3. Delete a database\n”); printf (“Merging…\n”);
printf (“4. Merge database\n”); break;
printf (“5. Exit system\n”); case 5:
printf (“Choose an option:”); printf (“Thank you, Bye.\n”);
scanf (“%d”, &choice); break;
switch (choice) { default::
case 1: printf (“Invalid input!\n”);
printf (“Creating….\n”); break;
break; }
case 2: return 0;
printf (“Editing…\n”); }
break;
Switch Structures : Example 3
#include <stdio.h>
int main()
{
char o;
float num1,num2;
scanf (“%d”,&beta)
switch (beta)
{
case 1:
case 2: beta = beta + 6; break;
case 4: beta--;
case 5: beta = 6 * beta;
case 6: beta = beta + 10; break;
default: beta--;
} (2 marks)
Past Year Questions ( Midterm 2015/2016)
Convert the if-else if segment in the following Figure to a switch statement.
if (colour == 1)
printf (“red”);
else if (colour == 2)
printf (“yellow”);
else if (colour == 3)
printf (“green”);
else
printf (“other colours”); [8 Marks/Markah]