Download presentation
Presentation is loading. Please wait.
Published byBrandon Greer Modified over 8 years ago
1
Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute a sequence of statements. - branch to an alternative sequence, based on a test. - repeat a sequence until some condition is met. The decision-making constructs (if, if/else, if/else if, switch) contain a control expression that determines whether a block of statements will be executed. The looping constructs (while, for) allow the program to execute a statement block repetitively until some condition is satisfied. Compound statements or blocks are group of statements surrounded by curly braces.
2
if FORMAT: if (condition) { statements; } The block of statements is executed if the condition after the “if” is met. If the result is false, the block is not executed.
3
if/else FORMAT: if(condition){ statements1; } else{ statements2; } The block of statements1 is executed if the condition after the “if” is met. If the result is false, the block of statements2 is executed.
4
Example 1 Conditional Flow Control <!-- Hiding JavaScript from old browsers document.write(" "); var age=prompt("How old are you? ",""); if( age >= 55 ){ document.write("You pay the senior fare! "); } else{ document.write("You pay the regular adult fare. "); } document.write(" "); //-->
5
if/else if FORMAT: if(condition){ statements1; } else if (condition){ statements2; } else if (condition){ statements3; } else{ statements4; } The block of statements1 is executed if the condition after the “if” is met. If the result is false, the condition after the first else if is tested. If it is true, block of statements2 is executed. If it is false, the condition after the second else if is tested. If it is true, block of statements3 is executed. If it is false, statements4 is executed.
6
Example 2 Conditional Flow Control <!-- Hiding JavaScript from old browsers document.write(" "); var age=prompt("How old are you? ",""); if( age > 0 && age <=12){ document.write("You pay the child’s fare! "); } else if( age > 12 && age < 60){ document.write("You pay the regular adult fare! "); } else { document.write("You pay the senior fare. "); } document.write(" "); //-->
7
Switch Format switch (expression) { case label : statement(s); break; case label : statement(s); break; … default: statement; }
8
Example 3 The Switch Statement <!-- var color=prompt("What is your color?",""); switch(color){ case "red": document.bgColor="color"; document.write("Red is hot."); break; case "yellow": document.bgColor=color; document.write("Yellow is warm."); break; case "green": document.bgColor="lightgreen"; document.write("Green is soothing."); break; case "blue": document.bgColor="#RRGGBB"; document.write("Blue is cool."); break; default: document.bgColor="white"; document.write("Not available today. We'll use white"); break; } //-->
9
Switch Structure (CASE OF) – Fuzzy Dice Program var roll = 0; var die = ""; roll = Math.floor(Math.random()*6) + 1; switch (roll) { case 1: die = "|--------|\n"; die +="| |\n"; die +="| * |\n"; die +="| |\n"; die +="|--------|\n"; break; case 2: die = "|--------|\n"; die +="| * |\n"; die +="| |\n"; die +="| * |\n"; die +="|--------|\n"; break; case 3: die = "|--------|\n"; die +="| * |\n"; die +="|--------|\n"; break;
10
Switch Structure (CASE OF) – Fuzzy Dice Program case 4: die = "|--------|\n"; die +="| * * |\n"; die +="| |\n"; die +="| * * |\n"; die +="|--------|\n"; break; case 5: die = "|--------|\n"; die +="| * * |\n"; die +="| * |\n"; die +="| * * |\n"; die +="|--------|\n"; break; case 6: die = "|--------|\n"; die +="| * * |\n"; die +="|--------|\n"; break; default: die = "ERROR!"; } alert (die);
11
Loops Loops are used to execute a segment of code repeatedly until some condition is met. The basic looping constructors are: while for do/while
12
While FORMAT: while (condition) { statements; inc/dec counter; } The statements inside the curly braces are executed as long as the expression after the while is true.
13
Example 4 Looping Constructs While Loop document.write(" "); var i=0; // Initialize loop counter while ( i < 10 ){ // Test document.writeln(i); i++; // Increment the counter } // end of loop block
14
The Joke Teller Program var correct = “to get to the other side”; var guess = “ “; While (guess != correct) { guess = prompt(“Why did the chicken cross the road ?”); if guess == correct) { alert (“Pretty funny, huh? "); } else { alert(“That’s not it…”); }
15
Recognizing Loops that never execute Var I = 10; While (i<10) { Alert (i); i++; } Recognizing Endless Loops Var I = 0; While (i<10) { Alert (i); } The variable was not initialized properly. There is no code to increment the variable
16
do/while Format: do{ statements; } while (condition); This loop necessarily executes the statements in the body of the loop at least once before testing its condition.
17
Example 5 Looping Constructs Do While Loop document.write(" "); var i=0; do{ document.writeln(i); i++; } while ( i < 10 )
18
for FORMAT: for(expression1; expression2; expression3){ statement(s); } for(initialize; test; inc/dec) { statement(s); }
19
Example 6 Looping Constructs For Loop document.write(" "); for( var i = 0; i < 10; i++ ){ document.writeln(i); }
20
Creating the Racer Program var lap = 0; for (lap = 1; lap <= 10; lap++) { alert (“Now on lap: “+ lap); }
21
Skipping Values – Count By Five Program var i = 0; for (i = 5; i <= 100; i += 5) { alert (i); } … and so on till …
22
Counting Backwards var i = 0; For (i = 10; i > 0; i--) { alert (i); }
23
break - continue break; - is used to break out of a loop early. Exits the loop to the next statement after the closing curly brace of the loop’s statement block. continue; - is used to return to the testing condition early. Sends loop control to the top of the loop and re-evaluates the loop condition.
24
Example 7 Looping Constructs While Loop document.write(" "); while(true) { var grade=eval(prompt("What was your grade? ","")); if (grade 100) { document.write("Illegal choice "); continue; // go back to the top of the loop } if(grade > 89 && grade ");} else if (grade > 79 && grade ");} else if (grade > 69 && grade ");} else if (grade > 59 && grade ");} else {document.write("You Failed. ");} answer=prompt("Do you want to enter another grade? ",""); if(answer != "yes"){ document.write("So long. "); break; // break out of the loop }
25
Nested Loops and Labels A loop within a loop is called a nested loop. A common use is to display data in rows and columns. Labels are identifiers followed by a colon and placed on a line by themselves. They are used if you want to branch to some other part of the program.
26
Example 8 Nested loops <!-- Hiding JavaScript from old browsers var str = "@"; for (var row = 0; row < 6; row++){ for (var col = 0; col < row; col++){document.write(str);} document.write(" "); } //-->
27
Example 9 outer: while (condition1){ statement(s) middle: while (condition2){ if(expression is true) {break outer;} } inner: for (i=0;i<20;i++){ if (expression is true) {continue outer;} } document.write (“Out of all loops. ”);
28
LAB 6 Exercise 2 || 3 on page 110.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.