CIS 3301 C# Lesson 3 Control Statements - Selection.

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Control Structures.
Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
Module 4: Statements and Exceptions. Overview Introduction to Statements Using Selection Statements Using Iteration Statements Using Jump Statements Handling.
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Control Flow C and Data Structures Baojian Hua
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Week 10 Recap CSE 115 Spring For-each loop When we have a collection and want to do something to all elements of that collection we use the for-each.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Python Control Flow statements There are three control flow statements in Python - if, for and while.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
CIS 3301 C# Lesson 5 Methods. CIS 3302 Objectives Understand the structure of a method. Know the difference between static and instance methods. Learn.
Loops Repeating Code Multiple Times SoftUni Team Technical Trainers Software University
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Copyright 2003 Scott/Jones Publishing Making Decisions.
Methods SWE 344 Internet Protocols & Client Server Programming.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Chapter Making Decisions 4. Relational Operators 4.1.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
COMP Loop Statements Yi Hong May 21, 2015.
Session 02 Module 3: Statements and Operators Module 4: Programming constructs Module 5: Arrays.
1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.
Chapter 3. Control Structure C++ Programing. Conditional structure C++ programming language provides following types of decision making statements. Click.
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Chapter 6: Loops.
Chapter 4 Repetition Statements (loops)
The switch Statement, and Introduction to Looping
Loops in Java.
Chapter 4: Making Decisions.
Flow of Control.
Chapter 3 Branching Statements
Chapter 4: Making Decisions.
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
Chapter 8: Control Structures
The University of Texas – Pan American
Conditional Statements
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 4: Control Structures I (Selection)
By Hector M Lugo-Cordero September 3, 2008
CS2011 Introduction to Programming I Selections (I)
Introduction to Programming
Chap 7. Advanced Control Statements in Java
Controlling Program Flow
Presentation transcript:

CIS 3301 C# Lesson 3 Control Statements - Selection

CIS 3302 Objectives Learn the if statements. Learn the switch statement. Learn how break is used in switch statements. Understand proper use of the goto statement

CIS 3303 The if Statement An if statement allows you to take different paths of logic, depending on a given condition Condition: An expression that evaluates to true or false Basic Syntax: if (boolean expression) { statements } When the condition evaluates to a boolean true, a block of code for that true condition will execute else block executes if condition false

CIS 3304 The if Statement (cont) if (myInt != 0) { Console.WriteLine("Your number {0} is not equal to zero.", myInt); } else { Console.WriteLine("Your number {0} is equal to zero.", myInt); } // Multiple Case Decision if (myInt < 0 || myInt == 0) { Console.WriteLine("Your number {0} is less than or equal to zero.", myInt); } else if (myInt > 0 && myInt <= 10) { Console.WriteLine("Your number {0} is in the range from 1 to 10.", myInt); }

CIS 3305 The if Statement (cont) In C#, the condition must evaluate to a boolean value of either true or false Conditional OR ( || ) will evaluate the second sub-expression only if the first sub- expression evaluates to false Conditional AND ( && ) operator will evaluate the second sub-expression only when the first sub-expression evaluates to true

CIS 3306 The Switch Statement The switch statement executes a set of logic depending on the value of a given parameter Types of the values a switch statement operates on can only be booleans, enums, integral types, and strings

CIS 3307 The Switch Statement (cont) switch (myInt) { case 1: Console.WriteLine("Your number is {0}.", myInt); break; case 2: Console.WriteLine("Your number is {0}.", myInt); break; case 3: Console.WriteLine("Your number is {0}.", myInt); break; default: Console.WriteLine("Your number {0} is not between 1 and 3.", myInt); break; }

CIS 3308 C# Branching Statements Branching statementDescription breakLeaves the switch block continue Leaves the switch block, skips remaining logic in enclosing loop, and goes back to loop condition to determine if loop should be executed again from the beginning. Works only if switch statement is in a loop as described in Lesson 04: Control Statements - Loops.Lesson 04: Control Statements - Loops Goto (Not Recommended) Leaves the switch block and jumps directly to a label of the form " :" return Leaves the current method. Methods are described in more detail in Lesson 05: Methods.Lesson 05: Methods throw Throws an exception, as discussed in Lesson 15: Introduction to Exception Handling.Lesson 15: Introduction to Exception Handling

CIS 3309 C# Lesson 4 Control Statements - Loops

CIS Objectives Learn the while loop. Learn the do loop. Learn the for loop. Learn the foreach loop. Complete your knowledge of the break statement. Teach you how to use the continue statement

CIS The while Loop While loop will check a condition and then continues to execute a block of code as long as the condition evaluates to a boolean value of true Syntax: while ( ) { }

CIS The while Loop (cont) int myInt = 0; while (myInt < 10) { Console.Write("{0} ", myInt); myInt++; }

CIS The do Loop Similar to the while loop, except that it checks its condition at the end of the loop do { // Print A Menu Console.WriteLine("My Address Book\n"); Console.WriteLine("A - Add New Address"); Console.WriteLine("D - Delete Address"); Console.WriteLine("M - Modify Address"); Console.WriteLine("V - View Addresses"); Console.WriteLine("Q - Quit\n"); … Console.Write("Press Enter key to continue..."); Console.ReadLine(); Console.WriteLine(); } while (myChoice != "Q" && myChoice != "q"); // Keep going until the user wants to quit

CIS The for loop Loop includes initialization and condition modification Syntax: for ( ; ; ) { } for (int i=0; i < 20; i++) { if (i == 10) break; if (i % 2 == 0) continue; Console.Write("{0} ", i); }

CIS The foreach Loop Loop used to iterate through the items in a list Syntax: foreach ( in ) { } string[] names = {"Cheryl", "Joe", "Matt", "Robert"}; foreach (string person in names) { Console.WriteLine("{0} ", person); }