Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.

Slides:



Advertisements
Similar presentations
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure.
Introduction to Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
UNIT II Decision Making And Branching Decision Making And Looping
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
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:
© 2000 Scott S Albert Selection Structures Structured Programming 256 Chapter 4.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Object Oriented Programming Lecture 4: Flow Control Mustafa Emre İlal
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Overview of Java Loops By: Reid Hunter. What Is A Loop? A loop is a series of commands that will continue to repeat over and over again until a condition.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
1 Chapter 3: Loops and Logic. 2 Control Statements If statement Example NumberCheck.java Relational operators (, >=, ==, !=) Using code blocks with If.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Working with Loops, Conditional Statements, and Arrays.
Lecture 01b: C++ review Topics: if's and switch's while and for loops.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
CSI 3125, Preliminaries, page 1 Control Statements.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
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.
 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.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Follow up from lab See Magic8Ball.java Issues that you ran into.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
JavaScript: Control Statements I
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
CiS 260: App Dev I Chapter 4: Control Structures II.
Ch 7: JavaScript Control Statements I.
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
JavaScript: Control Statements I
Control Structures.
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Loop Control Structure.
محاضرة 1: مقدمة للمسـاق و مراجعـة للأساسيـات
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
3 Control Statements:.
Three Special Structures – Case, Do While, and Do Until
Computer Science Core Concepts
Control Structures Part 1
ICT Programming Lesson 3:
Control Statements Paritosh Srivastava.
PROGRAM FLOWCHART Iteration Statements.
How to allow the program to know when to stop a loop.
Presentation transcript:

Program Structures Chapter 5

5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements

5 If, Else, and Else-If if Statements “If the sky is blue, I’ll ride my bike.” else Statements “If the sky is red, I’ll drive; else (otherwise) I’ll ride my bike.” Else - if Idioms “If the sky is blue, I’ll ride my bike; else if the sky is red, I’ll drive, else I’ll walk.”

5 Nested if Statements if statements can be nested (combined) within other if statements to account for multiple conditions. If the sky is blue If there are clouds in the sky Do something for blue sky, cloudy days Else Do something for blue sky, cloudless days Else Do something for non-blue sky days

5 The switch Statement switch statements are an efficient way to choose between multiple options. Easier to expand options than else-if structures Easier to read than multiple else-if structures Use the break keyword to prevent “falling through” to next statement

5 Switch Evaluation switch statements evaluate int values: int x = 3; switch(x) { 1: x += 2; break; 2: x *= 2; break; 3: x /= 2; break; }

5 Logical Operators Combine or negate conditional tests Logical AND ( && ) “The value of x is greater than 10 and the value of x is less than 20” (x > 10) && (x < 20); Logical OR ( || ) “The value of x is greater than 10 or the value of x is less than 5” (x > 10) || (x < 5); Logical NOT ( ! ) “The value of x is not greater than 10” ! (x > 10);

5 Looping What is iteration? Iteration is a structure that repeats a block of code a certain number of times, or until a certain condition is met.

5 The while Loop “As long as this condition is true, execute this code” while ( counter < 11) { counter++; } Might not execute if the condition is already false

5 The for Loop Includes (optional) arguments for: An initialization variable A conditional test An action for ( int counter = 1; counter < 11; counter++ ) { System.out.print(counter + “ ”) }

5 The do...while Loop “Take this action, while this is true” int value = 10; do { value = value + 2; } while ( value < 10 ); Ensures at least one execution of code