110 likes | 280 Views
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.
E N D
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. • It is useful to add statements to a program that sometimes are executed and sometimes not. • Such statements are called conditional
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.
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.
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...
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.
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)
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
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
The if should be even with the statement before it a=a+1; if (a==b) b=b+1; Indenting Ifs cont’d
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.