Section 2 - Selection and Repetition. Equality and Relational Operators.

Slides:



Advertisements
Similar presentations
ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Advertisements

Statement-Level Control Structures
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.
Repeating Instructions
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Repeating Instructions
C# Programming: From Problem Analysis to Program Design
True or false A variable of type char can hold the value 301. ( F )
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Repeating Instructions
Copyright © 2012 Pearson Education, Inc. Chapter 2 Introduction to Visual C#
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
C# Programming: From Problem Analysis to Program Design1 Making Decisions C# Programming: From Problem Analysis to Program Design 3rd Edition 5.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
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
Getting Started Example ICS2O curriculum
Advanced Programming LOOP.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
sequence of execution of high-level statements
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Control Structures sequence of execution of high-level statements.
1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command.
Loops ISYS 350. Compute the sum of a list of numbers: Example: 5, 2, 10, 8, 4 Process: Sum= 0 Get a number from the list Sum = Sum + the number Repeat.
1 Iterative Statements Repeated execution of a (compound) statement by iteration or recursion –Iteration is statement level –Recursion is unit-level control.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled.
Advanced Programming LOOP. 2 while Repetition Structure Repetition Structure –An action is to be repeated Continues while statement is true Ends when.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
Session 1 C# Basics.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
W E E K F I V E Control Flow. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements Iterative Statements.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.
Lecture 2 Review of 1301 and C# Richard Gesick. Common data types int, long, double, float, char, bool, string.
1 Statements © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Microsoft Visual C# 2010 Fourth Edition Chapter 3 Using GUI Objects and the Visual Studio IDE.
CSIT 108 Review Visual Basic.NET Programming: From Problem Analysis to Program Design.
Information and Computer Sciences University of Hawaii, Manoa
C# Programming: From Problem Analysis to Program Design
Microsoft Visual C# .NET: From Problem Analysis to Program Design
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.
2.5 Another Java Application: Adding Integers
Chapter 4 – Control Structures Part 1
Chapter 8: Control Structures
Chapter 3: Understanding C# Language Fundamentals
Java Programming: Guided Learning with Early Objects
Advanced Programming Lecture 02: Introduction to C# Apps
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
Chapter 3 – Introduction to C# Programming
Chapter 5 – Control Structures: Part 2
Presentation transcript:

Section 2 - Selection and Repetition

Equality and Relational Operators

Relational Operators int aValue = 100, bValue = 1000; string sValue = “CS158”; decimal money = 50.22m; double dValue = 50.22; char cValue = ‘A’;

Logical Operators

C# Operators The table on the following slide describes the allowable operators, their precedence, and associativity. Left associativity means that operations are evaluated from left to right. Right associativity mean all operations occur from right to left - Assignment operator, for example - everything to the right is evaluated before the result is placed into the variable on the left.

Use round brackets ( ) to impose your own precedence on expression evaluation

Decision Structures - Selection if (Boolean expression) statement ; The basic selection statement is the if statement. Used to execute or skip a statement or block of statements { … } depending on a Boolean expression, Also have an if … else … construct Same syntax for C++, C#, and Java

Examples if (x == value100) { Console.Write("x is "); Console.Writeline( x ); } if (x == value100) Console.Writeline("x is 100"); else Console.Writeline("x is not 100");

if (salesForYear > ) { Console.WriteLine( ); Console.WriteLine(“YES...you get a bonus!”); bonusAmount = ; } if (hoursWorked > 40) { payAmount = (hoursWorked – 40) * payRate * payRate * 40; Console.WriteLine(“You worked {0} hours overtime.”, hoursWorked – 40); } else payAmount = hoursWorked * payRate;

Switch construct Same syntax for C++, C#, and Java switch can only be used to compare an expression with different constants. The types of the values a switch statement operates on can be booleans, integer types, and strings ( null is acceptable as a case label). Every statement sequence in a case must be terminated with break If no case label matches  default

switch (expression) { case constant1: block of instructions 1 break; case constant2: block of instructions 2 break;... default: default block of instructions break; } You may also include a default choice following all other choices. If none of the other choices match, then the default choice is taken and its statements are executed. Use of the default choice will help catch unforeseen circumstances and make your programs more reliable.

switch (x) { case 5: Console.Writeline("x is 5"); break; case 99: Console.Writeline("x is 99"); break; default: Console.Writeline("value of x unknown"); break; }

switch(country) { case"Germany": case"Austria": case"Switzerland": language = "German"; break; case"England": case"USA": language = "English"; break; case null: Console.WriteLine("no country specified"); break; default: Console.WriteLine("don't know language of", country); break; }

Loops while (Boolean expression) {statements} int x – 10; while ( x > 1 ) { Console.Writeline("x is "); Console.Writeline( x ); x--; } Same syntax for C++, C#, and Java

int sum = 0; int number = 1; while (number < 11) { sum = sum + number; number++; } Console.WriteLine(“Sum of values ” + “1 through 10” + “ is ” + sum);

do {statements} while (Boolean expression) int x = 10; do { Console.Write("x is "); Console.Writeline( x ); x--; } while (x !=1 );

Loops for (initialization; condition; in(de)crement) {statement;} for (int n=10; n>0; n--) { Console.WriteLine( n ); } Same syntax for C++, C#, and JAVA

Examples x = 5; if (x > 0 && x < 10) count++; else if (x == -1)... else {... } while (x > 0) {... x--; } for (int k = 0; k < 10; k++) {... }

foreach Construct Specialized foreach loop provided for collections like arrays –reduces risk of indexing error –provides read only access int[] data = { 1, 2, 3, 4, 5 }; int sum = 0; foreach (int x in data) { sum += x; } foreach typevaluearray

foreach statement For looping over arrays int[] a = {3, 17, 4, 8, 2, 29}; foreach (int x in a) sum += x; //sum numbers in array string s = "Hello"; foreach (char ch in s) Console.WriteLine(ch);

Windows Applications Using Loops Event-driven model –Manages the interaction between user and GUI by handling repetition for you Designed with graphical user interface (GUI) Predefined class called MessageBox –Used to display information to users through its Show( ) method

using System; using System.Text; using System.Windows.Forms; public class HelloWorld { public static void Main() { MessageBox.Show("Hello World!"); }

Windows Applications Example using System; using System.Windows.Forms; class SquaredValues { static void Main( ) { int counter = 0; string result =""; while (counter < 10) { counter++; result += " \t“+ counter + " \t" + Math.Pow(counter, 2) + "\n"; } MessageBox.Show(result, “1 through 10 and their squares”); }

Windows Applications To use MessageBox class in console application –Add a reference to System.Windows.Forms View > Solutions Explorer Right-click on the Reference folder –Select Add Reference –Look for and select System.Windows.Forms –Click OK –Add using directive to System.Windows.Forms namespace in program using System.Windows.Forms;

MessageBox.Show( ) method is overloaded – may be called with different number of parameters First parameter – String displayed in window Second parameter– Caption for Window title bar Third parameter– Type of dialog button Fourth parameter– Button type

MessageBox class MessageBox.Show("Do you want another number ?", “Numbers", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

MessageBox class (continued) MessageBox.Show("Do you want another number ?", “Numbers", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

static void Main() { bool moreData = true; Random number = new Random(); int s = number.next(100); while (moreData) { Console.WriteLIne(s); if (MessageBox.Show("Do you want another number ?", “Numbers", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) // Test to see if No clicked { moreData = false; } else { s = number.Next(100); }

MessageBox.Show( ) Method 1 st parameter 4 th parameter 2 nd parameter 3 rd parameter MessageBox.Show("Do you want another number ?", “Numbers", MessageBoxButtons.YesNo, MessageBoxIcon.Question);