Starting Out With Java - Java Programming Notes PDF

Summary

This document is a set of notes on starting out with Java. It covers control structures, decision structures, and logical operators in Java programming using examples and flowcharts. Useful for students learning Java.

Full Transcript

Starting Out with Java From Control Structures through Objects Session 7 (Chapter 3): Decision & Structures Dr. Muhammad Faheem November 24, 2020 1 / 29 Control Structures Three methods of Processing a Pro...

Starting Out with Java From Control Structures through Objects Session 7 (Chapter 3): Decision & Structures Dr. Muhammad Faheem November 24, 2020 1 / 29 Control Structures Three methods of Processing a Program: In sequence Branching Looping Branch: Altering the flow of program execution by making a selection or choice. By making a selection or a choice, which is also called a selection/decision/branch. Loop: Altering the flow of program execution by repetition of statement(s). 2 / 29 Decisions Decision: Do we need to take the umbrella outside? Decision will depend on weather condition. Question: Is it raining outside. The answer will be either Yes or No. If raining then take an umbrella. Otherwise do not take an umbrella. Example: Coffee Making Machine 3 / 29 Decisions Exemplary Statements: If it is cold outside, I will wear a coat. If the obtained marks are greater than 85, then grade is A. If the obtained marks are greater than or equal to 40, then passed, otherwise failed. All above statements contain some condition (written in red) and based on the result of that condition some action is performed. Conditions are written as boolean expressions. A boolean Expression is a logical statement that either True or False. 4 / 29 Flowchart Flowchart graphically represents an algorithm. Flowchart are very useful in explaining program. It helps people to understand the flow and logic of the program. 5 / 29 Flow of Execution 6 / 29 Sequential Control Structure Statements are executed one after another. There are no brances/decisions/selections. Program calculates profit on sales. 7 / 29 Example: Sequential Control Structure public class Area{ public static void main(String[] args){ double length, width, area; length = 10; width = 5; area = length * width; System.out.println("Area: " + area); } } 8 / 29 Selection (a.k.a branch, decision) Control Structure There will be atleast one decision (diamond) symbol. 9 / 29 Example: Selection Control Structure public class Even{ public static void main(String[] args){ int num = 12; if(num % 2 == 0) System.out.println("Num is Even"); } } 10 / 29 Relational Operators Relational Operators Relational operators are binary operator. Allows you to make comparisons in a program. Condition is represented by a logical expression in Java. Logical Expression: Expression that has a value of either true or false. 11 / 29 Relational Operators Mathematical Representation Java Description = == equal to 6= != not equal to < < less than ≤ > greater than ≥ >= greater than or equal to 12 / 29 Example: Relational Operators See the Code Example 1 for Session 7! 13 / 29 Relational Operators with char Relational Operators can be used with char data type. For comparison, JAVA use respective ASCII value (Unicode collating sequence) of characters. ‘ ’ < ’a’ is true ‘R’ > ’T’ is false ‘+’ < ’*’ is false ‘6’ ’ is true See Appendix B from Book. 14 / 29 Logical Operators Java has three logical operators. Logical operators works on boolean operands. Boolean Expressions’ result is either true or false. Meaning Description && AND Both expressions must be true for the overall expression to be true. || OR Aleast One expression must be true for the overall expression to be true. ! NOT Reverse the truth of a boolean expression. 15 / 29 Logical Operators: Truth Table Boolean Expression 1 (BE1) Boolean Expression 2 (BE2) BE1 && BE2 BE1 || BE2 !(BE1) true true true true false true false false true false false true false true true false false false false true 16 / 29 Example: Expression Result Explanation (10 >= 5) && (’a’ < ’b’) true ASCII value of a is also less than b (20 >= 25) && (’a’ < ’b’) false Because (20 >= 25) is false (20 >= 25) || (’a’ > ’b’) false Both expressions evaluate to false (’A’ < ’a’) || (5 ! = 5) tue Because (’A’ = 5 == != 6 && 7 || 8 = += -= *= /= %= 18 / 29 Selection One-way Selection (if statement) Two-way Selection Compound (block of) Statements Multiple Selections (nested if ) Conditional Operator Switch Structures 19 / 29 if Statement The if statement decides whether a section of code executes or not. Syntax: if (boolean expression){ statement(s) } The if statement causes one or more statements to execute only when a boolean expression is true. Statement(s) referred to as action statement(s). 20 / 29 if Statement 21 / 29 if Statement An if statement can span more than one line; however, it is still one statement. if (average > 95) grade = ’A’; is functionally equivalent to if(average > 95) grade = ’A’; Note: No semicolon after if condition. 22 / 29 if Statement if (coldOutside) wearCoat(); 23 / 29 Compound (Block of) Statements Syntax { statement1 statement2... statementn } 24 / 29 Block if Statement if (coldOutside){ wearCoat(); wearHat(); wearGloves(); } Use of curly brackets to block several statements together! 25 / 29 Block if Statement Remember that when the curly braces are not used, then only the next statement after the if condition will be executed conditionally. if (expression) statement1; statement2; statement3; Only Statement1 will be conditionaly executed. statement2 and statement3 will be executed sequentially. 26 / 29 if Statements and Boolean Expressions if (x > y) System.out.println("X is greater than Y"); if(x == y) System.out.println("X is equal to Y"); if(x != y){ System.out.println("X is not equal to Y"); x = y; System.out.println("However, now it is."); } 27 / 29 Example: if statement See the Code Examples for Session 7! 28 / 29 Credit Tony Gaddis, Starting out with Java: From Control Structures through Objects, 6th Edition, Pearson 2016. 29 / 29

Use Quizgecko on...
Browser
Browser