90 likes | 100 Views
Learn about control structures in Java programming, including if statements, conditional operators, and examples for selecting actions based on conditions. Enhance your understanding of sequential, selection, and looping control types.
E N D
Announcements • Quiz 1 Posted on blackboard • Quiz 1 Handed Back (and Gone Over) Next Monday • “Just a Quiz” • 5% of Final Grade • Exam 1 is 10% of Final Grade
Selection (if-then-else) • Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else): Control Proceeds Dependent on Conditions Iteration (looping): Control Repeated until Condition Met
JAVA if Statement Syntax • Syntax if (condition)statement; • If Condition Is True, Statement Is Executed • If Condition Is False, Statement Is Not Executed • If Multiple Actions Are Required for Statement, a Compound Statement Must Be Used: if (condition) { statement; statement; }
Conditional Operators • Relational Operators: < , > , >= , <= • Equality Operators: == , !=
Comparing Strings • Cannot use Equality Operators • Use .equals() instead: String myString; … if (myString.equals(“Hello”) ) System.out.println(“Well, Hello to you too!!”);
Selection Examples int age; char grade; String firstName; //Code to assign to age, grade, and firstName if (age < 30) System.out.println( “You are very very Young”); if (grade == ‘A’) System.out.println(“Congratulations!” ); if (grade != ‘F’) System.out.println(“You passed!” ); if (firstName.equals(“Jon”)) System.out.println(“Thanks, Jon!” );
else Statement • Syntax: • if (condition) • { • statement(s);//condition true • } • else • { • statement(s);//condition false • }
if-else Example • if (myGrade >= 60) • { • System.out.println(“You passed!” ); • } • else • { • System.out.println(“How about them Cubs?” ); • }
Compound Statements • Compound Statement: One or More Statements within a Set of Curly Braces • Must Use Compound Statement if More than One Statement Is Supposed to be under Control of if or else Conditional • Include Curly Braces after if so if other Statements Are Added in Future, Curly Braces Are Already There