Você está na página 1de 27

ITEC 102

Week 4
Dr. M.A Qureshi

Last Week

Relational Operators
Logical Operators
Boolean Expressions
Conditional Statements
If Statement

Dr. M.A. Qureshi

This Week
Conditional Statements (Continued)
Switch Statement
Examples and Exercises

Dr. M.A. Qureshi

Recap
What is the value of the variable b after the
following code has run?
int b = 20;
int i = 3;
int j = 15;
if ((i >= 7) || (j < 0))
b = b + j;
Dr. M.A. Qureshi

Recap
What is the value of the variable b after
the following code has run?
int b = 10;
int i = 6;
int j = 9;
if ((i >= 7) || (j > 10))
b = b + j;
else
b = b + 1;
Dr. M.A. Qureshi

Exercise

Write a program:

Draw a white background


Draw horizontal and vertical lines down the center
If mouse is in top left, draw black rectangle in that quadrant
If mouse is in top right, draw black rectangle in that quadrant
If mouse is in bottom left, draw black rectangle in that quadrant
If mouse is in bottom right, draw black rectangle in that quadrant

But how can we tell which quadrant it is in?

Dr. M.A. Qureshi

Upper Left: x = 0 to 99, y = 0 to 99


Upper right: x = 100 to 199, y = 0 to 99

//fourSquare.pde

Nested if
Write a processing program to show the output as
//circleConstrainedInABoxv2InProgress.pde

Dr. M.A. Qureshi

Issues with if Statement


int numberOfStudentsInClass =
5*(int)random(1,11);
// numberOfStudentsInClass is 5 or 10 or
15 or 50
if(numberOfStudentsInClass == 5 )
do something;
if(numberOfStudentsInClass == 10 )
do something;

if(numberOfStudentsInClass == 50 )
do something; //longIf.pde
Dr. M.A. Qureshi

ISSUES WITH IF
A single variable (numberOfStudentsInClass)
is being checked for equality with multiple
options. Hence, it is not efficient to repeat the
variable name many times..
To overcome this problem, we have a switch
statement

Dr. M.A. Qureshi

Switch Statement
A switch statement takes different possible courses based on the
value of a single variable (of type int or char)
switch ( variableName ) //value determines
entry-point
{
case x:
statement_x_1;

<break;>
case y:
statement_y_1;

<break;>
<default>: statement_default_1;

}
Dr. M.A. Qureshi

Switch
Based on the value of the variable,
corresponding section of the switch block is
entered, and the switch block is executed,

a. either till the end, or,

b. until a break statement occurs


NOTE: Default case is optional and so are
break statements
Dr. M.A. Qureshi

Switch example
char gender = 'm';

switch(gender) {
case 'm':
println("hello mr.");
break;
case 'f':
println("hello ms.");
break;
default:
println("hello!");
}
//switchGender.pde

Dr. M.A. Qureshi

Need for break


char gender = 'm';
switch(gender) {
case 'm':
println("hello mr.");
case 'f':
println("hello ms.");
default:
println("hello!");
}
Output: hello mr.
hello ms.
hello!.
Dr. M.A. Qureshi

Switch Example
switch ( x )
{
case 1:
case 7:
case 8:

y = 1;
y = 4;
y = 2;
break;
case 9:
y = 3;
break;
case 2:
y = 5;
default: y = 6;

}
Dr. M.A. Qureshi

x=1
// blue - entry point, red executed statements
switch ( x )
{

case 1:
y = 1;

case 7:
y = 4;

case 8:
y = 2;

break;

case 9:
y = 3;

break;

case 2:
y = 5;

default:
y = 6;

} //for x = 1, y is 2

Dr. M.A. Qureshi

x=7
// blue - entry point, red executed statements
switch ( x )
{

case 1:
y = 1;

case 7:
y = 4;

case 8:
y = 2;

break;

case 9:
y = 3;

break;

case 2:
y = 5;

default:
y = 6;

} //for x = 7, y is 2

Dr. M.A. Qureshi

x=8
// blue - entry point, red executed statements
switch ( x )
{

case 1:
y = 1;

case 7:
y = 4;

case 8:
y = 2;

break;

case 9:
y = 3;

break;

case 2:
y = 5;

default:
y = 6;

} //for x = 8, y is 2

Dr. M.A. Qureshi

x=9
// blue - entry point, red executed statements
switch ( x )
{

case 1:
y = 1;

case 7:
y = 4;

case 8:
y = 2;

break;

case 9:
y = 3;

break;

case 2:
y = 5;

default:
y = 6;

} //for x = 9, y is 3

Dr. M.A. Qureshi

x=2
// blue - entry point, red executed statements
switch ( x )
{

case 1:
y = 1;

case 7:
y = 4;

case 8:
y = 2;

break;

case 9:
y = 3;

break;

case 2:
y = 5;

default:
y = 6;

} //for x = 2, y is 6

Dr. M.A. Qureshi

Any value except 1/2/7/8/9


// blue - entry point, red executed statements
switch ( x )
{

case 1:
y = 1;

case 7:
y = 4;

case 8:
y = 2;

break;

case 9:
y = 3;

break;

case 2:
y = 5;

default:
y = 6;

} //for x = value other than 1/2/7/8/9, y is 6

Dr. M.A. Qureshi

Switch example summary


switch ( x )
{
case 1:
case 7:
case 8:

y = 1;
y = 4;
y = 2;
break;
case 9:
y = 3;
break;
case 2:
y = 5;
default: y = 6;

}
Dr. M.A. Qureshi

any other value

Switch Example
int monthNumber = (int)random(1,13);
String monthName = "";
switch ( monthNumber )
{
case 1:
monthName = "January";
break;
case 2:
monthName = "February";
break;

case 12:

monthName = "December";
break;

}
Dr. M.A. Qureshi

Switch Example 2
Write a program to generate a monthNumber
randomly.
Declare a String variable season.
Assign the following values to season based on
the month number.
Summer
Month 12, 1 or 2

Autumn
Month 3, 4 or 5

Winter
Month 6, 7 or 8

Spring
Moth 9,10 or 11

Dr. M.A. Qureshi

Switch Example 2
int monthNumber = (int)random(1,13);
String season = "";
switch ( monthNumber )
{
case 12:
case 1:
case 2: season = "Summer";
break;
case 3:
case 4:
case 5: season = "Autumn";
break;

Dr. M.A. Qureshi

Convert into switch statement


int choice = 0;
if(choice == 1)
{
println("You selected 1.");
}
else if(choice == 2 || choice == 3)
{
println("You selected 2 or 3.");
}
else if(choice == 4)
{
println("You selected 4.");
}
else
{
println("Please enter a choice between 1-4.");
}
Dr. M.A. Qureshi
}

Convert into if statement.


char option;
int a;
switch(option)
{
case 'a':
case 'A': a=20+10;
print("Addition process result +a);
break;
case 'b':
case 'B': a=20-10;
print("Subtraction process resul +a);
break;
case 'c':
case 'C': a=20*10;
print("Multiplication process result +a);
break;
case 'd':
case 'D': a=20/10;
print("Division process result +a);
break;
Dr. M.A. option");
Qureshi
default: print("Invalid

Dr. M.A. Qureshi

Você também pode gostar