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 I Dr. Nur Hafizah Ghazali

2 Outline Intro to control structure Types of selection
One-way selection Two-way selection Multi-selection Compound statement

3 Learning Objectives Apply concepts for developing algorithms, using variables and structuring a C program Explain what is meant by a control structures Explain the three basic types of control structures

4 Control Structure All programs can be written in terms of three control structures (like building blocks) Sequence Structure Selection Structure Repetition Structure

5 1. Sequence Structure Sequence Structure ‘Built-in’ to C
Unless otherwise directed, one statement after the next is executed Is a series of steps executed sequentially by default Pseudo Code Flow Chart Begin Input A and B Calculate A + B Print result of SUM End Begin Read input A,B Calculate SUM=A + B Print SUM End

6 2. Selection Structure Used to choose among alternative courses of action Used to control the flow of a program Also called as decision or branches Branches are conditions or choices used to enable selection of program flow C has three types: If selection statement (one-way selection) If…else selection statement (two-way selection) Switch selection statement (multiple selection)

7 One Way Selection : if if structures is a single-entry/single-exit structure The primary tool of selection structure Used when we want a single or a group of statements to be executed if the condition is true. If the condition is false, the statement is skipped and program continues by executing the first statement after the if selection structure Condition is express as a statement and two possible outcomes are indicate by true and false

8 if syntax – Part 1 Exercise: Draw flowchart for the syntax above.
if (test expression) { statement/s to be executed if test expression is true; } Exercise: Draw flowchart for the syntax above.

9 Flow Chart : if Selection
Test expression True Statement (s) False

10 if syntax – Part 2 if (this logical expression is true) { statement 1;
} next_statement; NO SEMI-COLON! The expression in parentheses can be any expression that results in a value of true or false If the expression is true, Statement 1 is executed, after which the program continues with next_statement If the expression is false, Statement 1 is skipped and execution continues immediately with next_statement

11 if statement : Basic Understanding
Example : Is it raining? If the answer yes, take umbrella Then walk to work Exercise: Draw flowchart for the above statement

12 If statement : Flow Chart
Expression Statement Next Statement True False Is it raining False True Take umbrella Walk to work

13 If statement : Example 1 Exercise: Draw flowchart for the above code
#include <stdio.h> int main() { int num; printf("Enter a number to check.\n"); scanf("%d",&num); if (num<10)/* checking whether number is less than 0 or not. */ { /*If test condition is true, statement below will be executed, otherwise it will not be executed */ printf("Number = %d\n",num); } printf("if statement in C programming is easy."); return 0; Exercise: Draw flowchart for the above code Predict and display the output

14 If statement : Example 2 Exercise: Draw flowchart for the above code
int main () { int num; printf (“Enter a number between -10 and 10:\n”); scanf (“%d”, &num); if (num > 0) printf (“%d is a positive number\n”, num); return 0; } Exercise: Draw flowchart for the above code Predict and display the output

15 If statement : Example 3 Exercise: Draw flowchart for the above code
#include <stdio.h> int main (void) { int number = 0; printf (“\nEnter an integer between 1 and 10\n”); scanf (“%d”, &number); if (number > 3) printf (“%d is greater than 3\n”, number); if (number < 6) printf (“%d is less than 6\n”, number); return 0; } Exercise: Draw flowchart for the above code Predict and display the output

16 If statement : Example 4 Write flow chart and a C program to determine if the student pass based on an average of 2 test marks. The pass marks is 40 and above.

17 Two Way Selection : if … else
Specifies and action to be performed both when the condition is true and when it is false If the boolean expression (or statements) evaluates to true, then the if block of code will be executed, otherwise else block of code will be executed.

18 if … else Syntax Exercise: Draw flowchart for the syntax above.
if (this logical expression is true) { statements(s) will be execute if the logical expression is true } else statement(s) will be execute if the logical expression is false NO SEMI-COLON! NO SEMI-COLON! Exercise: Draw flowchart for the syntax above.

19 If … else statement : Flow Chart
Test expression True Statement(s) False Statement(s)

20 if … else : Basic Understanding
If the rain today is worse than the rain yesterday I will take my umbrella else I will take my jacket Then I will go to work Exercise: Draw flowchart for the above statement

21 if … else : Basic Understanding
If the rain today is worse than the rain yesterday I will take my umbrella else I will take my jacket Then I will go to work Take jacket True False Take umbrella Rain worse than yesterday Go to work

22 if … else statement : Example 1
#include <stdio.h> int main () { int num; printf (“Enter a number between -10 and 10;”); scanf (“%d”, &num); if (num >= 0 ) printf(“%d is a positive number\n”, num); else printf (“%d is a negative number\n”, num); return 0; } Exercise: Draw flowchart for the above code Predict and display the output

23 if … else statement : Example 2
#include <stdio.h> int main () { int num; printf (“Enter a number you want to check.\n”); scanf (“%d”, &num); if ((num%2) == 0) /*checking whether remainder is 0 or not*/ printf (%d is even number.”, num); else printf (“%d is odd number.”, num); return 0; } Exercise: Draw flowchart for the above code Predict and display the output

24 if … else statement : Example 3
#include <stdio.h> int main () { int num = 0; printf (“Enter a number you want to check.\n”); scanf (“%d”, &num); if( num < 20 ){ /* if condition is true then print the following */ printf(“%d is less than 20\n“,num ); } else{ /* if condition is false then print the following */ printf(“%d is equal to or greater than 20\n“,num ); return 0; Exercise: Draw flowchart for the above code Predict and display the output

25 if … else statement : Example 4
Write flow chart and a C program to determine if the student pass or failed based on an average of 2 test marks. The pass marks is 40 and above.

26 Multiple Selection : if … else if
An if…else if control structure shifts program control, step by step, through a series of statement blocks. Is used when program requires more than one test expression. Very useful to test various conditions using single if…else statement.

27 if … else if Syntax Exercise: Draw flowchart for the syntax above.
if (test expression1) { statements(s) to be execute if test expression1 is true; } else if (test expression2) { statement(s) to be execute if test expression1 is false and 2 is true; else if (test expression3) { statement(s) to be execute if test expression1 and 2 are false and 3 is true; else { statements to be executed if all test expressions are false; Exercise: Draw flowchart for the syntax above.

28 if … else if statement : Flow Chart
Test expression 1 True Statement(s) False Test expression 2 True Statement(s) False Test expression 3 True Statement(s) False Statement(s)

29 if … else if statement : Example 1
#include <stdio.h> int main(){ int numb1, numb2; printf("Enter two integers for comparison: "\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); else printf("Result: %d > %d",numb2,numb1); return 0; } Exercise: Draw flowchart for the above code Predict and display the output

30 if … else if statement : Example 2
#include <stdio.h> int main () { char grade; printf(“Enter your grade:”); scanf(“%c”,&grade): if( grade == ‘A’ ) printf(“Your academic performance is excellent\n" ); else if( grade == ‘B’ ) printf(“Your academic performance is good\n" ); else if( grade == ‘C’ ) printf(“Your academic performance is satisfactory\n" ); else printf(“Your academic performance is poor\n" ); return 0; } Exercise: Draw flowchart for the above code Predict and display the output

31 if … else if statement : Example 3
Write flow chart and a C program to determine if the student pass or failed based on an average of 2 test marks. If the average mark is equal to and above 45, display “Passed”, if the average mark is between 40 and 45, display “Conditional Passed” and display “Failed” for marks lower than 40.

32 Compound (Block of ) Statement:
Any block of the code is written or embedded inside the pair of the curly braces. If condition specified inside the ‘if block’ is true, then statements written inside the pair of curly braces will be executed If expression evaluates to false, all the statements between the braces following the else will be executed In either case, execution continues with next statement. if (expression) { StatementA1; StatementA2; ... } else StatementB1; StatementB2; Next_Statement;

33 Compound Statement: Basic Understanding
If the weather is sunny, I will walk to the park, have a picnic and walk home else I will stay in, watch football and drink coke Sleep at 10 Exercise: Draw flowchart for the above statement

34 Compound Statement: Basic Understanding
True Weather is sunny False Stay in Walk to park False Watch Football Have a picnic Drink coke Walk home Sleep

35 Compound Statement: Example 1
int main() { int num = 10 ; if(num > 0) printf ("\nNumber is Positive"); printf ("\nThis is The Example of Compound Statement"); } return 0;

36 Compound Statement: Example 2
#include<stdio.h> int main() { int iAge=18; if (iAge > 17 ) printf (“Eligible to take car driving license\n”); print (“Allow to register at driving school\n”); } else printf (“Not eligible to drive\n”); printf (“Not allows to register at driving school\n”); return 0;


Download ppt "EKT150 INTRODUCTION TO COMPUTER PROGRAMMING"

Similar presentations


Ads by Google