0% found this document useful (0 votes)
109 views4 pages

Sekani Millette HW6: If (A 10) (B 0 C 1 )

This document contains code examples of if, if-else, nested if, and switch statements along with explanations of relational operators. It demonstrates using conditional logic to assign values to variables based on comparisons of other variable values and includes examples checking if values are within specified ranges. The document also asks short answer questions about risks of omitting else statements and how && and || operators work.

Uploaded by

Team Kapappies
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views4 pages

Sekani Millette HW6: If (A 10) (B 0 C 1 )

This document contains code examples of if, if-else, nested if, and switch statements along with explanations of relational operators. It demonstrates using conditional logic to assign values to variables based on comparisons of other variable values and includes examples checking if values are within specified ranges. The document also asks short answer questions about risks of omitting else statements and how && and || operators work.

Uploaded by

Team Kapappies
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Sekani Millette

HW 6

Checkpoint 3.6 - 3.19 (skip 3.7)


Algorithm Workbench: 6-10
Short Answer: 6-10

3.6 Write an if statement that assigns 0 to the variable b and assigns 1 to the variable c
if the variable a is less than 10.

if (a < 10)
{b = 0;
c = 1;
}

3.8 Write an if-else statement that assigns 20 to the variable y if the variable x is greater
than 100.
Otherwise, it should assign 0 to the variable y.

if (x < 100)
y = 20;
else
y = 0;

3.9 Write an if-else statement that assigns 1 to x when y is equal to 100. Otherwise, it
should assign 0 to x.

if (y == 100)
x = 1;
else
y = 0;

3.10 Write an if-else statement that assigns 0.10 to commission unless sales is greater
than or equal to 50000.0, in which case it assigns 0.2 to commission.

if (sales >= 50000.0)


commission = .2;
if else (sales < 50000.0)
commission = .1;

3.11 Write an if-else statement that assigns 0 to the variable b and assigns 1 to the
variable c if the variable a is less than 10. Otherwise, it should assign -99 to the variable
b and assign 0 to the variable c.

if (a < 10)
{b = 0;
c = 0;
}
else
{b = 99;
c = 0;
}

3.12 Write nested if statements that perform the following test: If amount1 is greater
than 10 and amount2 is less than 100, display the greater of the two.

if (amount1 > 10)


{ if (amount2 < 100)
{ if (amount1 < amount2)
System.out.print(amount1);
else
System.out.print(amount2);
}
}

3.13 Write code that tests the variable x to determine whether it is greater than 0. If x is
greater than 0, the code should test the variable y to determine whether it is less than
20. If y is less than 20, the code should assign 1 to the variable z. If y is not less than
20, the code should assign 0 to the variable z.

if (x > 0)
{ if (y < 20)
z = 1;
else
z = 0;
}

3.14 What will the following program display?


The program should display 0 0.

3.15 Complete the table that appears after the program.


If the customer purchases this many books: This many
coupons are given:

1 1
2 1
3 2
4 2
5 3
10 3

6. Write an if statement that prints the message "The number is valid" if the variable
grade is within the range 0 through 100.

if (grade >= 0 || grade <=100)


System.out.println(“The number is valid.”);

7. Write an if statement that prints the message "The number is valid" if the variable
temperature is within the range -50 through 150.

if (temperature >= -50 || temperature <= 150)


System.out.println(“The number is valid.”);

8. Write an if statement that prints the message "The number is not valid" if the variable
hours is outside the range 0 through 80.

if (hours > 0 || hours < 80)


System.out.println(“The number is not valid.”);

9. Write an if-else statement that displays the string objects titlel and title2 in
alphabetical order.

if (title1.compareTo(title2)<0)
System.out.println(title1 + “ comes before “ + title2 + “ alphabetically.”);
else
System.out.println(title2 + “ comes before “ + title1 + “ alphabetically.”);

10. Convert the following if-else-if statement into a twitch statement:


switch (choice)
case1:
System.out.print(“You entered 1.”);
break;
case2||3:
System.out.print(“You entered 2 or 3.”);
break;
case4:
System.out.print(“You entered 4.”);
break;
default:
System.out.print(“Select again please.”);
break;

6. What risk does a programmer take when not placing a trailing else at the end of an if-
else-if statement?
There is a chance that the program will not compile because Java is looking for
other factors that may be possible.

7. Briefly describe how the && operator works.


The program must fit both cases in the && operation in order for the code to
complete.

8. Briefly describe how the | | operator works.


The program must fit either case in the || operator in order for the code to
complete.

9. Why are the relational operators called "relational"?


Because they relate the values that you input.

You might also like