CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall 2012 1.

Slides:



Advertisements
Similar presentations
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter5: Control Statements Part II.
1 CIS Jan Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
1 CSCE 1030 Computer Science 1 Control Statements in Java.
Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
1 Chap 4. Data Should know Declare & Initialize variables Declare constants Assignment Operators Increment and Decrement Operators Precedence of Operators.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
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.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Chapter 05 (Part II) Control Statements: Part II.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2B) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Looping Increment/Decrement Switch. Flow of Control Iteration/Switch Statements.
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.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Chapter 9 Repetition.
Chapter 4 – C Program Control
Control Statements: Part 2
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2B) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
CiS 260: App Dev I Chapter 4: Control Structures II.
JavaScript: Control Statements.
JavaScript: Control Statements I
Chapter 5 Repetition.
Chapter 5- part 2 Control Statements: Loops 2
JavaScript: Control Statements (II)
MSIS 655 Advanced Business Applications Programming
The University of Texas Rio Grande Valley
The University of Texas – Pan American
Outline Altering flow of control Boolean expressions
The University of Texas – Pan American
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises UTPA – Fall 2012 This set of slides is revised from lecture.
The University of Texas – Pan American
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises UTPA – Fall 2012 This set of slides is revised from.
Chapter 5 Control Statements: Loops 2
Chapter 6 Control Statements: Part 2
Control Statements Paritosh Srivastava.
PROGRAM FLOWCHART Iteration Statements.
Control Statements:.
Presentation transcript:

CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall

Objectives In this chapter, you will: – Learn more control structures Repetition statements – for, do … while Selection – switch – Know how to use break and continue statements to terminate the loop or current iteration – Learn how to use logical operators in C# 2

Repetition Structure Visual C# provides 3 repetition statements – while – for – do … while 3

In the Last Class: Counter Controlled while Loop – int Counter =1; – while (Counter <=10) – { … Counter ++; – }//does it 10 times 4

Example of for Repetition Statement Output even numbers between 2 and 10 for (int counter = 2; counter<= 10; counter+=2) { Console.Write("{0} ", counter); Console. WriteLine(); } 5

Discussions on for Repetition Statement for (int counter = 2; counter <= 10; counter += 2) – Keywords: for – "int counter" declares a counter of integer data type counter – control variable name int – control variable type – Initial value of control variable – 2 – Final value of control variable – 10 6

General Form of a for Statement For (initialization; loopContinuationCondition; increment) { statement } 7

Other Examples of for Statement (1) Declaring the control variable before a for statement – int counter; for (counter = 2; counter <= 10; counter+=2) Console.Write("{0} ", counter); Using expressions in the for statement – for (counter = x; counter <= 4*x*y; counter+=y/x) Console.Write("{0} ", counter); 8

Other Examples of for Statement (2) Increment: 1 for (counter = 2; counter <= 10; counter++) Decrement for (counter = 10; counter >= 2; counter-=2) 9

Example of Interest decimal amount; decimal principal = 1000; double rate = 0.05; Console.WriteLine("Year{0, 20}", "Amount on deposit"); for (int year = 1; year <= 10; year++) { amount = principal *((decimal)Math.Pow(1.0+rate, year)); } 10 field width

do…while Repetition Statements The loop body is always executed at least once int product = 1 do { product = product * 3; }while (product <=100); 11

Use break to Terminate Repetition Statements for (int counter = 1; counter <=10; counter ++) { if (counter == 5) break; Console.Write("{0} ", counter); } // answer:

Use continue to Terminate the Current Iteration for (int counter = 1; counter <=10; counter ++) { if (counter == 5) continue; Console.Write("{0} ", counter); } // answer:

In the Last Class: Selection Structures in C# if – single selection statement if … else – double selection statement switch – multiple selection statement 14 display "passed" [grade>=60] [grade<60]

Example of Multiple Selection Statement public void CalculateGrade(int grade) { switch (grade/10) { case 9: // case 10: // 100 Console.WriteLine("Grade: A"); break; case 8: Console.WriteLine("Grade: B"); break; // case 7: Console.WriteLine("Grade: C"); break; // case 6: Console.WriteLine("Grade: D"); break; // default: Console.WriteLine("Grade: F"); break; // < 60 } 15

Logical Operators Conditional AND operator (&&) – if (gender == 'F' && age >=65) – seniorFemales += 1; 16 Expression 1Expression 2Expression 1 && Expression 2 false truefalse truefalse true

Logical Operators (cont'd) Conditional OR operator (||) 17 Expression 1Expression 2Expression 1 || Expression 2 false true falsetrue

Short-Circuit Evaluation Conditional AND operator (&&) – if (gender == 'F' && age >=65) – If gender is not "F", then "age >=65" will not be evaluated Conditional OR operator (||) – if (gender == 'F' || age >=65) – If gender is "F", then "age >=65" will not be evalutated 18

Boolean Logical Operators Boolean Logical AND ( & ) – if (gender == 'F' & age >=65) – No matter what value gender has, “age>=65” will always be evaluated Boolean Logical OR ( | ) Logical Negative ( ! ) 19

Other Boolean Logical Operators Boolean Logical Exclusive OR (^) – If both conditions are the same, then output is false 20 Expression 1Expression 2Expression 1 ^ Expression 2 False True FalseTrue False