CSE 1020:Control Structure

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
If Statements & Relational Operators Programming.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
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.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Flow of Control Part 1: Selection
Multi Way Selection You can choose statement(s) to run from many sets of choices. There are two cases for this: (a)Multi way selection by nested IF structure.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
1 Example: Solution of Quadratic Equations We want to solve for real values of x, for given values of a, b, and c. The value of x can be determined from.
CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
Control statements Mostafa Abdallah
Control Statements: Part1  if, if…else, switch 1.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
CSE 1020:Using Objects Mark Shtern 1-1. Summary Read API Method binding and overloading Development process Input validation Assert 1-2.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
 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.
CSE 1020:Using API Mark Shtern 1-1. Summary Delegation – Static Method, Object Client view vs Implementer view API, UML Errors Software Engineering Utility.
Selection UCT Department of Computer Science Computer Science 1015F Hussein Suleman March 2009.
The Ohio State University
Chapter 4: Control Structures I
Chapter 4: Control Structures I
Decisions Chapter 4.
C-Language Lecture By B.S.S.Tejesh, S.Neeraja
Lecture 7: Repeating a Known Number of Times
CHAPTER 4 Selection CSEG1003 Introduction to Computing
A mechanism for deciding whether an action should be taken
Chapter 5: Control Structures II
Flow of Control.
Conditionals & Boolean Expressions
CiS 260: App Dev I Chapter 4: Control Structures II.
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Chapter 4: Control Structures I
SELECTION STATEMENTS (2)
Summary Two basic concepts: variables and assignments Basic types:
Relational, Logical, and Equality Operators
SELECTIONS STATEMENTS
Conditionals.
CS2011 Introduction to Programming I Selections (I)
Program Flow.
Decisions, decisions, decisions
CSCE 206 Lab Structured Programming in C
Controlling Program Flow
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

CSE 1020:Control Structure Mark Shtern

Selection

Flow of Control Flow of control refers to order in which statements are executed Sequence flow S1 S2 S3

Flow of Control Selection flow S2 S2.1 S4 S1 S3 S3.1

Flow of Control Selection flow True S2 S2.1 S4 S1 S3 S3.1 False

Flow of Control Selection flow If (condition) { statement-S2 } else statement-S3 statement-S3.1 True S2 S2.1 S4 S1 S3 S3.1 False

If statement If (count > maximum) { count--; output.println(“Maximum exceeded.”); }

Example 5.1 Write a fragment that prompts for and reads an integer from the user and outputs its absolute value without using Math.abs

Answer output.print(“Enter an integer...”); int entry = input.nextInt(); int absValue = entry; If (entry < 0 ) { absValue = - entry; } output.println(absValue);

Example 5.2 Rewrite solution of previous Example such that no defaults are used.

Answer output.print(“Enter an integer...”); int entry = input.nextInt(); int absValue; if (entry < 0) { absValue = -entry; } else absValue = entry; output.println(absValue);

Building the Condition Relational Expression Boolean Variable Boolean-Returning Method Boolean Expression Operator Syntax true if ! !x x is false && x && y x and y are both true || x|| y either x or y or both are true

deMorgan law !(A && B) is equivalent to (!A) || (!B) !(A || B) is equivalent to (!A) && (!B)

Examples if (x >= a && x <b) If (!(x >= a && x <b) ) If (x.equals(y)) vs if (x!=null && x.equals(y))

Example 5.3 Write a fragment that determine whether a given integer y is a multiple of a given integer x, and output message accordingly. Note that no integer is a multiple of zero.

Answer If ( x == 0 || y % x !=0) { output.println(“”Not a multiple.”); } else ouput.println(“Yes, it is a multiple”);

Multiway if Statement-S If (condition-1) { Statements-A } else if (condition-2) Statements-B } else if (condition-3) Statements-C } Statements-X

Example 5.5 Write a program that prompts for and reads an year (an int) from a user. If the entry is not positive, the program crash. Otherwise, it should output a messages that indicates whether the year entered is a leap year

Nested IF if ( x<=a && y >d) if ( x<=a) { if (y >d) }

Switch If (gender == ‘M’) { male++; } else if (gender == ‘F’) female++; } else other++; } switch (gender) { case ‘M’: male++ break; case ‘F’: female++; default: other++; }

Predict results (Ex505) int x =1; boolean b1 = (x > 0); boolean b2 = false; boolean b3 = b1 && !b2; If (b3 || x == 1) { x++; }

Predict results (Ex506) int x =1; int y = -1; If (x >=y && y > 0) { x++; y = x; } else if (x == y || y < 1) { x--; } else { y = 0; }