Sie sind auf Seite 1von 19

ECE 321: Advanced

Computer Programming for ECE

by: Engr. Junard P. Kaquilala


Control Structure: Selection

Selection
Control Structures I: Selection
Selection
>> the program executes particular statements depending on one or more conditions

Logical
false Expression true

statement 2 statement 1

Boolean Operator
Boolean Operator

Operator Description
! logical NOT Operation
&& logical AND Operation
II logical OR Operation
Boolean Operation

x y x && y x II y !x

true true true true false

true false false true false

false true False true true

false false False false true

Relational Operator
Relational Operator

Operator Operation Example Meaning


== Equal a == b Is a equal to b ?
!= Not equal a != b Is a not equal to
b?
< Less than a < b Is a less than b?
> Greater than a > b Is a greater than
b?
<= Less than or a <= b Is a less than or
equal equal to b?
>= Greater than or a >= b Is a greater than
equal or equal to b?

Relational Operator: Example


Operator Precedence
Operator Precedence Associativity
!, +, - ( unary operators ) First Right to left

*, /, % Second Left to Right

+, - Third Left to Right

<, <=, >, >= Fourth Left to Right

==, != Fifth Left to Right

&& Sixth Left to Right

II Seventh Left to Right

= Last Left to Right

Operator Precednce Example


If Statement
The syntax for an If statement:

if (boolean - expression)
{
statement 1;
statement 2;
...
statement n;
}
Flow Execution of If Statement

Logical
Expression true

statement 1
false
If – Else Statement
The syntax for an If – Else statement:

if (boolean - expression)
{
statement – list - 1
}
else
{
statement – list - 2
}
Flow Execution of If – Else Statement

Logical
false Expression true

statement 2 statement 1
Nested If – Else Statement
 Syntax:
if (logical expression) {

if(logical expression 1)
statement – list – 1
else
statement – list – 2
}
else
{
if(logical expression 2)
statement – list – 3
else
statement – list – 4
}
Else – If Statement
 Syntax:
if(logical expression 1) {
statement – list – 1
}
else if(logical expression 2) {
statement – list -2
}
else if(logical expression 3) {
statement – list -3
}

else {
Statement – list – n
}
Switch Statement
switch statement
- a variation of the if statement is the switch statement,
which performs a multi-way branch instead of a simple
binary branch.

Syntax :
switch (expression) {
case value : statement(s);
break;
case value : statement(s);
break;
.......
default : statement(s);
break;
}
Flow Execution of Switch Statement

statement 1

Case Value 1
true statement 1 break

false

Case Value n
true statement n break

false

Default
statement
SAMPLE PROBLEM
1. Write a program that would accepts two
integers and output the greatest number.(Two
numbers must be different)

2. Write a program that would accept an integer


number and determine if the number is positive
even/odd number , or negative even/odd
number.
SAMPLE PROBLEM
3. Write a program that simulates the basic
operation of an ATM machine. It can withdraw,
deposit, and check balance. The user enter a
desired transaction number, and output the
corresponding transaction. Assume that the
user has an initial deposit of 5,000.
Transaction number:
Withdraw Cash = 1
Make a Deposit = 2
Check Balance = 3
Exit = 4
SAMPLE PROBLEM
4. A certain company gives COMMISSION to each of their
salesman based from the TYPE of appliance sold and the
PRICE of the appliance. Below are the given rates will be
used in computing for the salesman’s COMMISSION:
If TYPE is COMMISSION
1,3 25% of the PRICE
2,4 15% of the PRICE
5–7 30% of the PRICE
8 – 10 P5000.00, regardless of the price
Input the salesman’s Name, TYPE of appliance sold (from 1
- 10), and the PRICE of the appliance sold. Compute and
display the salesman’s COMMISSION.
QUIZ
Write a program that simulates a vending
machine. The machine holds six items
numbered 1 through 6, with prizes(in dollars)
1.25, 0.75, 0.90, 0.75, 1.50, and 0.75,
respectively. The input to your program is an
integer and a floating – point number
representing an item number and a sum of
money. If the money is enough to buy the item,
your program should print:
“Thank you for buying item X. Your change is Y.”
If the money inserted is insufficient, then your
program should say so.
QUIZ
In the casino version of the game craps, a player
rolls two dice. If he bets on the “don’t pass” line, he
-loses with a 7 or 11
-wins with 2 or 3
-neither wins nor loses with 12(and must
begin the game again)
-continues rolling with 4, 5, 6, 8, 9, or 10.
Write an application that generates two random
integers between 1 and 6 inclusive, and determines
whether or not the player wins, loses, starts over, or
keeps rolling.
Hint: To generate a random integer in the range 1 –
6 use the expression:
(int)(6*Math.random() + 1)

Das könnte Ihnen auch gefallen