Engineering Science 1036A/B Lecture Notes - Lecture 10: Switch Statement, Semicolon, Leap Year
if Statements
A single statement is executed if expression is true
if(expression)
statement;
○
if (expression) statement;
○
•
Statements inside { } are executed if expression is true
The { } make all statements appear as a single statement
○
○
•
A semicolon at the end of an if clause is a logical error
○
•
Using assignment operator (=) in place of an equality operator (==) in an if-
expression is an error
compilation error (easy to detect): if (num%2 = 0)
Left side of assignment operator is not a writable memory location
§
○
logical error (hard to detect): if (num = 0)
Left side of assignment operator is a writable memory location
§
○
•
if(0 <= n && n <= 100) cannot be written as if(0 <= n <= 100)
The operator <= associates from left to right
○
○
•
if(x == 5 || x == 100) cannot be written as if(x == 5 || 100)
The operator == has the higher priority than ||:
○
○
•
Expressions
If-expression is always evaluated logically as either true (1) or false (0)•
The expression in the if statement can use two types of operators:
Comparison
Relational (>, <, >=, <=)
§
Equality (==, !=)
§
○
Logical (&&, ||, !)
○
•
The expression can be a combination of more than one nested expression or it
can be just a single variable without any operator
•
•
if-else Statement
Write a program that prompts the user to enter three integers and finds the
maximum of the three
Write a program that lets the user enter a year and checks whether it is a leap
year
A year is a leap year if it is divisible by 4 but not by 100 or if it is divisible
by 400
○
Nested if statement
If-statements can appear in the body of another if statement•
if-else-if Statement
switch Statement
switch-expression must result an integer or character and always be inside
a bracket
•
Keyword case must be followed by a literal which must have the same data
type as the value of the switch-expression
•
Resulting statements in the case statements are executed when the value in the
case statement matches the value of the switch-expression
•
Keyword break should be used at the end of each case to terminate remainder
of the switch statement
If break statement is not present, the next case statement will be
executed along with the chosen one
○
•
The default case, can be used to perform actions when none of the specified
cases matches the switch-expression
•
No block structure is required inside the case statement, if you don’t declare
any variable inside the case
○
•
Case labels can be in any order•
Enter month number to print the name of the month
Modify the following code using switch statement
Switch structure with character value
Selection Structures
if Statements
A single statement is executed if expression is true
if(expression)
statement;
○
if (expression) statement;
○
•
Statements inside { } are executed if expression is true
The { } make all statements appear as a single statement
○
○
•
A semicolon at the end of an if clause is a logical error
○
•
Using assignment operator (=) in place of an equality operator (==) in an if-
expression is an error
compilation error (easy to detect): if (num%2 = 0)
Left side of assignment operator is not a writable memory location
§
○
logical error (hard to detect): if (num = 0)
Left side of assignment operator is a writable memory location
§
○
•
if(0 <= n && n <= 100) cannot be written as if(0 <= n <= 100)
The operator <= associates from left to right
○
○
•
if(x == 5 || x == 100) cannot be written as if(x == 5 || 100)
The operator == has the higher priority than ||:
○
○
•
Expressions
If-expression is always evaluated logically as either true (1) or false (0)•
The expression in the if statement can use two types of operators:
Comparison
Relational (>, <, >=, <=)
§
Equality (==, !=)
§
○
Logical (&&, ||, !)
○
•
The expression can be a combination of more than one nested expression or it
can be just a single variable without any operator
•
•
if-else Statement
Write a program that prompts the user to enter three integers and finds the
maximum of the three
Write a program that lets the user enter a year and checks whether it is a leap
year
A year is a leap year if it is divisible by 4 but not by 100 or if it is divisible
by 400
○
Nested if statement
If-statements can appear in the body of another if statement•
if-else-if Statement
switch Statement
switch-expression must result an integer or character and always be inside
a bracket
•
Keyword case must be followed by a literal which must have the same data
type as the value of the switch-expression
•
Resulting statements in the case statements are executed when the value in the
case statement matches the value of the switch-expression
•
Keyword break should be used at the end of each case to terminate remainder
of the switch statement
If break statement is not present, the next case statement will be
executed along with the chosen one
○
•
The default case, can be used to perform actions when none of the specified
cases matches the switch-expression
•
No block structure is required inside the case statement, if you don’t declare
any variable inside the case
○
•
Case labels can be in any order•
Enter month number to print the name of the month
Modify the following code using switch statement
Switch structure with character value
Selection Structures
if Statements
A single statement is executed if expression is true
if(expression)
statement;
○
if (expression) statement;
○
•
Statements inside { } are executed if expression is true
The { } make all statements appear as a single statement
○
○
•
A semicolon at the end of an if clause is a logical error
○
•
Using assignment operator (=) in place of an equality operator (==) in an if-
expression is an error
compilation error (easy to detect): if (num%2 = 0)
Left side of assignment operator is not a writable memory location
§
○
logical error (hard to detect): if (num = 0)
Left side of assignment operator is a writable memory location
§
○
•
if(0 <= n && n <= 100) cannot be written as if(0 <= n <= 100)
The operator <= associates from left to right
○
○
•
if(x == 5 || x == 100) cannot be written as if(x == 5 || 100)
The operator == has the higher priority than ||:
○
○
•
Expressions
If-expression is always evaluated logically as either true (1) or false (0)
•
The expression in the if statement can use two types of operators:
Comparison
Relational (>, <, >=, <=)
§
Equality (==, !=)
§
○
Logical (&&, ||, !)
○
•
The expression can be a combination of more than one nested expression or it
can be just a single variable without any operator
•
•
if-else Statement
Write a program that prompts the user to enter three integers and finds the
maximum of the three
Write a program that lets the user enter a year and checks whether it is a leap
year
A year is a leap year if it is divisible by 4 but not by 100 or if it is divisible
by 400
○
Nested if statement
If-statements can appear in the body of another if statement•
if-else-if Statement
switch Statement
switch-expression must result an integer or character and always be inside
a bracket
•
Keyword case must be followed by a literal which must have the same data
type as the value of the switch-expression
•
Resulting statements in the case statements are executed when the value in the
case statement matches the value of the switch-expression
•
Keyword break should be used at the end of each case to terminate remainder
of the switch statement
If break statement is not present, the next case statement will be
executed along with the chosen one
○
•
The default case, can be used to perform actions when none of the specified
cases matches the switch-expression
•
No block structure is required inside the case statement, if you don’t declare
any variable inside the case
○
•
Case labels can be in any order•
Enter month number to print the name of the month
Modify the following code using switch statement
Switch structure with character value
Selection Structures
Document Summary
A single statement is executed if expression is true if(expression) statement; if (expression) statement; Statements inside { } are executed if expression is true. The { } make all statements appear as a single statement. A semicolon at the end of an if clause is a logical error. Using assignment operator (=) in place of an equality operator (==) in an if- expression is an error compilation error (easy to detect): if (num%2 = 0) Left side of assignment operator is not a writable memory location logical error (hard to detect): if (num = 0) Left side of assignment operator is a writable memory location if(0 <= n && n <= 100) cannot be written as if(0 <= n <= 100) The operator <= associates from left to right if(x == 5 || x == 100) cannot be written as if(x == 5 || 100) The operator == has the higher priority than ||: