28
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)

Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute

Embed Size (px)

Citation preview

Page 1: Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute

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.

Page 2: Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute

ifFORMAT:

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.

Page 3: Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute

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.

Page 4: Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute

Example 1

<html>

<head><title>Conditional Flow Control</title></head>

<body><script language=javascript><!-- Hiding JavaScript from old browsers

document.write("<h3>");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("</h3>");//-->

</script></body>

</html>

Page 5: Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute

if/else ifFORMAT:

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.

Page 6: Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute

Example 2<html>

<head><title>Conditional Flow Control</title></head>

<body><script language=javascript><!-- Hiding JavaScript from old browsers

document.write("<h2>");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("</h2>");//-->

</script></body>

</html>

Page 7: Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute

SwitchFormatswitch (expression) {case label :

statement(s);break;

case label :statement(s);break;…

default: statement;}

Page 8: Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute

Example 3<html>

<head><title>The Switch Statement</title></head>

<body><script language=javascript><!--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;

} //--></script></body>

</html>

Page 9: Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute

Switch Structure (CASE OF) – Fuzzy Dice Program

• <script>

• 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";• die +="| * |\n";• die +="|--------|\n";• break;

Page 10: Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute

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";• die +="| * * |\n";• die +="|--------|\n";• break;• default:• die = "ERROR!";• }

• alert (die);

• </script>

Page 11: Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute

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

Page 12: Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute

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.

Page 13: Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute

Example 4<html>

<head><title>Looping Constructs</title><h2>While Loop</h2><script language="JavaScript"> document.write("<font size='+2'>"); var i=0; // Initialize loop counter while ( i < 10 ){ // Test document.writeln(i); i++; // Increment the counter } // end of loop block</script></head>

<body></body>

</html>

Page 14: Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute

The Joke Teller Program

<script>

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…”);}

}

</script>

Page 15: Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute

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

Page 16: Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute

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.

Page 17: Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute

Example 5<html>

<head><title>Looping Constructs</title><h2>Do While Loop</h2><script language="JavaScript"> document.write("<font size='+2'>"); var i=0; do{

document.writeln(i);i++;

} while ( i < 10 )</script></head>

<body></body>

</html>

Page 18: Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute

forFORMAT:

for(expression1; expression2; expression3){

statement(s);

}

for(initialize; test; inc/dec) {

statement(s);

}

Page 19: Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute

Example 6<html>

<head><title>Looping Constructs</title><h2>For Loop</h2><script language="JavaScript"> document.write("<font size='+2'>"); for( var i = 0; i < 10; i++ ){ document.writeln(i); }</script></head>

<body></body>

</html>

Page 20: Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute

Creating the Racer Program

<script>

var lap = 0;

for (lap = 1; lap <= 10; lap++){

alert (“Now on lap: “+ lap);}

</script>

Page 21: Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute

Skipping Values – Count By Five Program

<script>

var i = 0;

for (i = 5; i <= 100; i += 5){

alert (i);}

</script>

… and so on till …

Page 22: Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute

Counting Backwards

<script>

var i = 0;

For (i = 10; i > 0; i--){

alert (i);}

</script>

Page 23: Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute

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.

Page 24: Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute

Example 7<html>

<head><title>Looping Constructs</title></head>

<body><h2>While Loop</h2><script language="JavaScript">document.write("<font size='+2'>");while(true) { var grade=eval(prompt("What was your grade? ","")); if (grade < 0 || grade > 100) {

document.write("Illegal choice<br>");continue; // go back to the top of the loop

} if(grade > 89 && grade < 101) {document.write("A<br>");}

else if (grade > 79 && grade < 90) {document.write("B<br>");}else if (grade > 69 && grade < 80) {document.write("C<br>");}else if (grade > 59 && grade < 70) {document.write("D<br>");}else {document.write("You Failed.<br>");}answer=prompt("Do you want to enter another grade? ","");if(answer != "yes"){ document.write("So long.<br>"); break; // break out of the loop}

</script></body>

</html>

Page 25: Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute

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.

Page 26: Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute

Example 8<html>

<head><title>Nested loops</title></head>

<body><script language=javascript> <!-- 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("<br>"); }//--></script></body>

</html>

Page 27: Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute

Example 9outer:

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. <br>”);

Page 28: Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute

LAB 6

Exercise 2 || 3 on page 110.