Sahar Mosleh California State University San MarcosPage 1 Program Control Statement.

Slides:



Advertisements
Similar presentations
Control Structures.
Advertisements

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.
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.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
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:
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.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
CSCI1402: Lecture 2 Week 6 Dr. David A. Elizondo Centre for Computational Intelligence School of Computing Office: Gateway 6.61
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
The switch Statement, DecimalFormat, and Introduction to Looping
JAVA Control Statement.
UNIT II Decision Making And Branching Decision Making And Looping
L EC. 03: C ONTROL STATEMENTS Fall Java Programming.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Control Structures if else do while continue break switch case return for.
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
Compound Statements If you want to do more than one statement if an IF- else case, you can form a block of statements, or compound statement, by enclosing.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
Switch Statement Is a selection control structure for multi-way branching. SYNTAX switch ( IntegralExpression ) { case Constant1 : Statement(s); // optional.
1 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX do { Statement } while.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
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.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/29 The switch Statement The switch statement provides another way.
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.
Sahar Mosleh California State University San MarcosPage 1 Data Types and Operators.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Control Flow Statements
CSI 3125, Preliminaries, page 1 Control Statements.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Loops and Logic. Making Decisions Conditional operator Switch Statement Variable scope Loops Assertions.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Introduction to Control Statements IT108 George Mason University.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Lecture 4b Repeating With Loops
The switch Statement, and Introduction to Looping
Control Structures.
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Additional Control Structures
محاضرة 1: مقدمة للمسـاق و مراجعـة للأساسيـات
Lab5 PROGRAMMING 1 Loop chapter4.
Conditionals.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Repetition Statements
Loops CGS3416 Spring 2019 Lecture 7.
Presentation transcript:

Sahar Mosleh California State University San MarcosPage 1 Program Control Statement

Sahar Mosleh California State University San MarcosPage 2 Example of simple loop class Grade{ public static void main(String args[]) { } public static void FindLetterGrade(int Score) { char LetterGrade; if ( Score =90 ) { LetterGrade = 'A'; System.out.println ("The letter Grade is " + LetterGrade); } else if ( Score =80 ) { LetterGrade = 'B'; System.out.println ("The letter Grade is " + LetterGrade); } else if (Score =70) { LetterGrade = 'C'; System.out.println ("The letter Grade is " + LetterGrade); } else if (Score =60) { LetterGrade = 'D'; System.out.println ("Thr letter Grade is " + LetterGrade); } else { LetterGrade = 'F'; System.out.println ("The letter Grade is " + LetterGrade); }

Sahar Mosleh California State University San MarcosPage 3 Nested loop Sometimes, the body of a loop is again a loop. We say that the inner loop is nested inside an outer loop. This happens often when you process two- dimensional structures, such as tables. Lets look at an example that looks a bit more interesting than a table of numbers. We want to generate the following triangular shape: [] [] [] [] [] [] [] [] [] [] []

Sahar Mosleh California State University San MarcosPage 4 The basic idea is simple. We generate a sequence of rows: for ( int i=1; i<= width; i++ ) { // Make triangle row … } How you make a triangle row? Use another loop to concatenate the squares [] for that row. Then add a newline character at the end of the row. The ith row has i symbols, so the loop counter goes from 1 to i. For ( int j=1; j<= i; j++) r = r + “[]”; r = r + “\n”;

Sahar Mosleh California State University San MarcosPage 5 Putting both loops together yields two nested loops: String r= “”; for ( int i=1; i<= width; i++ ) { // Make triangle row For ( int j=1; j<= i; j++) r = r + “[]”; r = r + “\n”; System.out.println( r ); }

Sahar Mosleh California State University San MarcosPage 6 The while loop The general form of the while loop is: while (condition) statement; where statement can be a single statement or a block of statements, and condition that controls the loop, and it may be any valid boolean expression. The loop repeats while the condition is true When the condition becomes false, program control passes to the line immediately following the loop.

Sahar Mosleh California State University San MarcosPage 7 class WhileDemo { public static void main(String args[]) { char ch; // print the alphabet using a while loop ch = ‘a’; while (ch <=‘z’) { System.out.print(ch); ch++; } Output: abcdefghijklmnopqrstuvwxyz

Sahar Mosleh California State University San MarcosPage 8 Break statement break statement within the body of the loop forces an immediate exit from a loop, bypassing any remaining code in the body of the loop and the loop’s conditional test. If the break statement in the loop is executed, the loop is terminated and the control resumes at the next statement following the loop.

Sahar Mosleh California State University San MarcosPage 9 class BreakDemo { public static void main(String args[]) { int num = 100; // loop while i-squared is less than num for (int i=0; i < num; i++) { //terminate the loop if i*i < num if (i*i >=num) break; System.out.print(i+ “ ”); } System.out.println(“Loop complete.”); } Output: Loop complete.

Sahar Mosleh California State University San MarcosPage 10 Continue statement This is the complement of break statement. Continue forces the next iteration of a loop. class ContDemo { public static void main(String args[]) { int i; // print even numbers between 0 and 100 for (i=0;i<100; i++) { if ((i%2) != 0) continue; System.out.println(i); }

Sahar Mosleh California State University San MarcosPage 11 The Switch statement Switch provides for a multiway branch Although, the nested-if ladder can do the same thing in some situations, it is more feasible to use switch block The general form of switch block is: switch(expression) { casecondition1: statement sequence; break casecondition2: statement sequence; break; casecondition3: statement sequence; break... … default: statement sequence; }

Sahar Mosleh California State University San MarcosPage 12 Rules of switch block The switch expression must be of type char, byte, short, or int. (floating point expressions are not allowed) The case constants must be literals of the type compatible with the expression No two case constant must be identical The default statement is optional in the switch block and is executed if all other options fails.

Sahar Mosleh California State University San MarcosPage 13 class SwitchDemo { public static void main(String args[]) { int I; I = 3; switch (I) { case 0: System.out.println(“I is zero”);break; case 1: System.out.println(“I is one”);break; case 2: System.out.println(“I is two”);break; case 3: System.out.println(“I is three”);break; default: System.out.println(“probably, I is four or more”); }