The University of Texas – Pan American

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look 1 Xiang Lian The University of Texas – Pan American Edinburg,
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 3: Control Structures (Part 1) – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg,
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
The University of Texas – Pan American
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
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.
1 Chap 4. Data Should know Declare & Initialize variables Declare constants Assignment Operators Increment and Decrement Operators Precedence of Operators.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX.
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.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) Xiang Lian The University of Texas – Pan American Edinburg, TX
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.
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.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 7: Introduction to Object- Oriented Programming in Python – Exercises Xiang Lian The University of.
Chapter 9 Repetition.
Lecture 4b Repeating With Loops
Chapter 5- Control Structures: Part 2
Control Structures.
Problem Solving and Control Statements: Part 2
Control Statements: Part 2
Control Structures: 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 – Control Structures: Part 2
The University of Texas – Pan American
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
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I UTPA – Fall 2012 This set of slides is revised from lecture slides.
Outline Altering flow of control Boolean expressions
The University of Texas – Pan American
Chapter 8 JavaScript: Control Statements, Part 2
Advanced Programming Chapters 5 & 6: Control Structures
Lecture Notes – Week 3 Lecture-2
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.
CSCI 3328 Object Oriented Programming in C# Review: Exam I
The University of Texas – Pan American
The University of Texas – Pan American
The University of Texas Rio Grande Valley
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
The University of Texas – Pan American
CSCI 3328 Object Oriented Programming in C# Chapter 3: Introduction to Classes and Objects – Exercises UTPA – Fall 2012 This set of slides is revised.
Chapter 6 Control Statements: Part 2
The University of Texas – Pan American
CSCI 3328 Object Oriented Programming in C# Review: Exam II
CSCI 3328 Object Oriented Programming in C# Review: Final Exam
The University of Texas – Pan American
CSCI 3328 Object Oriented Programming in C# Review: Final Exam
Dale Roberts, Lecturer IUPUI
Control Statements Paritosh Srivastava.
PROGRAM FLOWCHART Iteration Statements.
Chapter 8 JavaScript: Control Statements, Part 2
CSCI 3328 Object Oriented Programming in C# Review: Exam II
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays – Exercises UTPA – Fall 2012 This set of slides is revised from lecture slides of Prof.
Control Statements:.
Presentation transcript:

The University of Texas – Pan American CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa.edu This set of slides is revised from lecture slides of Prof. John Abraham. -- Xiang Lian

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#

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

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

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(); }

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

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

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)

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

Example of Interest String.Format("{0, 20}", "..."); 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)); } field width String.Format("{0, 20}", "...");

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

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

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

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

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

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

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

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

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 ( ! )

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