1 / 11

Conditionals in Java

Conditionals in Java. Computer Science 3 Gerb Objective: Use conditional statements in Java. Why Conditionals?. Up until now, we have only considered programs where all statements are executed all the time.

olympe
Télécharger la présentation

Conditionals in Java

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. Conditionals in Java Computer Science 3 Gerb Objective: Use conditional statements in Java

  2. Why Conditionals? • Up until now, we have only considered programs where all statements are executed all the time. • It is useful to add statements to a program that sometimes are executed and sometimes not. • Such statements are called conditional

  3. The if statement if (condition) statement; • If the condition is found to be true, execute the statement, otherwise skip it. • Condition must be in parentheses. • The condition must have a boolean value. • Note there is no semicolon after the condition • If you put one there, will mean the empty statement comes after the if. • The next statement will always execute. • Traditionally the statement dependant on the if is indented.

  4. Comparison Operators • The following operators are commonly used in conditions. • Greater than (>) or less than (<) • Greater than or equal to (>=) or less than or equal to (<=) • Equal to (==). Note two equal signs. • Not equal to (!= ) • Single equal sign (=) means assignment. It is not a comparison operator.

  5. Blocks • Java is a “block-structured” language • Supports blocks, groups of statements executed together. • Surrounded by curly brackets • When the first character after the condition of an if is a curly bracket, the entire block is dependant on the if. • I.e. if the condition is true, entire block executes in order. • If false, entire block is skipped. • Of course the block can contain another if...

  6. Some examples • If m is greater than 0, print it: if (m>0) System.out.print(m); • If y is equal to z, set x equal to y and a equal to b. if (y==z) { x=y; a=b; } • Note that there is no semicolon after the close bracket.

  7. Else • The if/else statement provides not only a statement to execute when the condition is true, but also a different one to execute when the condition is false. if (condition) statement; else statement; • Either statement can be a block (without a semicolon)

  8. Dependant statements Must be on subsequent lines Must be indented Open brackets may be on the same line or the next Each level uses the same number of spaces if (a==b) a=a+1; if (a==b) { -OR- if (a==b) { if (a==b) if (b==c) c=c+1; Indenting Ifs

  9. Else statements must be indented the same as the corresponding if Statements dependent on the same if or else must be indented the same Close curly brackets must be indented the same as their if or else if (a==b) a=a+1; else b=b+1; if (a==b) { a=a+1; b=b+1; } Indenting Ifs cont’d

  10. The if should be even with the statement before it a=a+1; if (a==b) b=b+1; Indenting Ifs cont’d

  11. Summary • The if statement supports conditionals • Condition is boolean • Statement executed only if condition is true • Relational operators frequently used in the condition. • Surround multiple statements with curly brackets. • If/else lets program choose between two statements or blocks • Best to use curly brackets when nesting ifs.

More Related