1 / 18

Decision Structures: The If Statement, Else-If Statement, and Relational Operators

Decision Structures: The If Statement, Else-If Statement, and Relational Operators. CS0007: Introduction to Computer Programming. Review. Scope refers to… where in a program an entity can be accessed by name. Three kinds of comments in Java:

dore
Download Presentation

Decision Structures: The If Statement, Else-If Statement, and Relational Operators

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Review • Scope refers to… • 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? Boolean Expression Condition Yes True True Wear a coat. Statement 1 Statement 1 False False No Statement 2 Wear a coat. Statement 2 Statement 3 Wear a coat. Statement 3

  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 caseif 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.

  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… • false • Another example: length >= width • If length is less than width the whole expression resolves to… • false • If length is greater than width the whole expression resolves to… • true • If length is equal to width the whole expression resolves to… • true

  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 orblock 2 • Just like the if statement, BooleanExpressionis 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 Statement 4 Statement 5 Boolean Expression True False Statement 6 Statement 1 Statement 2 Statement 3

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

  18. Group Programming: Fraction to Decimal

More Related