1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Logic & program control part 3: Compound selection structures.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
True or false A variable of type char can hold the value 301. ( F )
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Multi-alternative Selection Both the if clause and the else clause of an if...else statement can contain any kind of statement, including another selection.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Control Structures I (Selection)
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
Advanced Programming LOOP.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Semester Review. As we have discussed, Friday we will have in class time for you to work on a program, this program will come with instructions and you.
CONTROLLING PROGRAM FLOW
Chapter 7 Additional Control Structures. Chapter 7 Topics l Switch Statement for Multi-Way Branching l Do-While Statement for Looping l For Statement.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
1 Chap 4. Data Should know Declare & Initialize variables Declare constants Assignment Operators Increment and Decrement Operators Precedence of Operators.
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
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.
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.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Part 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures.
Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 03 – Operators, Algorithm Design, Programming Constructs Richard Salomon.
1 Conditions, Logical Expressions, and Selection Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Part 1 Outline Counter-Controlled Repetition: Example Sentinel-Controlled Repetition:
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Control Statements: Part1  if, if…else, switch 1.
1 Advanced Programming Windows Applications. Create a new Project Select a C# Windows Application.
C++ Programming Control Structures I (Selection).
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Branching statements.
Chapter 4: Control Structures I
The if…else Selection Statement
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Chapter 4: Control Structures I
Sachin Malhotra Saurabh Choudhary
C Short Overview Lembit Jürimägi.
Computing with C# and the .NET Framework
Chapter 4 – Control Structures Part 1
Chapter 3: Understanding C# Language Fundamentals
SELECTION STATEMENTS (1)
Chapter 4: Control Structures I
SELECTION STATEMENTS (2)
Structured Program
Java Tokens & Data types
3 Control Statements:.
Chapter 4 Selection.
Control Statements.
Presentation transcript:

1 Advanced Programming IF

2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different statement other than the next one executes Selection structure –The if and if/else statements –The goto statement No longer used unless absolutely needed Causes many readability problems Repetition structure –The while and do/while loops –The for and foreach loops

Control Statements If, Else While For Foreach

4 using System; class Comparison { static void Main( string[] args ) { int number1, number2; number1 = Int32.Parse( Console.ReadLine() ); number2 = Int32.Parse( Console.ReadLine() ); if ( number1 == number2 ) Console.WriteLine( number1 + " == " + number2 ); if ( number1 > number2 ) Console.WriteLine( number1 + " > " + number2 );

Operators Precedence CategoryOperators Additive Add: + Subtract: - Shift Shift bits left: << Shift bits right: >> Relational Less than: < Greater than: > Less than or equal to: <= Greater than or equal to: >= Type equality/compatibility: is Type conversion: as

6 Decision Making: Equality and Relational Operators

Operators Precedence CategoryOperators Equality Equals: == Not equals: != Bitwise AND& Bitwise XOR^ Bitwise OR| Logical AND&& Logical OR||

8 Example Your city classifies a pollution index –less than 35 as “Pleasant”, –35 through 60 as “Unpleasant”, – and above 60 as “Health Hazard.” –Display the correct description of the –pollution index value.

9 Assignment Operators Assignment operators –Can reduce code x += 2 is the same as x = x + 2 –Can be done with all the math operators ++, -=, *=, /=, and %=

10 Assignment Operators

11 Increment and Decrement Operators Increment operator –Used to add one to the variable –x++ –Same as x = x + 1 Decrement operator –Used to subtract 1 from the variable –y-- Pre-increment vs. post-increment –x++ or x-- Will perform an action and then add to or subtract one from the value –++x or --x Will add to or subtract one from the value and then perform an action

12 Conditional Operator (?:) –Similar to an if/else structure –The syntax is: (boolean value ? if true : if false) Console.WriteLine ( studentGrade >=60? “Passed” : “Failed”);

13 Example Display one word to describe the integer value of number as “Positive”, “Negative”, or “Zero”

switch Statement Can branch on any predefined type (including string ) or enum –User-defined types can provide implicit conversion to these types Must explicitly state how to end case –With break, goto case, goto label, return, throw or continue –Eliminates fall-through bugs –Not needed if no code supplied after the label

Statements switch Statement int Test(string label) { int result; switch(label) { case null: goto case “runner-up”; case “fastest”: case “winner”: result = 1; break; case “runner-up”: result = 2; break; default: result = 0; } return result; }

16 Example Write a program to ask a student for his grades in 3 exams ( each out of 50 ), get their total and inform the student whether he passed or failed the course.

17 Example Compute the Grade of students as A, B, C, D, and F in a class depending on their scores

18 Write a program that calculates bills for customer of the Electricity company. There are 3 types of customers: residential (code R), commercial (code C), and Industrial (code I). - For a code R customer, the bill is $10 plus $0.05 for each kilowatt used. - For a code C customer, the bill is $1000 for the first 2000 kilowatt, and $0.005 for each additional kilowatt used. - For a code I customer, the bill is $1000 if he used less than 4000 kilowatt, $2000 if he used between 4000 and kilowatt, or $3000 if he used more than kilowatt. The inputs of the program should be the type of customer ( R, C or I) and the kilowatts used. The output should be the amount of money the customer has to pay.

Predefined Types char Escape sequence characters (partial list) CharMeaningValue \’ Single quote0x0027 \” Double quote0x0022 \\ Backslash0x005C \0 Null0x0000 \n New line0x000A \r Carriage return0x000D \t Tab0x0009

20 If--Else for a mail order Write a program to calculate the total price of a certain purchase. There is a discount and shipping cost:  The discount rate is 25% and the shipping is if purchase is over  Otherwise, The discount rate is 15% and the shipping is 5.00 pounds.