BIM313 – Advanced Programming Techniques Flow Control 1.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

Intro to CS – Honors I Control Flow: Branches GEORGIOS PORTOKALIDIS
CSCI 51 Introduction to Programming Dr. Joshua Stough February 10, 2009.
Introduction to Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
Chubaka Producciones Presenta :.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
2012 JANUARY Sun Mon Tue Wed Thu Fri Sat
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 6 Control Structures.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007.
BIM313 – Advanced Programming Techniques C# Basics 1.
Subroutines in Computer Programming Svetlin Nakov Telerik Corporation
CISC474 - JavaScript 03/02/2011. Some Background… Great JavaScript Guides: –
BİL528 – Bilgisayar Programlama II Making Decisions, Loops, Debugging, Designing Objects Using Classes 1.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
1 nd Semester Module3 Selection Statement Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
CIS 234: Control Structures - Selection Dr. Ralph D. Westfall April, 2010.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
1 Session 3: Flow Control & Functions iNET Academy Open Source Web Programming.
Conditional Statements Implementing Control-Flow Logic in C# SoftUni Team Technical Trainers Software University
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
If-else, switch, while, for, do-while Control Statements.
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
WORD JUMBLE. Months of the year Word in jumbled form e r r f b u y a Word in jumbled form e r r f b u y a february Click for the answer Next Question.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
Week 8: Decisions Bryan Burlingame 21 October 2015.
Chapter 3 More Flow of Control Goals: To analyze the use of Boolean expressions To analyze the use of Boolean expressions To introduce the notion of enumerated.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:
ICT Introduction to Programming Chapter 4 – Control Structures I.
2011 Calendar Important Dates/Events/Homework. SunSatFriThursWedTuesMon January
Statements and Control Flow The programs we have seen so far do exactly the same list of instructions every time What if we want to do different things.
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter three Conditional Statements ©
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
July 2007 SundayMondayTuesdayWednesdayThursdayFridaySaturday
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Selections Java.
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
Control Structures – Selection
Objectives Create an array Work with array properties and methods
McDonald’s Kalender 2009.
McDonald’s Kalender 2009.
McDonald’s Kalender 2009.
slides courtesy of Eric Roberts
Chapter 4: Control Structures I (Selection)
McDonald’s calendar 2007.
Decision making and control functions
Chap 7. Advanced Control Statements in Java
McDonald’s calendar 2007.
2015 January February March April May June July August September
Presentation transcript:

BIM313 – Advanced Programming Techniques Flow Control 1

Contents Flow Control – if, switch – while, do-while, for, foreach – Binary operators 2

Flow Control Branching (if, switch, ternary operator) Looping (for, while, do-while, foreach) 3

Comparison Operators OperatorMeaning ==equal to !=not equal to <less than >greater than <=less than or equal to >=greater than or equal to 4

Boolean Variables A Boolean variable may take values true or false – bool isWhite = true; – isWhite = false; Comparison results can be stored in Boolean variables – bool isLong = (height > 195); – bool isWhite = (color == Color.White); 5

Fundamental Logical Operators OperatorNameExample &&AND(a > 0) && (a < 10) ||OR(a = 10) !NOT!(a < 100) 6

The ‘if’ Statement int height; Console.Write("Enter your height (in cm.) "); height = int.Parse(Console.ReadLine()); if (height > 190) Console.WriteLine("You are a tall person!"); else Console.WriteLine("Your height is normal!"); 7

if Statement if (expression) ; if (expression) { ; } 8

if..else if (expression) ; else ; If there are more statements, use curly brackets. 9

Some Notes on ‘if’ Parentheses are required, they can’t be omitted Curly braces (‘{’ and ‘}’)should be used if there are more than one statements: if (test) { statement1; statement2; } else part can be omitted if statements can be nested 10

Example: Finding the smallest of 3 integers int a, b, c, min; Console.WriteLine("Enter 3 integers:"); Console.Write("a = "); a = int.Parse(Console.ReadLine()); Console.Write("b = "); b = int.Parse(Console.ReadLine()); Console.Write("c = "); c = int.Parse(Console.ReadLine()); if (a < b) { if (a < c) min = a; else min = c; } else { if (b < c) min = b; else min = c; } Console.WriteLine("The smallest one is {0}.", min); 11

Checking Conditions if (var1 == 1) { // Do something. } else { if (var1 == 2) { // Do something else. } else { if (var1 == 3 || var1 == 4) { // Do something else. } else { // Do something else. } if (var1 == 1) { // Do something. } else if (var1 == 2) { // Do something else. } else if (var1 == 3 || var1 == 4) { // Do something else. } else { // Do something else. } 12

Common Mistakes if (var1 = 1) {…} if (var1 == 1 || 2) {…} 13

The ‘switch’ Statement switch ( ) { case : == > break; case : == > break;... case : == > break; default: != comparisonVals> break; } 14

Example 1 switch (var1) { case 1: // Do something. break; case 2: // Do something else. break; case 3: case 4: // Do something else. break; default: // Do something else. break; } 15

Example 2 switch (option) { case 1: Console.WriteLine(“You select 1”); break; case 2: Console.WriteLine(“You select 2”); break; case 3: Console.WriteLine(“You select 3”); break; default: Console.WriteLine(“Please select an integer between 1 and 3.”); break; } 16

switch Example switch (strProfession) { case "teacher": MessageBox.Show("You educate our young"); break; case "programmer": MessageBox.Show("You are most likely a geek"); break; case "accountant": MessageBox.Show("You are a bean counter"); break; default: MessageBox.Show("Profession not found in switch statement"); break; } 17 In C, only integer values can be used as the expression but in C#, strings can be used too. Don’t forget to use breaks!

Example 3 switch (strAnimal) { case “bird”: Console.WriteLine(“It has 2 legs.”); break; case “horse”: case “dog”: case “cat”: Console.WriteLine(“It has 4 legs.”); break; case “centipede”: Console.WriteLine(“It has 40 legs.”); break; case “snake”: Console.WriteLine(“It has no legs.”); break; default: Console.WriteLine(“I don’t know that animal!”); break; } 18

The Ternary Operator ? : Tertiary operator because it acts on 3 operands (remember unary and binary operators acting on 1 and 2 operands resp.) Example: – if (a < b) min = a; else min = b; – min = (a < b) ? a : b; 19

Looping 20

for Loop for (initializers; check_condition; modifying_expressions) { } Example: for (i = 0; i < 10; i++) { Console.WriteLine("i = " + i.ToString()); } 21

while Loop while (expression) { } 22

do-while Loop do { } while (expression); 23

foreach Loop foreach ( in ) { } 24

Displaying Months using for Loop string[] months = new string[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; for (int i = 0; i < months.Length; i++) { MessageBox.Show(months[i]); } 25

Displaying Months using foreach string[] months = new string[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; foreach (string month in months) { MessageBox.Show(month); } 26

Exercise Display first ten prime numbers. 27

Interrupting Loops break – ends the loop immediately continue – ends the current loop cycle return – jumps out of the function goto – jumps to the specified location (don’t use) 28

Infinite Loops while (true) { … } 29

Example int num; while (true) { Console.Write(“Enter a number between 1 and 100: ”); num = Convert.ToInt32(Console.ReadLine()); if (num >= 1 && num <= 100) break; else { Console.WriteLine(“It should be between 1 and 100.”); Console.WriteLine(“Please try again!\n”); } 30

Bitwise Operators & (Bitwise AND) | (Bitwise OR) ~ (Bitwise NOT) ^ (Bitwise XOR) 31

Examples 0110 & 0101 = 0100 (1&1=1, otherwise 0) 0110 | 0101 = 0111 (0|0=0, otherwise 1) 0110 ^ 0101 = 0011 (same  0, different  1) ~0110 = 1001 (0  1, 1  0) 32

Examples option = Location.Left | Location.Bottom; if (option & Location.Left != 0) MessageBox.Show(“Indented to left.”); if (option & Location.Bottom != 0) MessageBox.Show(“Indented to right.”); 33

Shift Operators >> (Shift right) << (Shift left) >>= <<= 34

Examples int a = 16; int b = a >> 2; // b becomes 4 int c = a << 4; // c becomes 256 a >>= 2; // a becomes 4 a <<= 4; // a becomes 64 35