IT CS 200: C ONDITION Lect. Napat Amphaiphan
T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2
Overview (Midterm) Midterm Examination 30 % Data Type, Declaration, Display Interactive Input SelectionRepetition Analyze Problem Solving Problem Solving int float double char printf() int float double char printf() scanf() if…else switch…case if…else switch…case while statement for statement do-while statement while statement for statement do-while statement
IF-ELSE StatementsIF-ELSE Statements 1 Switch StatementSwitch Statement 2 Today’s Overview 4
Introduction Flow of control refers to the order in which a program’s statements are executed Any algorithm can be built using combinations of four standardized flow of control structures: – Normal flow of control for all programs is sequential – Selection is used to select which statements are performed next based on a condition – Repetition is used to repeat a set of statements – Invocation is used to invoke a sequence of instructions using a single statement, as in calling a function
Relational Expressions Simplest decision structure: if (condition) statement executed if condition is true – The condition is evaluated to determine its numerical value, which is interpreted as either true (non-zero) or false (0) – If condition is “true” the statement following the if is executed; otherwise, statement is not executed The condition used in all of C’s if statements can be any valid C expression – Most commonly, a relational expression (can yield only 0 or 1)
Relational Expressions (cont.) Relational expressions are also known as conditions A relational expression evaluates to 1 (true) or 0 (false) – The expression 3 < 4 has a value of 1 – The expression 2.0 > 3.3 has a value of 0 – The value of hours > 0 depends on the value of hours Character data can also be compared using relational operators
Relational Expressions (cont.)
Relational expressions are evaluated to yield a numerical result – 0 interpreted as false – Nonzero interpreted as true Example printf(“The value of 3 < 4 is %d”, 3 < 4); printf(“\nThe value of 2.0 > 3.3 is %d”, 2.0 > 3.3); The value of 3 < 4 is 1 The value of 2.0 > 3.3 is 0
Logical Operators Can be called “Boolean Algebra” More complex conditions can be created using the logical operations AND (&&), OR (||), and NOT (!) Expression one (Logical Operators) Expression two
Logical Operators (cont.)
int i = 15, j = 30; double a = 12.0, b = 2.0, complete = 0.0;
Logical Operators (cont.)
char key = 'm'; int i = 5, j = 7, k = 12; double x = 22.5;
The if Statement The structure of an if statement is as follows: if ( statement is TRUE ) Execute this line of code Here is a simple example that shows the syntax: if ( 5 < 10 ) printf( "Five is now less than ten, that's a big surprise" );
The if-else Statement Sometimes when the condition in an if statement evaluates to false, it would be nice to execute some code The "else" statement effectively says that whatever code after it (whether a single line or code between brackets) is executed if the if statement is FALSE. 20
The if-else Statement general form if (expression) statement1; else statement2; – If value of expression is nonzero, statement1 is executed – If value of expression is zero, statement2 is executed
The if-else Statement (cont.)
The if-else Statement - Examples Problems: Print the result of ‘pass’ or ‘fail’ base on the student score (score >= 50 then pass) int score; scanf("%d",&score); if (score >= 50) printf("You have passed the exam >.<"); else printf("You have failed the exam T_T"); int score; scanf("%d",&score); if (score >= 50) printf("You have passed the exam >.<"); else printf("You have failed the exam T_T");
Compound Statements To have more than one statement execute after an if statement that evaluates to true, use braces { }
Compound Statements (cont.) Problem: Check if the input type is circle then calculate the area = pi * r * r if (type == ‘c’) { area = pi * r * r; // Statement1 printf(“The area of circle is: %d”, area); //Statement2 }
Problems Associated with the if-else Statement Misunderstanding the full implications of what an expression is Using the assignment operator (=), in place of the relational operator (==) Example age = 18; printf(“\nThe value of the first expression is %d”, age + 5); printf(“\nThe value of the second expression is %d”, age = 30); printf(“\nThe value of the third expression is %d”, age == 40); The value of the first expression is 23 (arithmetic operator) The value of the second expression is 30 (assignment operator) The value of the third expression is 0 (relational operator)
Nested if Statements One or more if-else statements can be included within either part of if-else statement Example if (condition1) { if (condition2) printf(“snap”); } else printf(“pop”);
The if-else Chain (Else-IF) General form if (expression1) statement1; else if (expression2) statement2; else if (expression3) statement3;. else if (expressionn) statementn; else lastStatement; Another use of else is when there are multiple conditional statements that may all evaluate to true,
Example if-else chain: display a person’s marital status corresponding to a letter input
Example
The switch Statement Terminated with a colon default is optional If the break statement was omitted, the following case would be executed
The switch Statement (cont.)
Summary Relational Expression Logical Operators if…else statement switch…case statement
? || // 36