Advanced Programming LOOP.

Slides:



Advertisements
Similar presentations
Pemrograman VisualMinggu …2… Page 1 MINGGU Ke Dua Pemrograman Visual Pokok Bahasan: Console Application Tujuan Instruksional Khusus: Mahasiswa dapat menjelaskan.
Advertisements

3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
Section 2 - Selection and Repetition. Equality and Relational Operators.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
 2002 Prentice Hall. All rights reserved Control Structures 3 control structures –Sequential structure Built into Python –Selection structure The.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
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.
 2002 Prentice Hall. All rights reserved. 1 Outline 3.1Introduction 3.2Simple Program: Printing a Line of Text 3.3Another Simple Program: Adding Integers.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Repeating Instructions
Chapter 5: Control Structures II (Repetition)
 2002 Prentice Hall. All rights reserved. 1 Chapter 3 – Control Structures Outline 3.1 Introduction 3.2 Algorithms 3.3 Pseudocode 3.4Control Structures.
An Introduction to C# and the .NET Framework
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
The University of Texas – Pan American
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
 2001 Prentice Hall, Inc. All rights reserved. 1 Introduction to C# Programming Part I.
Chapter 3 – Introduction to Visual Basic Programming
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.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
 2009 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
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 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures.
Introduction to Visual Basic Programming Introduction to Visual Basic Programming Ch3 – Deitel’s Book.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
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.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled.
 2001 Prentice Hall, Inc. All rights reserved. 1 Introduction to C# Part II.
Advanced Programming LOOP. 2 while Repetition Structure Repetition Structure –An action is to be repeated Continues while statement is true Ends when.
1 Chapter 3 – JavaScript Outline Introduction Flowcharts Control Structures if Selection Structure if/else Selection Structure while Repetition Structure.
 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.
Java Review if Online Time For loop Quiz on Thursday.
 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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
COMP Loop Statements Yi Hong May 21, 2015.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 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.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Part 1 Outline Counter-Controlled Repetition: Example Sentinel-Controlled Repetition:
CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) Xiang Lian The University of Texas – Pan American Edinburg, TX
A DVANCED P ROGRAMMING C HAPTER 5 & 6: C ONTROL S TRUCTURES Dr Shahriar Bijani Spring 2016.
1 Advanced Programming Windows Applications. Create a new Project Select a C# Windows Application.
Solving Problems with Repetition Version 1.0. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C#
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Lecture 02 Dr. Eng. Ibrahim El-Nahry Methods. 2 Learning Objectives Class Definition includes both methods and data properties Method Definition and Declaration.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Loops, Part II IT108 George Mason University. Indefinite Loop Don’t always have access to the number of iterations ahead of time If a condition (user-response,
C++ Programming: CS102 LOOP. Not everything that can be counted counts, and not every thing that counts can be counted. −Albert Einstein Who can control.
Midterm preview.
CSIT 108 Review Visual Basic.NET Programming: From Problem Analysis to Program Design.
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.
Chapter 5- Control Structures: Part 2
C++ Programming: CS150 For.
Chapter 4 – Control Structures Part 1
Chapter 5 – Control Structures: Part 2
Advanced Programming Lecture 02: Introduction to C# Apps
Advanced Programming Chapters 5 & 6: Control Structures
Chapter 3 – Control Structures
Chapter 3 – Introduction to C# Programming
Chapter 5 – Control Structures: Part 2
Presentation transcript:

Advanced Programming LOOP

Control Structures The if and if/else statements The goto statement 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

while Repetition Structure An action is to be repeated Continues while statement is true Ends when statement is false Contain either a line or a body of code Must alter conditional Endless loop

Example write a program that reads in the grades of 50 students in a course (out of 100 points each ) and then count the number of A students ( grade > 85 ) and the number of B students (grade > 75 ). And print the average grad for all students.

// Class average with counter-controlled repetition // Class average with counter-controlled repetition. using System; class Average1 { static void Main( string[] args ) { int total, // sum of grades gradeCounter, // number of grades entered gradeValue, // grade value average; // average of all grades total = 0; // clear total gradeCounter = 1; // prepare to loop while ( gradeCounter <= 10 ) // loop 10 times {

Console. Write( "Enter integer grade: " ); gradeValue = Int32 Console.Write( "Enter integer grade: " ); gradeValue = Int32.Parse( Console.ReadLine() ); total = total + gradeValue; gradeCounter = gradeCounter + 1; } average = total / 10; // integer division Console.WriteLine( "\nClass average is {0}", average ); } // end Main } // end class Average1

Example Assume you put 1000 pounds in a projects that returns a profit of about 5% per year. How long will it take for your money to double ? Assume you put 5000 pounds in a projects that returns a profit of about 10% per year. How much money will you have in 5 years

for Repetition Structure ( int counter = 1; counter <= 5; counter++ ) Initial value of control variable Increment Control variable name Final value keyword Loop-continuation condition

// Counter-controlled repetition with the for structure. using System; class ForCounter { static void Main( string[] args ) { for ( int counter = 1; counter <= 5; counter++ ) Console.WriteLine( counter +” “); }

factorial(n) is 1 X 2 X 3 …… x n Example Write a program to ask the user for a positive integer, and then display its factorial. Given that factorial(n) is 1 X 2 X 3 …… x n 

using System; using System.Windows.Forms; class Interest { static void Main( string[] args ) double amount, principal = 1000.00, rate = .05; string output; output = "Year\tAmount on deposit\n"; for ( int year = 1; year <= 10; year++ ) amount = principal *Math.Pow( 1.0 + rate, year ); output += year + "\t" + String.Format( "{0:C}", amount ) + "\n"; } MessageBox.Show( output, "Compound Interest", MessageBoxButtons.OK, MessageBoxIcon.Information );}}

Example In one university, it has a rule that at least 75% of student in each course must pass. This means that the pass mark for each course will differ. Write a program that will read 100 students’ marks and determine the pass mark.

Welcome4.cs Message Box // Printing multiple lines in a dialog Box. using System; using System.Windows.Forms; class Welcome4 { static void Main( string[] args ) MessageBox.Show( "Welcome \n to \n C# \n programming!" );} }

Simple Program Graphical User Interface GUIs are used to make it easier to get data from the user as well as display data to the user Message boxes Within the System.Windows.Forms namespace Used to prompt or display information to the user

Simple Program Many compiled classes need to be referenced before they can be used in classes Assemblies are the packing unit for code in C# Namespaces group related classes together Add Reference dialogue

Simple Program References folder Solution Explorer System.Windows.Forms reference Adding a reference to an assembly in Visual Studio .NET (part 2).

Message boxes - Icons Formatting Buttons (variable : format) OK OKCancel YesNo AbortRetryIgnore YesNoCancel RetryCancel - Icons Exclamation Question Error Information Formatting (variable : format)

using System; using System.Windows.Forms; class Sum { static void Main( string[] args ) { int sum = 0; for ( int number = 2; number <= 100; number += 2 ) sum += number; MessageBox.Show( "The sum is " + sum, "Sum Even Integers from 2 to 100", MessageBoxButtons.OK, MessageBoxIcon.Information ); } }

Argument 4: MessageBox Icon (Optional) Argument 3: OK dialog button. (Optional) Argument 2: Title bar string (Optional) Argument 1: Message to display

Examples Using the for Structure

A number of students are answering a questionnaire form that has 25 questions. Answer to each question is given as an integer number ranging from 1 to 5. Write a program that first reads the number of students that have left the fulfilled questionnaire. After that the programs reads answers so that first answers of the first student are entered starting from question 1 and ending to question 25. Answers of second student are then read in similar way. When all answers are entered the program finds out what are questions (if any) to which all students have given the same answer. Finally the program displays those questions (the question numbers).

Pascal’s triangle looks like this: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 The number in each cell of each row is the sum of the numbers in the two cells above it. Write a method Pascal (int rows ) to print Pascal's triangle with number of rows is row.

Write a console application has input a non-negative integer and show a string of binary bits representing n.

) (1/2 mark per correct circle, -1/2 mark per incorrect circle) Circle the syntax or logic errors in the following C# class and briefly explain what is wrong beside your circle. class TempConv; { public void main( ); double BASE_TEMP = 30; int Deg_C float Deg_F; Console.WriteLine( 'Please enter a temperature in degrees C ' ); Deg_C = console.readline(); Deg = (BASE_TEMP + (9 / 5) * (int) Deg_C); Console.WriteLine('The Fahrenheit temperature is ' + Deg_F + '.'); }

Write a code in each button of the flowing windows application that calculates the electricity bill. the price of the electricity rate is 10 for each kilowatt for governmental use, 5 for home use and 20 for commercial use. The command “Clear” removes the old values from the texts in the program

Construct C# console application to solve the following problem Construct C# console application to solve the following problem. A football team plays n games per year in its league. Given n and the scores of all of the games the team played this year (both the team’s score and its opponent’s score for each game), compute the team’s margin of victory in the games that it played (win = 3, tied = 1 and ignore lost).