Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming
Advertisements

Control Structures Corresponds with Chapters 3 and 4.
Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators.
Chapter 6 Horstmann Programs that make decisions: the IF command.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
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.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
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.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007.
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
UNIT II Decision Making And Branching Decision Making And Looping
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 7 Decision Making : selection statements.
Java Programming: From The ground Up  Chapter 4 Selection and Decision: if Statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
1 Week 6 Branching. 2 What is “Flow of Control”? l Flow of Control is the execution order of instructions in a program l All programs can be written with.
1 2. Program Construction in Java. 2.4 Selection (decisions)
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
Chapter Making Decisions 4. Relational Operators 4.1.
Chapter 3 1 l Branching l Loops l exit(n) method l Boolean data type and logic expressions Flow of Control – Part 1.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Control Flow Statements
Decision Statements, Short- Circuit Evaluation, Errors.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
LESSON 2: CONTROL STRUCTURES JAVASCRIPT. CONTROL STRUCTURES – ALLOWS US TO CHANGE THE ORDERING OF HOW THE STATEMENTS IN OUR PROGRAMS ARE EXECUTED.
Control Statements: Part1  if, if…else, switch 1.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Branching statements.
Decision Making in C.
CS0007: Introduction to Computer Programming
The switch Statement, and Introduction to Looping
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Lecture 3- Decision Structures
Flow of Control.
SELECTION STATEMENTS (1)
Control Structures.
Chapter 3:Decision Structures
Control Structures: Selection Statement
Structured Program Development in C++
PROGRAM FLOWCHART Selection Statements.
Control Structures: Selection Statement
Branching statements Kingdom of Saudi Arabia
Presentation transcript:

Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011

Control Structures

Objectives Use decision control structures (if, else, switch) allows selection of specific sections of code to be executed Use repetition control structures (while, do-while, for) allow executing specific sections of code a number of times Use branching statements (break, continue, return) allows redirection of program flow

Control Structures Control structures  allows us to change the ordering of how the statements in our programs are executed Two types of Control Structures  Decision control structures allows us to select specific sections of code to be executed  Repetition control structures allows us to execute specific sections of the code a number of times

Decision Control Structures Decision control structures  Allow us to select and execute specific blocks of code while skipping other sections Types:  if-statement  if-else-statement  If-else if-statement

if-statement  specifies that a statement (or block of code) will be executed if and only if a certain boolean statement is true. if-statement has the form: if( boolean_expression ) statement; or if( boolean_expression ){ statement1; statement2; }  where, boolean_expression is either a boolean expression or boolean variable.

if-statement Flowchart bolean_expr statement true false

Example 1  Write a Java code that for a student that has a grade greater than 60 will print out a “Congratulations!” message. int grade = 68; if( grade > 60) System.out.println("Congratulations!");

Example 2  Write a Java code that for a student that has a grade greater than 60 will print out first a “Congratulations!” message and in the next line the “You passed!” message. int grade = 68; if( grade > 60 ){ System.out.println("Congratulations!"); System.out.println("You passed!"); }

Coding Guidelines  1. The boolean_expression part of a statement should evaluate to a boolean value. That means that the execution of the condition should either result to a value of true or a false.  2. Indent the statements inside the if-block. For example, if( boolean_expression ){ //statement1; //statement2; }

if-else statement  used when we want to execute a certain statement if a condition is true, and a different statement if the condition is false. if-else statement has the form: if( boolean_expression ){ statement1; statement2;... } else{ statement3; statement4;... }

if-else statement Flowchart bolean_expr statement true false statement

Example 1  Write a Java code that for a student:  That has a grade greater than 60 will print out “Congratulations!”.  That has a grade less than or equal to 60 will print out “Sorry you failed”); int grade = 68; if( grade > 60 ) System.out.println("Congratulations!"); else System.out.println("Sorry you failed");

Example 2  Write a Java code that for a student:  That has a grade greater than 60 will print out “Congratulations!” and in the next line “You passed!”.  That has a grade less than or equal to 60 will print out “Sorry you failed”);  int grade = 68;  if( grade > 60 ){  System.out.println("Congratulations!");  System.out.println("You passed!");  }  else{  System.out.println("Sorry you failed");  }

Recommendations  1. To avoid confusion, always place the statement or statements of an if or if-else block inside brackets {}.  2. You can have nested if-else blocks. This means that you can have other if-else blocks inside another if-else block. For example: if( boolean_expression ){ if( boolean_expression ){ //some statements here } } else{ //some statements here }

if-else-if statement The statement in the else-clause of an if-else block can be another if-else structures. This cascading of structures allows us to make more complex selections. The statement has the form: if( boolean_expression1 ) statement1; else statement 2; if( boolean_expression2 ) statement2; else statement3;

if-else-if statement Flowchart bolean_expr 1 statement 1 true false bolean_expr 2 statement 2statement 3

Example Write a Java code that for a student: If grade is greater than or equal to 90 will print out “Excellent!” If grade is greater than 60 will print out “Very good” That has a grade less than or equal to 60 will print out “Sorry you failed”. int grade = 68; if( grade > =90 ){ System.out.println(“Excellent!"); } else if( grade > 60 ){ System.out.println("Very good!"); } else{ System.out.println("Sorry you failed"); }

Common Errors  1. The condition inside the if-statement does not evaluate to a boolean value. For example, //WRONG int number = 0; if( number ){ //some statements here } The variable number does not hold a boolean value.  2. Writing elseif instead of else if.

Common Errors  3. Using = instead of == for comparison. For example, //WRONG int number = 0; if( number = 0 ){ //some statements here } This should be written as, //CORRECT int number = 0; if( number == 0 ){ //some statements here }

Sample Program 1public class Grade { 2 public static void main( String[] args ) 3 { 4 double grade = 92.0; 5 if( grade >= 90 ){ 6 System.out.println( "Excellent!" ); 7 } 8 else if(grade >= 80) { 9 System.out.println("Good job!" ); 10 } 11 else if( grade >= 60 ){ 12 System.out.println("Study harder!" ); 13 } 14 else{ System.out.println("Sorry, you failed."); 15 } 16 } 17 }

switch-statement switch  allows branching on multiple outcomes. switch statement has the form: switch( switch_expression ){ case case_selector1: statement1; statement2; break; case case_selector2: statement1; statement2; break; default: statement1; statement2; }

switch-statement Where:  switch_expression is an integer or character expression  case_selector1, case_selector2 and so on, are unique integer or character constants.

switch-statement When a switch is encountered,  Java first evaluates the switch_expression, and jumps to the case whose selector matches the value of the expression.  The program executes the statements in order from that point on until a break statement is encountered, skipping then to the first statement after the end of the switch structure.  If none of the cases are satisfied, the default block is executed. Take note however, that the default part is optional.

switch-statement NOTE:  Unlike with the if statement, the multiple statements are executed in the switch statement without needing the curly braces.  When a case in a switch statement has been matched, all the statements associated with that case are executed. Not only that, the statements associated with the succeeding cases are also executed.  To prevent the program from executing statements in the subsequent cases, we use a break statement as our last statement.

Flowchart

Example 1public class Grade { 2 public static void main( String[] args ) 3 { 4 int grade = 92; 5 switch(grade){ 6 case 100: 7 System.out.println( "Excellent!" ); 8 break; 9 case 90: 10 System.out.println("Good job!" ); 11 break; 12case 80: 13 System.out.println("Study harder!" ); 14 break; 15default: 16 System.out.println("Sorry, you failed."); 17 } 18 } 19} It will print: “Sorry, you failed!” Why????

Coding Guidelines  1. Deciding whether to use an if statement or a switch statement is a judgment call. You can decide which to use, based on readability and other factors.  2. An if statement can be used to make decisions based on ranges of values or conditions, whereas a switch statement can make decisions based only on a single integer or character value. Also, the value provided to each case statement must be unique.