Presentation is loading. Please wait.

Presentation is loading. Please wait.

EKT150 INTRODUCTION TO COMPUTER PROGRAMMING

Similar presentations


Presentation on theme: "EKT150 INTRODUCTION TO COMPUTER PROGRAMMING"— Presentation transcript:

1 EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Control Structure: Selection Part II Dr. Nur Hafizah Ghazali

2 Outline Continues with Selection Structures Nested if
Conditional Operator Switch Structures

3 Nested if Test for multiple cases by placing if…else statements inside if…else statement It is possible to have if within if statement When one control statement is within another, it is said to be nested

4 Nested if : Syntax Examples
No specific syntax for nested if Depends on the program structure Must have correct syntax for each if/ if- else/ if – else if statement if (expression1) { Statement A; if (expression2) Statement B; else Statement C; } Statement D; Statement E; if (expression1) { Statement A; if (expression2) Statement B; if (expression3) Statement C; } else Statement D; Statement E;

5 Example of Nested if – Basic Understanding
If the weather is good, I will go out in the garden, And if it’s warm, I will sit in the sun. else I will sit in the shade I will stay indoors I will then drink some lemonade Exercise: Draw flowchart for the above statement

6 Example of Nested if – Syntax
if (expression1) /*Weather is good?*/ { Statement A; /*Yes – go out in the garden*/ if (expression2) /*Warm?*/ Statement B; /*Yes – sit in the sun*/ else Statement C; /*No – Sit in the shade*/ } Statement D; /*Weather not god – stay in*/ Statement E; /*Drink lemonade in any event*/

7 Example of Nested if – Flow Chart
Is the weather good? YES Sit Indoors Out in the garden NO Is it warm? NO YES Sit in sun Sit in shade Drink some lemonade

8 Nested if – Exercise 1 Exercise: Draw flowchart for the above code
#include <stdio.h> int main() { int numb1, numb2; printf("Enter two integers to check\n"); scanf("%d %d",&numb1,&numb2); if(numb1==numb2) //checking whether two integers are equal printf("Result: %d = %d",numb1,numb2); else if(numb1>numb2) //checking whether numb1 is greater than numb2 printf("Result: %d > %d",numb1,numb2); printf("Result: %d > %d",numb2,numb1); } return 0; Exercise: Draw flowchart for the above code Predict and display the output

9 Nested if – Exercise 2 Exercise: Draw flowchart for the above code
#include<stdio.h> int main() { int mark; printf(“Enter mark:\n”); scanf("%d",&mark); if ( mark <= 10 ) printf("You did not study.\n"); printf("You failed!\n"); } else { // mark is 11 and above if ( mark < 60 ) // mark is between 11 and 60 printf("You failed !\n"); printf("Study harder\n"); else { //mark is equal or above than 60 printf("You passed!\n"); return 0; Exercise: Draw flowchart for the above code Predict and display the output

10 Read input temperature
Nested if – Exercise 3 Write a C program according to the following flow chart. Include one sample output of the program. Temperature<= 25 YES Begin Read input temperature temperature <= 35 NO Display “Warm” Display “Hot” End temperature <= 10 “ Cold” “Mild”

11 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

12 Conditional Operator - Syntax Condition ? result1 : result2
If the condition is true, result1 is returned else result2 is returned

13 Conditional Operator –
Basic Understanding c =( c > 0 ) ? 10 : - 10 If c is greater than 0, value of c will be 10 but, if c is less than n0, value of c will be -10

14 Conditional Operator –
Example 1 10 == 5 ? 11 : 12 10 ! = 4 ? 4 : 3 12 > 8 ? a : b Exercise: Predict and display the output for above conditional operator

15 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

16 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

17 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.

18 Switch Structures : Syntax
switch (expression) { case constant-expression 1: statement (s); break; case constant-expression 2: . default: statements; }

19 Switch Structures : Flow Chart
Is switch expression == case constant1? True Statement for first case False Is switch expression == case constant2? True Statement for second case False Default statements

20 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.

21 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.

22 Switch Structures : Example 1
#include <stdio.h> int main () { char grade = 'B'; switch(grade) case 'A' : printf("Excellent!\n" ); break; case 'B' : case 'C' : printf("Well done\n" ); case 'D' : printf("You passed\n" ); break; case 'F' : printf("Better try again\n" ); default : printf("Invalid grade\n" ); } printf("Your grade is %c\n", grade ); return 0;

23 Switch Structures : Example 2
int main () { int choice; printf (“1. Create a new database\n”); printf (“2. Edit a database\n”); printf (3. Delete a database\n”); printf (“4. Merge database\n”); printf (“5. Exit system\n”); printf (“Choose an option:”); scanf (“%d”, &choice); switch (choice) { case 1: printf (“Creating….\n”); break; case 2: printf (“Editing…\n”); case 3: printf (“Deleting…\n”); break; case 4: printf (“Merging…\n”); case 5: printf (“Thank you, Bye.\n”); default:: printf (“Invalid input!\n”); } return 0;

24 Switch Structures : Example 3
#include <stdio.h> int main() { char o; float num1,num2; printf("Select an operator either + or - or * or / \n"); scanf("%c",&o); printf("Enter two operands: "); scanf("%f%f",&num1,&num2); switch(o) { case '+': printf("%.1f + %.1f = %.1f",num1, num2, num1+num2); break; case '-': printf("%.1f - %.1f = %.1f",num1, num2, num1-num2); break; case '*': printf("%.1f * %.1f = %.1f",num1, num2, num1*num2); break; case '/': printf("%.1f / %.1f = %.1f",num1, num2, num1/num2); break; default: /* If operator is other than +, -, * or /, error message is shown */ printf("Error! operator is not correct"); } return 0;


Download ppt "EKT150 INTRODUCTION TO COMPUTER PROGRAMMING"

Similar presentations


Ads by Google