Lecture 03: Flow Control PDF
Document Details
Uploaded by HopefulCentaur
İzmir Ekonomi Üniversitesi
Tags
Summary
This document is a lecture on flow control in programming, specifically in the Java language. It details statements, variables, boolean expressions, operators. The lecture also touches on the concept of programming flow and how conditions affect that flow.
Full Transcript
Lecture 03: Flow Control SE115: Introduction to Programming I Previously on SE 115… Values Variables Evaluation Assignment Types Formatted output Flow So far, a program flowed top to bottom, line by line. import java.util.Scanner; public class Age { public static void main...
Lecture 03: Flow Control SE115: Introduction to Programming I Previously on SE 115… Values Variables Evaluation Assignment Types Formatted output Flow So far, a program flowed top to bottom, line by line. import java.util.Scanner; public class Age { public static void main(String args[]) { int age = -1; Scanner sc = new Scanner(System.in); System.out.print("Please enter your age: "); age = sc.nextInt(); System.out.println("You are " + age + " years old."); } } Flow It is possible to change the flow of the program depending on one or more conditions. For example, if the age is less than 18, then the program might refuse to continue. This check creates two paths of execution; ○ If the age is under 18, then the program might print a message and end. ○ Otherwise, it can continue to operate and let the user see other options. The same checks can be done to control if a valid value is entered. To do so, we have to first remember logical operations. Boolean Logic George Boole introduced the algebra in which the values of the variables are the truth values true and false, usually denoted 1 and 0. They are called boolean values after him. In programming, we make use of true and false to evaluate statements. For the variable “int x = 5;” we may check if “x == 5;” ○ if x is equal to five: then the statement is TRUE, ○ if x is equal to four: then the statement is FALSE We have several operators for testing values. Boolean Operators Operator Java Example Equal == x == 5; Not Equal != x != 5; Less than < x < 3; Greater than > x > 4; Less than or equal to = 6; Negation ! !(x == 4); Boolean Operators public class BooleanVals { public static void main(String args[]) { int x = 4; System.out.println("3 == 3 is " + (3 == 3)); System.out.println("x == 3 is " + (x == 3)); System.out.println("x != 3 is " + (x != 3)); System.out.println("x < 5 is " + (x < 5)); System.out.println("x