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

COSC1046 - Chapter 3

The document discusses control flow statements in Java including if/else statements, switch statements, and ternary operators. It provides examples of using these statements and operators. The document also includes an assignment to write a program that displays the larger of two numbers using a ternary operator.

Uploaded by

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

COSC1046 - Chapter 3

The document discusses control flow statements in Java including if/else statements, switch statements, and ternary operators. It provides examples of using these statements and operators. The document also includes an assignment to write a program that displays the larger of two numbers using a ternary operator.

Uploaded by

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

Name: Vishw Patel Student I’d(229689040) COSC1046 Lab 1

Chapter 3

We’ll start this lab with the if statement. The syntax looks like:

These can be used to compare numbers for our condition.

We can also nest if statements to check for multiple conditions or use the OR or AND operators:

We can also nest the else and else if after our if statement:

What do you get when you print this code? Why?

The switch statement works like an if statement, but has multiple cases:
Name: Vishw Patel Student I’d(229689040) COSC1046 Lab 1

It is important that break; is included after each case, or else the cases beneath the first executed one
will also execute. What does the default case do?

A Ternary operator is written as:


Name: Vishw Patel Student I’d(229689040) COSC1046 Lab 1

Assignment Portion – hand in your work once completed

Write a program that displays the larger of two numbers using a ternary operator

public class program1 {

public static void main(String[] args) {

import java.util.Scanner;

public class program1 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter the first number: ");

int firstNumber = input.nextInt();

System.out.print("Enter the second number: ");

int secondNumber = input.nextInt();

int largerNumber = (firstNumber > secondNumber) ? firstNumber : secondNumber;

System.out.println("The larger number is: " + largerNumber);

Fill out the table for all the boolean operators

< Less than


> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equal to
!= Not equal to
|| OR
&& AND
^ XOR
Name: Vishw Patel Student I’d(229689040) COSC1046 Lab 1

For the following program, use if statements to display the number or roots for the equation, and
calculate the roots using the Math.pow() method that is listed at the bottom of the question.

You might also like