Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout << “ Excellent ” ; if ( grade ==‘B’ ) cout << “ Very Good ” ; if ( grade ==‘C’ ) cout <<

Slides:



Advertisements
Similar presentations
Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':
Advertisements

Introduction to C Programming
Algorithm & Flow Charts Decision Making and Looping Presented By Manesh T Course:1090 CS.
UNIT 3 PROBLEM SOLVING WITH LOOP AND CASE LOGIC STRUCTURE
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
1 CIS Jan Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
CONTROLLING PROGRAM FLOW
Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Visual Basic Programming
Chapter 05 (Part III) Control Statements: Part II.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Iterations Very Useful: Ability to repeat a block of code Example:
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.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Control Structures RepetitionorIterationorLooping Part I.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
1 CS161 Introduction to Computer Science Topic #8.
Fundamental Programming Fundamental Programming More on Repetition.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
Algorithm & Flow Charts Decision Making and Looping
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Computer Programming -1-
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
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.
REPETITION CONTROL STRUCTURE
while Repetition Structure
Chapter 2.2 Control Structures (Iteration)
For & do/while Loops.
TOPIC 4: REPETITION CONTROL STRUCTURE
Introduction to Programming
Introduction to Programming
Conditional Construct
Structured Program
Chapter 3 - Structured Program Development
3 Control Statements:.
Chapter 3 – Control Structures
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Chapter 3 - Structured Program Development
Programming Fundamental
Programming Fundamental
Presentation transcript:

Lecture 4 Introduction to Programming

if ( grade ==‘A’ ) cout << “ Excellent ” ; if ( grade ==‘B’ ) cout << “ Very Good ” ; if ( grade ==‘C’ ) cout << “ Good ” ; if ( grade ==‘D’ ) cout << “ Poor ” ; if ( grade ==‘F’ ) cout << “ Fail ” ; if Statements

if ( grade ==‘A’ ) cout << “ Excellent ” ; else if ( grade ==‘B’ ) cout << “ Very Good ” ; else if ( grade ==‘C’ ) cout << “ Good ” ; else if ( grade ==‘D’ ) cout << “ Poor ” ; if else

if ( grade == ‘A’ ) cout << “ Excellent ” ; else if ( grade == ‘B’ ) … else if … … else … if else

while loop while (condition) { statements; : }statements;

While loop executes zero or more times. What if we want the loop to execute at least one time?

do-while

Do while loop execute one or more times

Syntax of do-while loop do{ statements ; } while ( condition ) ;

Example-Guessing game char c ; char c ; int tryNum = 1 ; int tryNum = 1 ; do do { cout << "Please enter your guess by pressing a character key from a to z “ ; cout << "Please enter your guess by pressing a character key from a to z “ ; cin >> c ; cin >> c ; if ( c == 'z‘ ) if ( c == 'z‘ ) { cout << "Congratulations! you guessed the right answer“ ; cout << "Congratulations! you guessed the right answer“ ; tryNum = 6 ; tryNum = 6 ; } else else tryNum = tryNum + 1 ; tryNum = tryNum + 1 ; } while ( tryNum <= 5 ) ; } while ( tryNum <= 5 ) ;

Flow chart for do-while loop Do-while condition Process Exit true false

Relational Operators char c ; char c ; int tryNum, maxTries ; int tryNum, maxTries ; tryNum = 1 ; tryNum = 1 ; maxTries = 5 ; maxTries = 5 ; cout << "Guess the alphabet between a to z “ ; cout << "Guess the alphabet between a to z “ ; cin >> c ; cin >> c ; while ( ( tryNum <= maxTries ) && ( c! = ‘z‘ ) ) while ( ( tryNum <= maxTries ) && ( c! = ‘z‘ ) ) { cout << "Guess the alphabet between a to z “ ; cout << "Guess the alphabet between a to z “ ; cin >> c ; cin >> c ; tryNum = tryNum + 1 ; tryNum = tryNum + 1 ; }

for Loop

For loop for ( initialization condition ; termination condition ; increment condition ) { statement ( s ) ; }

Example int counter ; for( counter = 0 ; counter < 10 ; counter = counter + 1 ) cout << counter; cout << counter; Output

Table for 2 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 :: 2 x 10 = 20

Example - Calculate Table for 2 #include #include main ( ) { int counter ; for ( counter = 1 ; counter <= 10 ; counter = counter + 1 ) for ( counter = 1 ; counter <= 10 ; counter = counter + 1 ){ cout << "2 x " << counter << " = " << 2* counter << "\n“ ; cout << "2 x " << counter << " = " << 2* counter << "\n“ ; }}

Output 2 x1 = 2 2 x 2 = 4 2 x 3 = 6 :: 2 x 10 = 20

Flow chart for the ‘Table’ example counter=1 While Print 2*counter counter <=10? Stop Start No Exit Counter = counter + 1 yes

Example: Calculate Table- Enhanced #include #include main ( ) { int number ; int number ; int maxMultiplier ; int maxMultiplier ; int counter ; int counter ; maxMultiplier = 10 ; maxMultiplier = 10 ; cout << " Please enter the number for which you wish to construct the table “ ; cout << " Please enter the number for which you wish to construct the table “ ; cin >> number ; cin >> number ; for ( counter = 1 ; counter <= maxMultiplier ; counter = counter + 1 ) for ( counter = 1 ; counter <= maxMultiplier ; counter = counter + 1 ) { cout << number <<" x " << counter<< " = " << number * counter << "\n“ ; cout << number <<" x " << counter<< " = " << number * counter << "\n“ ; }}

Increment operator ++ counter ++ ; counter ++ ; same as counter = counter + 1; counter = counter + 1;

Decrement operator -- counter -- ; counter -- ; same as counter = counter - 1 counter = counter - 1

+= counter += 3 ; counter += 3 ; same as counter = counter + 3 ; counter = counter + 3 ;

-= counter -= 5 ; counter -= 5 ; same as counter = counter – 5 ; counter = counter – 5 ;

*=x*=2 x = x * 2

/= x /= 2 x = x / 2

Compound Assignment Operators operator=

%= x %= 2 ; x %= 2 ; same as x = x % 2 ; x = x % 2 ;

Comments Write comment at the top program to show what it does Write comment at the top program to show what it does Write comments that mean some thing Write comments that mean some thing

int sum; int students ; int average ; sum = 0 ; students = 0 ; do{ cin >> grade ; sum += grade ; students ++ ; } while (grade >= 0) ; average = sum / students ; cout << average ; Example: Program to calculate the average marks of class A Flaw in the code

switch statement

switch statements switch ( variable name ) { case ‘a’ : statements; case ‘b’ : statements; case ‘c’ : statements; … }

switch ( grade) { case ‘A’ : cout << “ Excellent ” ; case ‘B’ : cout << “ Very Good ” ; case ‘C’ : … … } switch statements

case ‘A’ : cout << “ Excellent ” ; … … switch statements

Example switch ( grade) { case ‘A’ : cout << “ Excellent ” ; case ‘B’ : cout << “ Very Good ” ; case ‘C’ : cout << “Good ” ; case ‘D’ : cout << “ Poor ” ; case ‘F’ : cout << “ Fail ” ; }

break;

Example switch ( grade ) { case ‘A’ : cout << “ Excellent ” ; break ; case ‘B’ : cout << “ Very Good ” ; break ; case ‘C’ : break ; case ‘C’ : cout << “Good ” ; break ; break ; case ‘D’ : cout << “ Poor ” ; break ; case ‘F’ : cout << “ Fail ” ; break ; case ‘F’ : cout << “ Fail ” ; break ; break ;}

default : cout << “ Please Enter Grade from ‘A’ to ‘D’ or ‘F’ “ ; default :

switch (grade) Display “Excellent” case ‘B’ : case ‘A’ : Display “Very Good” Default : “……..” Flow Chart of switch statement …

if ( amount > ) statements ;

Whole Number short short int int long long

case ‘A’ : case ‘ 300 ‘ : case ‘ f ‘ :

if (c == ‘z’ ) { cout << “ Great ! You have made the correct guess “ ; break ; } break ;

continue ;

continue while trynum <= 5 ; {….…. continue ; }

for ( counter = 0 ;counter <= 10 ; counter ++ ) {……. continue ; } continue in ‘for’ loop

Sequential Statements Sequential Statements Decisions Decisions – if, if else, switch Loops Loops – while, do while, for What have we done till now …

goto Unconditional Branch of Execution

Sequences Sequences Decisions Decisions Loops Loops Structured Programming

Minimize the use of break Minimize the use of break Minimize the use of continue Minimize the use of continue Never use goto Never use goto

Guide lines for structured programming Modular Modular Single entry - single exit Single entry - single exit

Rule 1 : Use the simplest flowchart Rule 2 : Any rectangle can be replaced by two rectangles. Rule 3 : Any rectangle can be replaced with structured flowcharting constructs. structured flowcharting constructs. Rule 4 : It says, rule 2 and rule 3 can be repeated as many times as needed repeated as many times as needed Rules for Structured Flowchart

Structures Structures Arrays Arrays Character Strings Character Strings Pointers Pointers Next Milestones