Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a.

Slides:



Advertisements
Similar presentations
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Advertisements

Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
Introduction to Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
UNIT II Decision Making And Branching Decision Making And Looping
DAT602 Database Application Development Lecture 5 JAVA Review.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Conditional Control Flow By Muhammad Ahsan Qadar SACS Programming Fundamentals Workshop 1.
CPS120: Introduction to Computer Science Decision Making in Programs.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Controlling Execution Dong Shao, Nanjing Unviersity.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Chapter 05 (Part III) Control Statements: Part II.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
OOP (pre) Basic Programming. Writing to Screen print will display the string to the screen, the following print statement will be appended to the previous.
Lesson 6 Selection Structures. Example program //This will convert a numeric grade into a letter grade import TerminalIO.KeyboardReader; public class.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
Application development with Java Lecture 6 Rina Zviel-Girshin.
CSI 3125, Preliminaries, page 1 Control Statements.
1 PHP Intro PHP Introduction After this lecture, you should be able to: Know the fundamental concepts of Web Scripting Languages in general, PHP in particular.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Chapter 4: Control Structures I
Chapter 4: Control Structures I
Sequence, Selection, Iteration The IF Statement
Compound Condition Break , Continue Switch Statements in Java
Introduction to programming in java
Expressions and Control Flow in JavaScript
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
SELECTION STATEMENTS (1)
Control Structures.
Chapter 4: Control Structures I
Control Structures: Selection Statement
Chapter 4: Control Structures I (Selection)
Control Structure Chapter 3.
PROGRAM FLOWCHART Selection Statements.
Loops CGS3416 Spring 2019 Lecture 7.
Control Structure.
Presentation transcript:

Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a structured programming language are sequence, selection, and iteration. Sequence control structure is what you have been doing up until now. The second two is what we are going to take a look at next.

Selection/Iteration Structure Selection and Iteration allow the flow of the program to be altered, depending on one or more conditions. Selection is implemented by using a if, if/else, and switch statements. Iteration is implemented by using the while, do/while, and for statements.

The IF statement The if statement works much the same as a if/then statement in Visual Basic. It uses relational operators to test if something is true or false. If it is true, the program will execute the statement(s) within the if statement. If it is false, the program will bypass the statements and continue with the statements following the if statement.

Syntax of the If Statement if (test expression) { statement1; statement2; statement; } //this is an example of a compound statement

if/else Statement The if/else statement works in the same manner as the if/then/else in Visual Basic. It is considered a double-alternative statement. If the expression evaluates as true, then the statements inside the if are executed. If the expression evaluates as false, then the statements inside the else are executed.

Syntax for if/else if (test expression) { statement1; statement2; statement; } else { statement1; statement2; statement; } //example of compound statements

Relational Operators Relational operators allow two quantities to be compared. Mathematical SymbolJavaMeaning == =Equal to  ! =Not equal to <<Less than  < =Less than or equal >>Greater than  > =Greater than or equal

Logical Operators Java

Logical Operators

Example Switch Program import TerminalIO.KeyboardReader; public class ExampleSwitch { public static void main(String [] args) { KeyboardReader reader = new KeyboardReader(); int number; number = reader.readInt("Please enter a number: "); switch (number) { case 1: System.out.println("Your number is 1"); case 2: case 3: case 4: case 5: System.out.println("Your number is between 2 and 5"); break; case 6: case 7: case 8: case 9: case 10:System.out.println("Your number is between 6 and 10"); break; default:System.out.println("Your number is not between 1 and 10"); }

Switch Statement The switch statement works in the same manner as the case select statement in Visual Basic. A selector variable is first evaluated to produce a value. The selector is then compared to a series of cases. If the selector value matches one of the case values, the corresponding case statements are executed.

Syntax for a Switch Statement switch (selector variable) { case case1value :case1statements; break; case case2value :case2statements; break; case casenvalue : case_N_statements; break; default : case exception statements; }

Escape Sequences \b \t \n \” \’ \\ Backspace Tab Newline or line feed Double Quote Single Quote Backslash