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.

Slides:



Advertisements
Similar presentations
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Advertisements

INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter5: Control Statements Part II.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
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
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 1B) UTPA – Fall 2011.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Chapter 4: Control Structures II
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
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.
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.
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.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) Xiang Lian The University of Texas – Pan American Edinburg, TX
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Java Programming: From Problem Analysis to Program Design, 4e 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.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
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.
JavaScript: Control Statements.
MSIS 655 Advanced Business Applications Programming
The University of Texas Rio Grande Valley
The University of Texas – Pan American
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
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 6 Control Statements: Part 2
Presentation transcript:

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 Edinburg, TX

Objectives In this chapter, you will do exercises about: – more control structures Repetition statements – for, do … while Selection – Switch – break and continue statements – logical operators in C# 2

Multiple Choices Typically, ______ statements are used for counter-controlled repetition. – A. for B. while C. repetition D. until Typically, ______ statements are used for sentinel-controlled repetition. – A. for B. while C. repetition D. until The do … while statement tests the loop-continuation condition ________ executing the loop’s body; therefore, the body always executes at least once. – A. before B. after C. at the same time D. None of the above The _________statement, when executed in a repetition statement, skips the remaining statements in the loop body and proceeds with the next iteration of the loop. – A. break B. exit C. continue D. return 3

Multiple Choices (cont'd) The _____operator can be used to ensure that two conditions are both true before choosing a certain path of execution – A. and B. && C. or D. || If the loop continuation condition in a for header is initially ___________, the for statement’s body does not execute – A. true B. false C. 0 D. 1 Which of the following is the appropriate for statement for varying the control variable over the following sequence of values: 25, 20, 15, 10, 5? – A. for (i = 5; i<25; i+=5) B. for (i = 5; i<=25; i-=5) – C. for (i = 25; i =5; i-=5) An infinite loop occurs when the loop-continuation condition in a while or do…while statement______. – A. never becomes true B. never becomes false C. is false D. is true for finite times 4

True/False Statements The default label is required in the switch selection statement. The break statement is required in every case of a switch statement The expression ((x>y)&&(a y) is true or (a<b) is true. An expression containing the || operator is true if either or both of its operands are true. The integer after the comma (,) in a format item (e.g., {0, 4}) indicates the field width of the displayed string. To test for a range of values in a switch statement, use a hyphen (-) between the start and end values of the range in a case label. 5

Recall: 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"); return; // default: Console.WriteLine("Grade: F"); break; // < 60 } 6

True/False Statements (cont'd) Listing cases consecutively with no statements between them enables the cases to perform the same set of statements. Boolean logical operator (|) performs short-circuit evaluation. The && operator has a higher precedence than the || operator. The following statement alters the control variable from 0 to 50 in increment of 5? – for (int i=0;i<49; i+=5) 7

What Does the Code Do? public class Printing { public static void Main(string[] args) { for (int i = 1; i<=10; i++) { for (int j = 1; j<=5; j++) Console.Write Console.WriteLine(); } 8

What Does the Code Do? (cont'd) // What is the output of the following code? int x=1; int y=2; if ( x == 2 && y=4) Console.WriteLine ("expression is true"); else Console.WriteLine ("expression is false"); Console.WriteLine ("y="+y); 9

Debugging Errors The following code is to display whether integer value is odd or even Switch( value%2) { case 0; Console.WriteLine("Odd integer"); case 1: Console.Writeline("Even integer") } 10

Debugging Errors (cont'd) i=1 while (i <= 10); ++i; } For (k=0.1; k!=1.0; k+=0.1) Console.WriteLine(k); 11

Write a Program Use nested for statement to write a program to display the triangle of asterisks in outputTextBox. Use the following statements: – outputTextBox.AppendText("*"):displays asterisks one at a time – outputTextBox.AppendText(Environment.NewLine) – outputTextBox.AppendText(" "): inserts a space 12 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Write a Program (cont'd) Use nested for statement to write a program to display a rectangle in outputTextBox. Use the following statements: – outputTextBox.AppendText("+") – outputTextBox.AppendText(Environment.NewLine) – outputTextBox.AppendText(" "): inserts a space | |

Exercises After the Class Chapter 6 in your textbook – Self-Review Exercises – Quick Quiz – Exercises 14

15