Selection and repetitionBjørk Busch1 Selection and repetition statements in C# Selection - constructions If switch Repetition / itteration / loops - constructions.

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Written by: Dr. JJ Shepherd
Control Structures Corresponds with Chapters 3 and 4.
8-May-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
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.
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Loops – While, Do, For Repetition Statements Introduction to Arrays
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Week 11 Recap CSE 115 Spring Want to write a programming language? You’ll need three things: You’ll need three things: –Sequencing –Selection Polymorphism.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
Arrays, Conditionals & Loops in Java. Arrays in Java Arrays in Java, are a way to store collections of items into a single unit. The array has some number.
The switch Statement, DecimalFormat, and Introduction to Looping
TODAY’S LECTURE Review Chapter 2 Go over exercises.
UNIT II Decision Making And Branching Decision Making And Looping
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a.
“Introduction to Programming With Java” Lecture - 6 U MESH P ATIL
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
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.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
1 ENERGY 211 / CME 211 Lecture 6 October 3, 2008.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Control Flow Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 8 Java Fundamentals Control Structures Fri.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 How did your team meetings go? A list of three possible “adoptable” projects is linked near the bottom.
Written by: Dr. JJ Shepherd
COMP Loop Statements Yi Hong May 21, 2015.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
C syntax (simplified) BNF. Program ::= [ ] Directives ::= [ ] ::= | |… ::=#include > ::=#define.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
CS 1023 Intro to Engineering Computing 3: Computer Programming Looping Statements Mark Kerstetter Department of Computer Science Western Michigan University.
Switch-Case Statement. What is a switch-case?  The switch-case statement is really nothing more than a glorified.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
SWE 510: Object Oriented Programming in Java
The switch Statement, and Introduction to Looping
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.
Compound Condition Break , Continue Switch Statements in Java
Unit-1 Introduction to Java
Java for Android is specific
Flow of Control.
Alice in Action with Java
null, true, and false are also reserved.
Conditional Statements
CISC124 Labs start this week in JEFF 155. Fall 2018
Chapter 4: Control Structures I (Selection)
Conditionals.
CMPE212 – Reminders The other four assignments are now posted.
Program Flow.
If-statements & Indefinite Loops
Fundamental Programming
Welcome back to Software Development!
Additional control structures
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Arrays Introduction to Arrays Reading for this Lecture:
CSS 161: Fundamentals of Computing
Presentation transcript:

Selection and repetitionBjørk Busch1 Selection and repetition statements in C# Selection - constructions If switch Repetition / itteration / loops - constructions while do while for foreach (for itteration over collection and arays)

Selection and repetitionBjørk Busch2 Selection statements in C# If statement Basic syntax: if ( boolean-expression ) if-statement ; else else-statement; The else section is optional. The if-statement and else-statement may be a compound statement Compound stament is { statement-1; statement-2; …. statement-n; }

Selection and repetitionBjørk Busch3 Selection statements in C# (continued) If and else statements may be nested. For multiselection you can use the short nested form. if ( boolean-expression-1 ) if-1-statement; else if ( boolean-expression-2 ) if-2-statement; ……. else if ( boolean-expression-n ) if-n-statement; else else-n-statement; For the nestede if and else the statement could also be a compound statement {…..}

Selection and repetitionBjørk Busch4 Selection statements in C# Switch statements switch ( switch-expression ) { case case-1-value : case-1-statement-1; case-1-statement-n1; break ; case case-2-value : case-2-statement-1; case-2-statement-n; break ; default : default-statement-1; default-statement-n; break ; } Switch-expression can be int, boolean, char or string (so come on java and C++) Case-value must be of the same type as the switch-expression. You may exclude all the statements for the case, if you want to fall through to the nest case or for the last case to the default.

Selection and repetitionBjørk Busch5 Repetition statements in C# while-loops while ( boolean-expression ) while-statement ; Loops while the boolean-expression is true. The while-statement may be a compound or an empty statement. You can break out of a while-loop by the ”break;” statement. do-while-loops do do-while-statement ; while ( boolean-expression ); Loops while the boolean-expression is true. The do-while-statement may be a compound or an empty statement. You can break out of a do-while-loop by the ”break;” statement.

Selection and repetitionBjørk Busch6 Repetition statements in C# for-loops for ( init-statement ; boolean-expression ; in-/decremental-statement ) for-statement ; Loops while the boolean-expression is true. The init-statement is done once before loops. The in-/decremental-statement is done at end of each loop. The tree statements may be a compound or an empty statement. Normaly only the for-statement are a compound statement. You can break out of a for-loop by the ”break;” statement. The for-statement can be seen as a compact while construction. The only difference is that variables declared in the init-statement, don’t live after the loop has ended.

Selection and repetitionBjørk Busch7 Repetition statements in C# foreach-loops foreach ( foreach-class foreach-object in foreach-collection ) foreach-statement ; Loops while there are an foreach-object in the foreach-collection. Each object in the collection must be of foreach-class, as they are typecasted to the foreach-class. The foreach-statement may be a compound. You can break out of a foreach-loop by the ”break;” statement. The foreach-collection must implement the IEnummerable interface. In the System.Collections namespace library you find a lot of collections that can be uses. You can use C# array’s of one or more dimension.