490 likes | 614 Views
TMC1414/TMC1413 Introduction to Programming. Lecture 04: Control Structure. Objective. In this topic, you will learn about: Selection Structures if if-else Repetition control structures For while do..while statement Switch multiple selection statement
E N D
TMC1414/TMC1413Introduction to Programming Lecture 04: Control Structure
Objective • In this topic, you will learn about: • Selection Structures • if • if-else • Repetition control structures • For • while • do..while statement • Switch multiple selection statement • Break Vs continue statement • Structured Programming
Input & Output For example: int main() { int age; printf(“What is your age?”); scanf(“%d”, &age); printf(“your age is %d”, age); return 0; }
What if your program have more than two ways to perform? • For example: If age 18 and above is adult, while younger than 18 is minor. • From the program in previous slide, how can you code your program to perform in a way to determine you as adult or minor based on your input?
The if Selection Statement • Selection structure: • Used to choose among alternative courses of action • Pseudocode: If student’s grade is greater than or equal to 60Print “Passed” • If condition true • Print statement executed and program goes on to next statement • If false, print statement is ignored and the program goes onto the next statement
The if Selection Statement • Pseudocode statement in C: if ( grade >= 60 ) printf( "Passed\n" ); • C code corresponds closely to the pseudocode • 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
The if Selection Statement • if statement is a single-entry/single-exit structure
The if Selection Statement • if • Only performs an action if the condition is true • if…else • Specifies an action to be performed both when the condition is true and when it is false • Psuedocode: If student’s grade is greater than or equal to 60Print “Passed” elsePrint “Failed” • Note spacing/indentation conventions
The if Selection Statement • C code: if ( grade >= 60 ) printf( "Passed\n"); else printf( "Failed\n");
The if Selection Statement • Flow chart of the if…else selection statement • Nested if…else statements • Test for multiple cases by placing if…else selection statements inside if…else selection statement • Once condition is met, rest of statements skipped • Deep indentation usually not used in practice
The if Selection Statement • Pseudocode for a nested if…else statement If student’s grade is greater than or equal to 90 Print “A”else If student’s grade is greater than or equal to 80 Print “B” else If student’s grade is greater than or equal to 70 Print “C” else If student’s grade is greater than or equal to 60 Print “D” else Print “F”
The if Selection Statement • Compound statement: • Set of statements within a pair of braces • Example: if ( grade >= 60 ) printf( "Passed.\n" ); else { printf( "Failed.\n" ); printf( "You must take this course again.\n" );} • Without the braces, the statement printf( "You must take this course again.\n" ); would be executed automatically
Symbol Operator Name == Equal to != Not equal to < Less than <= Less than or equal to > Greater than >= Greater than or equal to Control Structure Using Relational Operators in Conditional Expressions Conditional expressions are formed using relational and logical operators. Relational operators are used to compare values. An expression that compares two expressions using relational operator is called a simple predicate. A simple predicate computes to either 1 (for true) or 0 (for false). C has six relational operators.
A == 1 • A = 2
Symbol Logical Name && Logical AND (conjunction) || Logical OR (disjunction) ! Logical NOT (negation) Control Structure Logical expression is one that computes to either 1 (for true) or 0 (for false). A simple predicate is a logical expression because it produces either a true or false result. • Logical AND • Returns true if both conditions are true. • Logical OR • -Returns true if either of its conditions are true. • Logical NOT • -Reverses the truth/false of its condition • -unary operator, has one operand
Truth Table for the || (Logical OR) Operator Truth Table for the && (Logical AND) Operator operand_1 operand_1 operand_2 operand_2 operand_1 && operand_2 operand_1 || operand_2 true true true true true true true true false false true false false false true true false true false false false false false false Truth Table for the ! (Logical NOT) Operator operand_1 ! (operand_1) true false false true Control Structure Example: ((X < 1) && (Y > =1)) Example: !(X < 1) == (X> =1) Example: ((X < 1) || (Y > =1))
Multiple Operators: Precedence of Logical Operators A precedence rule states the order in which different operators are applied. An associativity rule specifies the order in which multiple occurrence of the same operator are applied.
The switch Multiple-Selection Statement • switch • Useful when a variable or expression is tested for all the values it can assume and different actions are taken • Format • Series of case labels and an optional default case switch ( value ){ case '1': actions case '2': actions default: actions } • break; exits from statement
The switch Multiple-Selection Statement • Flowchart of the switch statement
The break and continue Statements • break • Causes immediate exit from a while, for, do…while or switch statement • Program execution continues with the first statement after the structure • Common uses of the break statement • Escape early from a loop • Skip the remainder of a switch statement
The break and continue Statements • continue • Skips the remaining statements in the body of a while, for or do…while statement • Proceeds with the next iteration of the loop • while and do…while • Loop-continuation test is evaluated immediately after the continue statement is executed • for • Increment expression is executed, then the loop-continuation test is evaluated
continue skips to end of for loop and performs next iteration
If…else statement • Let say your schedule is as below
int main() { string day; printf(“What is today?\n”); scanf(“%s”, &day); if (day == “Monday”) { printf(“TMC1413”); } else if (day == “Tuesday”) { printf(“TMC1233); }else if (day == “Wednesday”) { printf(“TMX1011”) } return 0 }
case “Saturday”: { printf(“Badminton”); break } default: { printf (“sleep”); break; } } return 0; } int main() { string day; printf(“What is today?\n”); scanf(“%s”, &day); switch (day){ case “Monday”: { printf(“TMC1413”); break; } case “Tuesday”: { printf(“TMC1233”); break; }….
Repetition Statement (Loops)
Repetition Statements • You can make decision with your program. • Another problem arise when you want repeating doing one operation. • For example: create a program to print 1 to 10.
Control Structure Repetition Statements • Every programming language has some constructs that can be used to define controlled repetitions (or loops) on a program block. • A program block that is executed repeatedly by a repetition statement is called its loop body. • The repetition is controlled by a condition that is associated with the repetition statement. • There are two types of repetition statements: • pretest repetition statements • posttest repetition statements
true predicate loop body false Control Structure A pretest repetition statement computes a value for its associated predicate before entering the loop body and will execute the loop body as long as the predicate is true. Once the predicate computes to false, repetition stops and the program control passes to the next statement that follows the repetition statement. Flowchart
true predicate loop body false Control Structure A posttest repetition statementfirst executes the loop body and then computes the predicate. If the predicate is true, the loop body is executed again; otherwise, the repetition terminates. Flowchart
Control Structure There are three statements for repetitions: while statement; for statement; do-while statement; a pretest repetition statement a posttest repetition statement Using while Statements enclosed in parenthesis while (Expression) Statement; /*end while*/ loop body comment: to show termination Example while (++counter <= 10) { printf (“%d\n”, counter); /*end while*/
The Essentials of Repetition • Loop • Group of instructions computer executes repeatedly while some condition remains true • Counter-controlled repetition • Definite repetition: know how many times loop will execute • Control variable used to count repetitions • Sentinel-controlled repetition • Indefinite repetition • Used when number of repetitions not known • Sentinel value indicates "end of data"
The for Repetition Statement • Format when using for loops for ( initialization; loopContinuationTest; increment ) statement • Example: for( int counter = 1; counter <= 10; counter++ ) printf( "%d\n", counter ); • Prints the integers from one to ten No semicolon (;) after last expression
The for Repetition Statement • For loops can usually be rewritten as while loops: initialization;while( loopContinuationTest ) { statement; increment;} • Initialization and increment • Can be comma-separated lists • Example: for (int i = 0, j = 0; j + i <= 10; j++, i++) printf( "%d\n", j + i );
The for Statement : Notes and Observations • Arithmetic expressions • Initialization, loop-continuation, and increment can contain arithmetic expressions. If x equals 2 and y equals 10 for ( j = x; j <= 4 * x * y; j += y / x ) is equivalent to for ( j = 2; j <= 80; j += 5 ) • Notes about the for statement: • "Increment" may be negative (decrement) • If the loop continuation condition is initially false • The body of the for statement is not performed • Control proceeds with the next statement after the for statement • Control variable • Often printed or used inside for body, but not necessary
Note that number has a different value each time this statement is executed
The while Repetition Statement • Repetition structure • Programmer specifies an action to be repeated while some condition remains true • Psuedocode: While there are more items on my shopping list Purchase next item and cross it off my list • while loop repeated until condition becomes false
The while Repetition Statement • Example: int product = 2; while ( product <= 1000 ) product = 2 * product; • Not providing the body of a while statement with an action that eventually causes the condition in the while to become false. Normally, such a repetition structure will never terminate—an error called an “infinite loop.”
The do…while Repetition Statement • The do…while repetition statement • Similar to the while structure • Condition for repetition tested after the body of the loop is performed • All actions are performed at least once • Format: do { statement; } while (condition );
The do…while Repetition Statement • Example (letting counter = 1): do { printf( "%d ", counter ); } while (++counter <= 10); • Prints the integers from 1 to 10
The do…while Repetition Statement • Flowchart of the do…while repetition statement
increments counter then checks if it is less than or equal to 10
C’s single-entry/single-exit sequence, selection and repetition statements.
References Problem Solving using C, Uckan, Yuksel, McGraw Hill, 1999. C How to Program, Deitel&Deitel, Prentice-Hall, 6thEdition, 2010.
Q & A • Any Question? • Can you write an infinity loop using for/while/do-while statements?