520 likes | 568 Views
Learn about sequence, selection, and repetition methods in programming with examples and UML diagrams. Understand if, else, switch, loops, and altering control flow. Explore integer division and compound assignment operators.
E N D
Control Statements Ch 5, 6
Control Structures • Sequence • Statements executed one by one • { … } // block • Selection • One of several statements is selected • if (if…else), switch • Repetition (Loop) • Same statements executed multiple times • while, do…while, for (foreach)
Block • Several statements bracketed by { and } • Statements within a block executed sequentially • Treated as a single statement • Can be used wherever one statement is legal
Selection Statements • if • Single-selection • if…else • Double-selection • switch • Multiple-selection
ifExamples if(grade >= 60) Console.WriteLine("Passed");
UML Activity Diagram • Describes execution sequence
if…else Examples if(grade >= 60) Console.WriteLine("Passed"); else Console.WriteLine("Failed");
ifSyntax if (<boolean_expression>) <yes_statement>else <no_statement> • Each alternative is only ONEstatement! • To have multiple statements in either branch, use a block • else is optional
Boolean Expression • Gives a Boolean result (true or false) • A value of type bool bool isGraduate = true; • Comparison operation grade >= 60 • Logical operation grade >= 60 && isGraduate
Logical Operators • Combine multiple Boolean expressions VB C# Example in C# && grade >= 60 && isGraduate AND OR || m=="MIS" || m=="CS" NOT ! ! isGraduate
Nested if Statement if(grade >= 90) Console.WriteLine("A"); else if(grade >= 80) Console.WriteLine("B"); else if(grade >= 70) Console.WriteLine("C"); else Console.WriteLine("F");
Equivalent To // Nested if in a different form if(grade >= 90) Console.WriteLine("A"); else if(grade >= 80) Console.WriteLine("B"); else if(grade >= 70) Console.WriteLine("C"); else Console.WriteLine("F");
Not Equivalent To Multiple if if(grade >= 90) Console.WriteLine("A"); if(grade >= 80) Console.WriteLine("B"); if(grade >= 70) Console.WriteLine("C"); else Console.WriteLine("F");
More on switch • Needs a break before end of each case • The default is optional • Must use exact values in cases • Can't test ranges, such as >=93, 90 to 93 • Can every switch be translated into if? • Can every if be translated into switch?
Select Case in VB is more general! SELECT CASE grade CASE >= 93 letterGrade = "A" CASE 90 to 93 letterGrade = "A-" … CASE ELSE letterGrade = "F" END SELECT
Loop Statements • while, for (foreach) • Loop body executed ZERO or many times • do…while • Loop body executed ONE or many times
whileExample // find the first power of 3 // larger than 100 int product = 3; while (product <= 100) product = 3 * product;
Formulating Algorithms with Loops • Counter-controlled loop • Use a variable (called counter) to control the number of iterations • Sentinel-controlled loop • Use a special sentinel value to indicate "end of data entry"
Three Key Items of a Loop • Initialization • Set initial state of the data before the loop • Where to start • Loop Continuation Condition • Determine whether continue to next iteration • When to stop • Update • During each iteration, some data must change • How to approach the end
Infinite Loop • A loop that can never terminate • A common error • Carefully design the three key items to avoid it
for Loop for (initialization; loopContinuationCondition; increment) statement;
while and for • The previous for is equivalent to the following while, unless there is a continue initialization; while ( loopContinuationCondition ) { statement; increment; }
Exercise • Translate the previous while examples into for
do…while Loop • Similar to while • Tests loop-continuation after performing body of loop • Loop body executes at least once • Can the previous while examples be changed to do…while?
Alter the Flow of Control • break • Forces loop to exit immediately • continue • Forces loop to skip rest of loop body and start another iteration immediately
Summary • Control structures • Sequence, selection, repetition • Selection • if (if…else), switch • switch can't test ranges • Repetition (Loop) • while, for (foreach) • do…while must go thru the loop body at least once • Alter the flow of control • break, continue
Aside: Other Topics • Integer Division • Type Casting • Conditional Operator • Compound Assignment Operators • Increment and Decrement Operators
Integer Division • Results are integers. • Remainder discarded ! Not rounded ! 7 / 5→1, not 1.4, not 2. • % returns the remainder 7 % 5→2 • m / n * n + m % n→ m 7 / 5 * 5 + 7 % 5→7
Division in VB / C# VB C# Division (real) / / Division (integer) \ / Modulus MOD %
Division Example (by Instructor) Enter first real number: 7 Enter second real number: 5 x = 7, y = 5 x / y = 1.4 x / y * y = 7 Enter first integer: 7 Enter second integer: 5 m = 7, n = 5 m / n = 1 m % n = 2 m / n * n = 5 m / n * n + m % n = 7
Type Conversion • Explicit conversion • Explicitly request type conversion average = (double)total / gradeCounter; • Implicit conversion • Performed implicitly when needed • Promotion (from a smaller type to a larger type) only
Conditional Operator • The only ternary operator Console.WriteLine(grade >=60 ? "Passed": "Failed"); • A Boolean expression and two possible values • If the Boolean expression is true, take the first value; • Otherwise, take the second value
Compound Assignment • Shorthand of assignment expression