Escolar Documentos
Profissional Documentos
Cultura Documentos
Week 4
Dr. M.A Qureshi
Last Week
Relational Operators
Logical Operators
Boolean Expressions
Conditional Statements
If Statement
This Week
Conditional Statements (Continued)
Switch Statement
Examples and Exercises
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:
//fourSquare.pde
Nested if
Write a processing program to show the output as
//circleConstrainedInABoxv2InProgress.pde
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
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,
Switch example
char gender = 'm';
switch(gender) {
case 'm':
println("hello mr.");
break;
case 'f':
println("hello ms.");
break;
default:
println("hello!");
}
//switchGender.pde
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
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
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
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
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
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;
y = 1;
y = 4;
y = 2;
break;
case 9:
y = 3;
break;
case 2:
y = 5;
default: y = 6;
}
Dr. M.A. Qureshi
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
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;