& Decision Making Algorithms BY: Aakash Indurkhya.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
Decisions If statements in C.
CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
8-May-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Switch Statement switch (month) { case 9: case 4: case 6: case 11: days = 30; break; case 2: days = 28; if (year % 4 == 0) days = 29; break; default: days.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Objectives You should be able to describe:
© 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.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
Administrative MUST GO TO CORRECT LAB SECTION! Homework due 11:59pm on Tuesday. 25 points off if late (up to 24 hours) Cannot submit after 11:59pm on Wednesday.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
The switch Statement, DecimalFormat, and Introduction to Looping
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
Iteration and Simple Menus Deterministic / Non-deterministic loops and simple menus.
Chapter 4 Selection Structures: Making Decisions.
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
CS 240 – Computer Programming I Lab Kalpa Gunaratna –
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
Lecture 01b: C++ review Topics: if's and switch's while and for loops.
1 Class Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
1 Control Structures (Chapter 3) 3 constructs are essential building blocks for programs Sequences  compound statement Decisions  if, switch, conditional.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
FOR LOOPS "LOOP FOR A SET NUMBER OF TIMES.". FOR ( START_VALUE; END_VALUE; INCREMENT_NUMBER ) { //YOUR_CODE_HERE } So after the word "for" (in lowercase)
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
JAVA PROGRAMMING Control Flow. Jeroo: Finish Activities 1-7 Finish InputTest program w/changes.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
CSE 110 Midterm Review Hans and Carmine. If Statements If statements use decision logic In order to properly use if statements relational operators must.
CHAPTER 4 DECISIONS & LOOPS
The switch Statement, and Introduction to Looping
Week 4 - Monday CS 121.
SELECTION STATEMENTS (1)
Chapter 5 Repetition.
Conditional Statements
Topic 1: Problem Solving
SELECTIONS STATEMENTS
Program Flow.
Unit 7 - Short-Circuit Evaluation - De Morgan’s Laws - The switch statement - Type-casting in Java - Using Math.random()
Fundamental Programming
Decisions, decisions, decisions
Additional control structures
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Presentation transcript:

& Decision Making Algorithms BY: Aakash Indurkhya

 The if statement allows for a boolean condition to be set on a section of code. The else statement requires an if statement to before it. The else statement offers the replacement code if the boolean condition is not true.

SOURCE CODEPSUEDO-CODE int num = kb.nextInt(); if(num % 3 == 0){ num++; } num is a variable set by a user if num is divisible by three: increment num by 1

SOURCE CODEPSEUDO-CODE int num = kb.nextInt(); if(num % 3 == 0){ num++; } else{ num+= 5; } num is a variable set by a user if num is divisible by three: increment num by 1 Otherwise: increment num by five

 You will find that Boolean algebra is easiest to understand when you talk it out to yourself.  Now you may begin to ask your self what if I want to have multiple conditions or two possible conditions.  There are several different ways to do all of this.  These topics involve nested if else statements, Boolean Algebra and De Morgan’s Law, and dangling else.

 If statements can be nested such that another condition is part of the code that has already passed the original (or parent) condition. Let’s look at some code. int num = kb.nextInt(); // user entered number if (num % 2 == 0){ // if num is even if (num % 6 == 0){ // AND it is divisible by 6 num+= 4; // add for to num } else{ // OTHERWISE (meaning it is even but not divisible by 6) num = num*num; // square num }

if (num > 30){ if (num-1 >= 31){ System.out.println(num);} else{ System.out.println(“Not 31”); } } // It is important not to be fooled by the indentation. The else is attached to the second if because else statements are attached to the last Finished if statement. This is why you should always indent with care.

 This is a very handy thing to remember.DeMorgans Law literally translates to "De Morgan's laws are rules relating the logical operators "and" and "or" in terms of each other via negation, namely:  NOT (P OR Q) = (NOT P) AND (NOT Q)NOT (P AND Q) = (NOT P) OR (NOT Q)" Much of this has to do with the Boolean Algebra behind our logic. Note that this topic is very if you say what you want to do before you start coding.

 Here is a real example...while (distance -3.5) // this loop will go on as long as the person hasnt stepped off the bridge { int direction = rndm.nextInt(2);  // direction is a random number 0 or 1 if (direction == 1) // if direction = 1 distance++; // the person steps forward 1 foot if (direction == 0) // if direction = 0 distance--; // the person steps backwards 1 foot counter++; // number of steps goes up each time }

 This was part of the random walk program and DeMorgans Law was used in the loop condition. while (distance - 3.5) states that the loop will continue while the distance is less than 3.5 and greater than If you look at this statement on a number line then it will seem obvious why it is true, but in your mind you tend to think of things like that in terms of or (||).This is an important aspect of the conditions that you put on loops.

Switch statements are really useful when you have several different scenarios. Switch statements are great substitutes for lengthy if statements. Also, switch statements are great for menus in bigger programs. Here is the concept: switch (numeric case){ case (some number): do something; break; case (some other number): do something; break; default: do something; }

Scanner kb = new Scanner(System.in); int choice = kb.nextInt(); switch (choice){ case 1: playGame1; break; case 2: playGame2; break; default: // if choice is not described by any of the cases System.out.println(“Invalid Input”); }

Here are some good sources I found on these topics: osters/po25.pdf nutsandbolts/if.html Java /ControlFlow/ dangl ing.html nutsandbolts/branch.html