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.

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.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
© 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
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
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.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
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.
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.
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:
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Introduction to Computer Programming
Java Fundamentals 4.
Lecture 4b Repeating With Loops
Chapter 4 – C Program Control
Chapter 4 C Program Control Part II
Control Statements: Part 2
Control Structures: Part 2
Chapter 4 - Program Control
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 II
The University of Texas – Pan American
Chapter 5- part 2 Control Statements: Loops 2
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.
The University of Texas – Pan American
Outline Altering flow of control Boolean expressions
The University of Texas – Pan American
Chapter 8 JavaScript: Control Statements, Part 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.
Chapter 4 - Program Control
The University of Texas – Pan American
The University of Texas – Pan American
CSCI 3328 Object Oriented Programming in C# Review: Exam II
3 Control Statements:.
Chapter 5 Control Statements: Loops 2
Control Statements Loops.
Program Control Topics While loop For loop Switch statement
Chapter 6 Control Statements: Part 2
CSCI 3327 Visual Basic Review: Exam I
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Control Statements Paritosh Srivastava.
Chapter 4 - Program Control
Loops CGS3416 Spring 2019 Lecture 7.
Chapter 8 JavaScript: Control Statements, Part 2
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:

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 lecture slides of Prof. John Abraham. -- Xiang Lian

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#

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. 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

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) D. 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

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<b)) is true if either (x>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.

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

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)

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

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

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") }

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

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 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

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