if-else if (condition) { statements1 } else { statements2

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Iterations for loop. Outcome Introduction to for loop The use of for loop instead of while loop Nested for loops.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
CS0007: Introduction to Computer Programming Methods: Documentation, Reference Parameters, Modularization 2.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
1 Repetition structures Overview while statement for statement do while statement.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 2 Sanjay Goel University at Albany,
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
1 Repetition structures Overview while statement for statement do while statement.
CSCI1402: Lecture 2 Week 6 Dr. David A. Elizondo Centre for Computational Intelligence School of Computing Office: Gateway 6.61
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.
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
LAB 10.
UNIT II Decision Making And Branching Decision Making And Looping
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.
The for-statement. Different loop-statements in Java Java provides 3 types of loop-statements: 1. The for-statement 2. The while-statement 3. The do-while-statement.
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
Introduction to Objects A way to create our own types.
Chapter 6 Iteration.  Executes a block of code repeatedly  A condition controls how often the loop is executed while (condition) statement  Most commonly,
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
 Executes a block of code repeatedly  A condition controls how often the loop is executed  Most commonly, the statement is a block statement (set of.
1 Conditionals Instructor: Mainak Chaudhuri
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
The if StatementtMyn1 The if Statement The basic if statement allows your program to execute a single statement, or a block of statements enclosed between.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
Recursion Pepper. Another way to loop Call yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Office hours: Thursday 1300 – 1400hrs.
Take out a piece of paper and PEN. The quiz starts ONE minute after the tardy bell rings. You will have 45 – 90 seconds per question. Determine the output.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
CS001 Introduction to Programming Day 6 Sujana Jyothi
1 Conditionals Instructor: Mainak Chaudhuri
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING While Loop.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
The if-else StatementtMyn1 The if-else Statement It might be that we want to execute a particular statement or block of statements only when the condition.
Department of Computer Science
using System; namespace Demo01 { class Program
Chapter 5: Control Structures II
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Repetition.
Selected Topics From Chapter 6 Iteration
TK1114 Computer Programming
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
Something about Java Introduction to Problem Solving and Programming 1.
Introduction to Robots and the Mind - Methods -
Decision statements. - They can use logic to arrive at desired results
Functions Used to write code only once Can use parameters.
Computing Adjusted Quiz Total Score
TO COMPLETE THE FOLLOWING:
LRobot Game.
An Introduction to Java – Part I, language basics
AP Java Warm-up Boolean Array.
Code Animation Examples
class PrintOnetoTen { public static void main(String args[]) {
Lecture Notes – Week 2 Lecture-2
Scope of variables class scopeofvars {
Object Oriented Programming
while while (condition) { statements }
CIS 110: Introduction to Computer Programming
Perfect squares class identifySquareButLessClever {
6.2 for Loops Example: for ( int i = 1; i
CIS 110: Introduction to Computer Programming
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

if-else if (condition) { statements1 } else { statements2 if within else if (condition1) { statements1 } if (condition2) { statements2 } statements3

if-else if-else if-…-else if (condition1) { statements1 } else if (condition2) { statements2 else if (condition3) { statements3 … else { statementsn

Example class greetings { public static void main(String arg[]) { int hour = 3; if ((hour >= 0) && (hour < 12)) { System.out.println(“Good Morning!”); } else if ((hour >= 12) && (hour < 18)) { System.out.println(“Good Afternoon!”); else if ((hour >=18) && (hour < 24)) { System.out.println(“Good Evening!”); else { System.out.println(“Bad time!”);

Dangling ‘else’ /* Write comments here */ class dangle { public static void main (String args[]) { int shape,circle,rectangle,radius,distance; circle=1; rectangle=2; radius=5; distance=6; shape=1; if (shape==circle) { if (radius >= distance) System.out.println("Point is enclosed in the circle"); else System.out.println("Point is not enclosed in the circle"); } else System.out.println("Shape is not circle");

Loops Needed in problems that require solving the same subproblem over and over Computing the sum of the first 100 natural numbers and putting the result in y Algorithm: y = 1; y = y + 2; y = y + 3; … y = y + 100; Cannot write 99 such additions: use a loop

while while (condition) { statements } Can put anything in “statements” The entire construct is called a while loop statements are executed until condition is true Even before executing it the first time condition is evaluated A while loop may not execute even once

Example class justAnExample { public static void main(String arg[]) { int x = 5; int y = 0; while (x < 10) { y--; x++; } System.out.println(y);

Example class justAnExample { public static void main(String arg[]) { int x = 15; int y = 0; while (x < 10) { y--; x++; } System.out.println(y);

Sum of natural numbers class naturalSum { public static void main(String arg[]) { int n = 2; int y = 1; while (n <= 100) { y += n; n++; } System.out.println(“Sum of the first ” + (n-1) + “ natural numbers is ” + y);