Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS0007: Introduction to Computer Programming

Similar presentations


Presentation on theme: "CS0007: Introduction to Computer Programming"— Presentation transcript:

1 CS0007: Introduction to Computer Programming
Decision Structures: The If Statement, Else-If Statement, and Relational Operators CS0007: Introduction to Computer Programming

2 Review Scope refers to… Three kinds of comments in Java:
where in a program an entity can be accessed by name. Three kinds of comments in Java: Single-Line – begin with // and the entire line is ignored Multi-Line – begin with /* and end with */, and everything between is ignored Documentation Comments – special comments that can be used to make attractively formatted HTML files that document the source code. Programming Style refers to… the way a programmer uses spaces, indentation, blank lines, and punctuation characters. The standard input device is normally the… keyboard

3 Review The Java object that refers to the standard input device is…
System.in The class that the Java API provides to allow us to take in input as primitive types or strings is named… Scanner

4 Decision Structures So far our programs have been very sequential:
int a = 2, b = 3, c, d; c = b + a; d = b – a; All of these statements will be executed, in order, from top to bottom This is what is called a sequence structure, because the statements are executed in order, without branching. However, it is often the case that we require a program to execute some statements only under certain circumstances. We can do this with decision structures.

5 Decision Structures In a decision structure’s simplest form certain statements are executed only when a specific condition exists. If the condition does not exist, the statements are not executed. It is said that the statements inside of the decision structure are conditionally executed. We will go over many decision structures, some of which are more complex than this. Is it cold outside? Wear a coat. Yes No Condition Statement 1 Statement 2 Statement 3 True False Boolean Expression Statement 1 Statement 2 Statement 3 True False

6 The if Statement The most basic decision structure in Java is the if statement. if(BooleanExpression) statement; if(BooleanExpression) { statement1; statement2; ... } BooleanExpression – a boolean expression. A boolean expression is one that is either true or false. If the boolean expression is true, the statement that follows will be executed In the multiple statement case if the boolean expression is true, all the statements in the block will be executed. A block is a collection of statements that are organized to be together physically. In this case (and in most) the statements in the brackets are a block. Otherwise the statement is skipped

7 If Statement Example 1 New Topics: If Statements Dead Code

8 Relational Operators Often we want to compare values in order to make a decision. We can do this in Java with relational operators Relational Operators determine whether a specific relationship exist between two values. All relational operators resolve to either true or false. Relational Operators (in Order of Precedence) Meaning > Greater than < Less than >= Greater than or equal to <= Less than or equal to == Equal to != Not equal to

9 Relational Operators Let’s look at an example: length < width
If length is less than width the whole expression resolves to… true If length is greater than width the whole expression resolves to… false If length is equal to width the whole expression resolves to… Another example: length >= width

10 Relational Operators Example 1
New Topics: Relational Operators

11 Programming Style and the if Statement
Even if an if there is only one conditionally executed statement, it is still acceptable to put brackets around it. if(BooleanExpression){ statement; } However, there are two rules you should always follow: The first conditionally executed statement should be on the next line after the if statement. The conditionally executed statement should be indented one level from the if statement. Note: There is NO semicolon after an if statement.

12 Flags A Flag is a boolean variable that signals when some condition exists in a program. When a flag is set to true, it means some condition exists When a flag is set to false, it means some condition does not exist. if(score > 95) highscore = true; Here, highscore is a flag indicating that the score is above 95. Right now, we don’t have any situations where these are terribly useful, but for now, just know we can and will use them.

13 Comparing Characters You can also use relational operators on character data as well: if(ch == 'A') System.out.println("The character is A"); Equal to and not equal to are the most natural for this data type, but you can use any relational operators. 'A' < 'B' Resolves to true 'a' < 'B' Resolves to false Why? Remember, all characters are represented as Unicode numbers. What Java does when comparing characters is compare the Unicode values

14 Character Comparison Example
New Topic: Character Comparison

15 The if-else Statement There is an expansion of the if statement called the if-else statement. if(BooleanExpression) statement or block 1 else statement or block 2 Just like the if statement, BooleanExpression is evaluated. If it resolves to true, then statement or block 1 is executed If it resolves to false, then statement or block 2 is executed

16 if-else Flowchart Boolean Expression Statement 1 Statement 2
True False Statement 4 Statement 5 Statement 6

17 else-if Example New Topic: else-if statement

18 Group Programming: Fraction to Decimal


Download ppt "CS0007: Introduction to Computer Programming"

Similar presentations


Ads by Google