Download presentation
Presentation is loading. Please wait.
1
Week 3 – Program Control Structure
Sequence and Selection control structure KUKUM Sem1-05/06 EKT120: Computer Programming
2
EKT120: Computer Programming
Outline Types of program control structure Sequence control Selection Types of selection One-way selection Two-way selection Multi-selection Compound statement Nested if Conditional operator Switch structure KUKUM Sem1-05/06 EKT120: Computer Programming
3
Types of Program Control Structure
Sequence Branch or Decision Loop KUKUM Sem1-05/06 EKT120: Computer Programming
4
Sequence Control Structure
The most common and basic structure of a program A set of instructions in sequence from top to bottom KUKUM Sem1-05/06 EKT120: Computer Programming
5
Sequence Control Structure, Cont…
Example Program to convert Temperature unit in Celcius to Fahrenheit unit, begin to 0 degree to 100 degree celcius in 20 degree increment. Solution The equation F = C + 32 C = degree Celcius F = degree Fahrenheit 9 5 KUKUM Sem1-05/06 EKT120: Computer Programming
6
Sequence Control Structure, Cont…
Algorithm / Pseudocode Flowchart C Program Begin Declare variable Print heading Set C = 0 Calculate F Print C and F Set C = 20 Set C = 40 Set C = 60 Set C = 80 Set C = 100 End #include <stdio.h> Void main (void) { float C, F; fprintf(“Table of Celcius and Fahrenheit”); fprintf(“Celcius\t\tFahrenheit”); C :=20; F :=(9/5)*C + 32; printf(“%8.2f\t\t%8.2f”,C, F); C :=40; F :=(9/5)*C + 32; C :=60; F :=(9/5)*C + 32; C :=80; F :=(9/5)*C + 32; C :=100; F :=(9/5)*C + 32; } KUKUM Sem1-05/06 EKT120: Computer Programming
7
EKT120: Computer Programming
Comment… 9 The equation F = C + 32 The program equation F :=(9/5)*C + 32; Command printf(“%8.2f\t\t%8.2f”,C, F); Print format to print float number with 8 numbers and at 2 decimal points. Semicolon ; must have at the end of C command. 5 KUKUM Sem1-05/06 EKT120: Computer Programming
8
EKT120: Computer Programming
Selection structure Used to choose among alternative courses of action C has three types: if, if..else, and switch KUKUM Sem1-05/06 EKT120: Computer Programming
9
EKT120: Computer Programming
Selection Statements 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 KUKUM Sem1-05/06 EKT120: Computer Programming
10
EKT120: Computer Programming
Types of selection One-way selection = if Two-way selection = if..else Multi-selection Nested if Switch structure = switch KUKUM Sem1-05/06 EKT120: Computer Programming
11
The if selection structure
if structure is a single-entry/single-exit structure Pseudocode Flowchart begin if student’s grade is greater than or equal to 60 Print “Passed” end KUKUM Sem1-05/06 EKT120: Computer Programming
12
EKT120: Computer Programming
One-way Selection in C Example: if (grade >= 60) printf(“Passed\n”); ….. Comment.. (grade>=60) – This is a relation statement which result in logic TRUE or FALSE. If the statement (grade >=60) is true the the command printd(Passed\n) shall be executed. KUKUM Sem1-05/06 EKT120: Computer Programming
13
One-way Selection- example
Another example: char grade; …… if(markah>= 90) grade = 'A'; …... printf(“Grade is : %c\n”, grade); KUKUM Sem1-05/06 EKT120: Computer Programming
14
EKT120: Computer Programming
One-way Selection = if In C, a condition is represented by a logical (Boolean) expression true and false are logical (Boolean) values The syntax of one-way selection is: if (expression) statement; If the value of the expression is true, statement is executed; if false, statement is not executed and the computer goes on to the next statement in the program. KUKUM Sem1-05/06 EKT120: Computer Programming
15
EKT120: Computer Programming
One-way Selection = if Another example: if (temperature is greater than 70 degree and it is not raining) recommended activity is golfing bool rain=false; … If ((temp > 70) && !(rain)) printf(“recommended activity is golfing”); And not KUKUM Sem1-05/06 EKT120: Computer Programming
16
EKT120: Computer Programming
One-way Selection = if Common Errors if score >= //no parentheses grade = 'A'; if(score >= 90); //; not here KUKUM Sem1-05/06 EKT120: Computer Programming
17
Two-way Selection = if..else
The syntax of two-way selection is: if (expression) statement1; else statement2; If the value of the expression is true, statement1 is executed; if false, statement2 is executed Relational exp, or logical exp KUKUM Sem1-05/06 EKT120: Computer Programming
18
The if..else selection structure
Specifies an action to be performed both when the condition is true and when it is false Pseudocode Flowchart begin if student’s grade greater than or equal to 60 then Print “Passed” else Print “Failed” end KUKUM Sem1-05/06 EKT120: Computer Programming
19
Two-way Selection - if..else in C
……… if (grade >=60) printf(“Passed\n”); else printf(“Failed\n”); …… KUKUM Sem1-05/06 EKT120: Computer Programming
20
Two-way Selection = if..else
Another example: if (hours > 40.0) //Line 1 wages = 40.0 * rate +1.5 * rate * (hours ); //Line 2 else //Line 3 wages = hours * rate; //Line 4 If hours is 50, then the statement at Line 2 executes If hours is 30, then the statement at Line 4 executes KUKUM Sem1-05/06 EKT120: Computer Programming
21
Multi-selection = if-else if
The syntax is: if(exp1) stmt1; else if(exp2) stmt2; else if(exp3) stmt3; … else stmt n; An if-else if control structure shifts program control, step by step, through a series of statement blocks. KUKUM Sem1-05/06 EKT120: Computer Programming
22
Multi-selection if-else if - example
marks grade 90 – 100 A 80 – 89 B 70 – 79 C 60 – 69 D 0 – 59 F KUKUM Sem1-05/06 EKT120: Computer Programming
23
Multi-selection - if-else if in C
if(mark >= 90) printf( “Grade = A”\n”); else if(mark >=80) printf(“Grade = B\n”); else if(mark >=70) printf(“Grade = C\n”); else if (mark >= 60) printf( “Grade = D\n”); else printf(“Grade = F); KUKUM Sem1-05/06 EKT120: Computer Programming
24
Compound (Block of) Stmt
A compound statement (also called a block of statements) takes the form { statement 1; statement 2; . statement n; } It is considered a single statement KUKUM Sem1-05/06 EKT120: Computer Programming
25
Compound (Block of) Stmt
Example: if(mark >= 90) { printf(“Pass”\n”); printf(“Grade = A\n”);} else if(mark >=80){ printf(“Grade = B\n”);} else if(mark >=70){ printf(“Grade = C\n”);} else if (mark >= 60){ printf( “Grade = D\n”);} else {printf(“Pass”\n”); printf(“Grade = F);} KUKUM Sem1-05/06 EKT120: Computer Programming
26
EKT120: Computer Programming
Nested if When one control statement is within another, it is said to be nested if(exp1) if(exp2) { stmt1; stmt2; } { stmt1; stmt2; } KUKUM Sem1-05/06 EKT120: Computer Programming
27
EKT120: Computer Programming
Nested if Eg. if (temperature >= 50) { if (temperature >= 80) printf( "Good day for swimming.\n”); else printf( "Good day for golfing.\n“); } printf("Good day to play tennis.\n“); KUKUM Sem1-05/06 EKT120: Computer Programming
28
The Conditional Operator (? :)
The syntax of using the conditional operator is: expression1 ? expression2 : expression3; This is called a conditional expression. The statement: if (a >= b) max = a; else max = b; Is equivalent to the statement: max = (a >= b) ? a : b; KUKUM Sem1-05/06 EKT120: Computer Programming
29
EKT120: Computer Programming
Switch Structures Similar to if-else if control structure The general form (syntax): switch (expression) { case value1: statements1; break; case value2: statements2; break; . case valuen: statementsn; break; default: statements; } KUKUM Sem1-05/06 EKT120: Computer Programming
30
EKT120: Computer Programming
Switch Structures The break statement has a special meaning and may or may not appear after each statement. In C, switch, case, break, and default are reserved words. In a switch structure, first the expression is evaluated. The value of the expression is then used to perform the corresponding action. KUKUM Sem1-05/06 EKT120: Computer Programming
31
EKT120: Computer Programming
Switch Structures The expression is usually an identifier. The value of the expression can be only integral. The expression is sometimes called the selector. Its value determines which statement is selected for execution. A particular case value should appear only once. One or more statements may follow a case label, so you do not need to use braces to turn multiple statements into a single compound statement. The break statement may or may not appear after each statement. KUKUM Sem1-05/06 EKT120: Computer Programming
32
EKT120: Computer Programming
Switch Structures Example: switch (grade) { case 'A': printf("The grade is A.“); break; case 'B': printf("The grade is B.“); break; case 'C': printf("The grade is C.“); break; case 'D': printf("The grade is D.“); break; case 'F': printf("The grade is F.“); break; default: printf("The grade is invalid.“); } where, grade is a variable of the type char. If the value of grade is, say 'A', the output is The grade is A. KUKUM Sem1-05/06 EKT120: Computer Programming
33
EKT120: Computer Programming
Switch Structures The switch statement executes according to the following rules: When the value of the expression is matched against a case value (also called a label), the statements execute until either a break statement is found or the end of the switch structure is reached. If the value of the expression does not match any of the case values, the statements following the default label execute. If the switch structure has no default label, and if the value of the expression does not match any of the case values, the entire switch statement is skipped. A break statement causes an immediate exit from the switch structure KUKUM Sem1-05/06 EKT120: Computer Programming
34
EKT120: Computer Programming
End Week 2 Q & A! KUKUM Sem1-05/06 EKT120: Computer Programming
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.