Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.

Slides:



Advertisements
Similar presentations
INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.
Advertisements

Flow of Control Chapter 3.
Control Structures.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
 Control structures  Algorithm & flowchart  If statements  While statements.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
True or false A variable of type char can hold the value 301. ( F )
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
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:
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Loops – While, Do, For Repetition Statements Introduction to Arrays
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Review Review Review. Concepts Comments: definition, example, different types of comments Class: definition, example Object: definition, example Data.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Jaeki Song ISQS6337 JAVA Lecture 04 Control Structure - Selection, and Repetition -
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
Flow of Control Part 1: Selection
Chapter 4: Loops and Files. The Increment and Decrement Operators  There are numerous times where a variable must simply be incremented or decremented.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
Expressions An expression is a series of variables, operators, and method calls (constructed according to the syntax of the language) that evaluates to.
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Loops and Files. 5.1 The Increment and Decrement Operators.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
5 Copyright © 2004, Oracle. All rights reserved. Controlling Program Flow.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
REPETITION CONTROL STRUCTURE
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
3 Control Statements:.
Control Statements Paritosh Srivastava.
Presentation transcript:

Selection statements Repetition statements

Lesson plan Main concepts Practice session –If- then –Switch –Nested if

Sequential sequence and Control Statement Sequential sequences are insufficient to solve most problems. –Get in the Car. –Drive to the supermarket. Tank nearly empty? –Get in the Car. –If we need gas, then fill up. –Drive to the supermarket.

Dependent Actions A question is asked. –A condition is checked. The dependent actions are only performed if the answer is Yes (the condition is true). Sometimes the dependent actions are performed, and sometimes they are not.

Example: Logging In Computer access: –Request a login name from the user. –Request a matching password. –If the password matches the login name, then grant access.

if ( ) ; else ; Simple Choice Statement if ( ) single statement; else single statement;

Boolean expression Boolean expression: is a conditional expression that is evaluated to either true or false. Conditional expression: is a three part expression: Boolean expressions can be combined by boolean operators

Relational Operators

Boolean operators && meansAND ||means OR ! means NOT

Boolean Operators

Example of Boolean expression testScore < 62 age > 35 Height > 6.5 (testScore 35) testScore = 95 ???????

Block -Represents compound statement { }

Example If (age < 35) { coPay = 5; System.out.println(“….”); } Else { coPay = 10; System.out.println(“….”); }

Practice Identify invalid boolean expression Identify invalid If-then-else statement

Answers for last practice exercise I. (i) (x+1>=0) && (x==1) Valid (ii) x 2 +2x-1 Invalid. Arithmetic expression (iii) ((y%2) != 0) Valid

Answers to last practice exercise if (x==y) { System.out.println(“ x=y”); } Else { System.out.println(“ x does not equal to y”); } Should be Lowercase

Answers to last practice exercise if (x=y) then { grade = “A”; } else { Grade = “B”; } If we actually ask (is x equal y) then it should be (x==y)

Answers to last practice exercise if (x==y) { grade = “A”; else grade = “B”; } Hanging “else”

if ( ) ; [else ;] // optional Simple Choice Statement if ( ) single statement; [else single statement;] // optional

Answers to some questions Java machine code is generated and stored in *.class files. For example: –LoanCalculator.java –LoanCalculator.class When we compile a java program outside of Textpad, we need to use: javac.exe When we run a java program outside of Textpad, we need to use:java.exe

Answers to some questions How to clear screen from DOS: for (int i=0; i<25; i++) System.out.println();

Nested If Statement if ( ) if ( ) { statement 1 } else if ( ) { statement 2 } else { statement 3 } else ….

Example if (score >= 95) grade= “A”; else if (score >=90) grade=“A-”; else if (score>=85) grade =“B+”;

SWITCH statement switch (variable) { : …… : } : has the form case or: default : is a block or a single statement, with break variable: of integer, or char data type

Example switch (month) { case 1: System.out.println("January"); break; case 2: System.out.println("February"); break; case 3: System.out.println("March"); break; case 4: System.out.println("April"); break; case 5: System.out.println("May"); break; case 6: System.out.println("June"); break; …………….. default: System.out.println("Not a month!"); break; }

Char data type & boolean data type Char: –It is 2 bytes –Char constant is denoted by single quote: for example: ‘s’, ‘h’ Special characters: \bbackspace, \ttab Boolean: - Can be true or false

Examples char aLetter; aLetter =‘k’; … aLetter =‘k’; boolean flag; flag = true; … flag = false;

Practice Evaluate boolean expressions Identify invalid Switch statement

Project 1 (d) Exit the system normally: –System.exit(0); Exit the system abnormally: e.g: System.exit(1); –a nonzero status code indicates abnormal termination.

Repetition statements Repetition of statements is a further common facet of everyday life and object behavior: –Repeat every day: Get up; have breakfast; go off to work; have lunch; do more work; have an evening meal; relax; go to bed. Java has three types of loop for repetition: –while, do-while, for.

The While Loop while( ){ // Repeat multiple statements. statement 1 statement 2 statement 3... }

Example // Print the numbers 1 to maximum. Assuming that maximum is 100 int maximum = 100; int nextToPrint = 1; while(nextToPrint <= maximum){ System.out.print(nextToPrint+" "); // Get ready to print the next number. nextToPrint = nextToPrint +1; } System.out.println(); }

The Do Loop do{ // Repeat multiple statements. statement 1 statement 2 statement 3... } while(<boolean expression); Note that the statements in the body of the loop are always executed at least one. Note the final semicolon, which is required.

Printing a Sequence int maximum = 100; int nextToPrint = 1; do{ System.out.print(nextToPrint+" "); // Get ready for the next number. nextToPrint = nextToPrint + 1; } while(nextToPrint <= maximum); System.out.println(); }

The For Loop Used to repeat statements a fixed number of times: –Add three teaspoons of sugar. –Knock twice and then give the password. Used very often with arrays and collections of objects Similar in effect to the while loop.

The For-Loop Outline // Repeat multiple statements. for(initialization; condition; post-body update){ // Statements to be repeated. statement 1 statement 2 statement 3... } Commonly used with increment and decrement operators.

Increment and Decrement Used as a shorthand for add-one-to and subtract-one-from: value = value+1; value += 1; value++; Prefix and postfix forms: ++value; --value; value--;

A For Loop to Print a Sequence int maximum = 100; int nextToPrint; for(nextToPrint = 1; nextToPrint <= maximum; nextToPrint++){ System.out.print(nextToPrint+“$“+” ”); } System.out.println(); The variable is not available outside the loop $ 2$ 3$ 4$ 5$ 6$.

Practice Develop a java class called: SumCalculator.java which computes a sum of all integer from 1 to 100 and displays the result to the screen