Score score < score < score < 65 Score < 50

Similar documents
3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

C++ PROGRAMMING SKILLS Part 2 Programming Structures

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

REPETITION CONTROL STRUCTURE LOGO

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Unit 7. 'while' Loops

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

Add Subtract Multiply Divide

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Logical Operators and if/else statement. If Statement. If/Else (4.3)

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

Other Loop Options EXAMPLE

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

CS150 Introduction to Computer Science 1. Logical Operators and if/else statement

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

Increment and the While. Class 15

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

For loops, nested loops and scopes. Jordi Cortadella Department of Computer Science

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 4: Repetition Control Structure

More Complex Versions of the if Statement. Class 13

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Repetition Algorithms

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

Computer Programming

Lecture 7 Tao Wang 1

Outline. For loops, nested loops and scopes. Calculate x y. For loops. Scopes. Nested loops. Algorithm: repeated multiplication x x x x

Concepts Review. 2. A program is the implementation of an algorithm in a particular computer language, like C and C++.

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Advanced Computer Programming

Loops (while and for)

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Introduction. C provides two styles of flow control:

Name Section: M/W T/TH Number Definition Matching (8 Points)

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

In this chapter you will learn:

Looping. Arizona State University 1

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

CS 105 Lecture 5 Logical Operators; Switch Statement. Wed, Feb 16, 2011, 5:11 pm

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

Loops and Files. of do-while loop

5. Control Statements

Quiz Determine the output of the following program:

Loops / Repetition Statements

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

Information Science 1

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

Flow Control in C++ Condi&onals & Loops CS 16: Solving Problems with Computers I Lecture #4

LAB 4.1 Relational Operators and the if Statement

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

Selection / making decision If statement if-else, if-else-if or nested if Switch Case

Control Structures II. Repetition (Loops)

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

CS110D: PROGRAMMING LANGUAGE I

Lecture 10. Daily Puzzle

Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

Flow of Control. Branching Loops exit(n) method Boolean data type and expressions

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

Name Section: M/W T/TH Number Definition Matching (6 Points)

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

CHAPTER 4 CONTROL STRUCTURES

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Chapter 4: Making Decisions

STUDENT LESSON A12 Iterations

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

Chapter 5 : Repetition (pp )

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1

Outline. Review Choice Statements. Review Sequential Flow. Review Choice Before Loops. Review Choice After Loops

Introduction to Programming

Chapter 4: Making Decisions

Information Science 1

C++ Programming: From Problem Analysis to Program Design, Third Edition

Chapter 4: Control structures. Repetition

Transcription:

What if we need to write a code segment to assign letter grades based on exam scores according to the following rules. Write this using if-only. How to use if-else correctly in this example? score Score 85 75 score < 85 65 score < 75 50 score < 65 Score < 50 grade A B C D F

Else-If structure: To be used when you have more than one condition The general form is: If cond.1 statement 1; else if cond.2 statement 2; else if cond. 3 statement 3; else statement n; The first condition is tested. If it is True the statement following it is executed and the rest of the nested if is ignored. If the first condition is False, the second condition is tested and so on. If all conditions above else are false the statement following to the else is executed.

cin>>score; If(score >= 85) Solution: Write this code segment using the lower limit. Rewrite using only if. grade= A ; Take care: else if(score >= 75) if (score>=85) grade= B ; else if(score >= 65) grade= A ; grade = C ; if( score>=75) else if(score >= 50) grade = D ; grade= B ; else grade = F ; cout<< for score= <<score<< grade= <<grade;

Loops Repetitive Structures

Selections vs. Loops

Loops Many programs require to repeat one statement or group of statements as long as a condition is satisfied. This structure is called loops. C++ includes several ways to create loops We start with the while-loop

The While Loop This structure allows a certain statement or a set of statements to be repeated, as long as a certain Boolean expression remains true. Is there a connection between the boolean expression and the loop body? The general form is: while(boolean expression) statement 1; statement 2;. statement n; Loop Body Next sequential statement Boolean expression T Loop Body F Next sequential statement

While Loop Operation First, the boolean expression is evaluated If false, the program skips to the line following the while loop If true, the body of the loop is executed During execution, some item from the boolean expression is changed After executing the loop body, the boolean expression is checked again repeating the process until the expression becomes false Semi-colons are used only to end the statements within the loop A while loop might not execute at all if the boolean expression is false on the first check

Example: write a code segment to print the numbers 1, 2, 3,,10 each in a separate line. k=1; while(k<=10) cout<<k<<endl; k=k+1; Iteration # k output 1 1 1 1 2 2 2.. 10 10 10 Note that k=11 when you exit the loop K is called the loop control variable

Example Examples of several runs of the program What is the value of count_down when the program terminates?

Sample Program Write a code segment that tells you how long does it take (in months) to have your balance exceed $100 starting with $50 if the interest is 2% per month. After 1 month: $50 + 2% of $50 = $51 After 2 months: $51 + 2% of $51 = $52.02 After 3 months: $52.02 + 2% of $52.02 float balance=50; int count=0; while(balance<=100) balance+=balance*0.02; count++; cout<< Number of months= <<count;

Infinite Loops Loops that never stop are infinite loops The loop body should contain a line that will eventually cause the boolean expression to become false Example: Print the odd numbers less than 12 x = 1; while (x!= 12) cout << x << endl; x = x + 2; Better to use this comparison: while ( x < 12)

Exercise Can you Show the output of this code if x is of type int? x = 10; while ( x > 0) cout << x << endl; x = x 3; Show the output of the previous code using the comparison x < 0 instead of x > 0?

while(k<=10) cout<<k<<endl; k=k+1; k=1; while(k<=10) cout<<k<<endl; k=k-1; Common Mistakes k=1; while(k<=10); cout<<k<<endl; k=k+1; k=1; while(k>=10) cout<<k<<endl; k=k+1; k=1; while(k<=10) cout<<k<<endl; k=k-1;

Program Style A program written with attention to style is easier to read easier to correct easier to change

Example Indent groups of statements using braces Note that to print a new line, you can use \n instead of endl.

Example: write a complete C++ program to find out and print the maximum and the minimum numbers in a set of n float numbers. Start Read N, X max = X,min = X I = 2 Trace if X=5, 2,4,1 I N F write max, min stop T read x x>max T max = X F x<min T min = X F I = I + 1

#include <iostream.h> int main() int n, i ; float x, min, max ; cin>> n >> x; max = x ; min = x ; i= 2; while(i<= n) cin>> x ; if(x > max) max =x; else if(x < min) min = x ; i = i + 1 ; cout<< max= <<max<< min= <<min; return 0 ;

Exercises Write a code segment using a while loop that 1) Outputs the numbers -9, -6, -3, 0, 3, 6, 9, 12. 2) Outputs the integers from 30 to 5. 3) Computes and prints the sum of squared integers till n, e.g. if n =3. Sum= 1 2 +2 2 +3 2 =13. 5) Computes and prints the sum of all negative numbers only in a set of N +ve and ve float numbers read one by one. Also determine the average of ve numbers entered in the set of N numbers.

Exercise: Draw a flowchart and write a complete C++ program to compute and print average of n float numbers read one by one.

Exercises: (Event-controlled loops) Assume that x, y, k are int. Consider the code segment and answer the following: 1) Write the output (trace table) if the input is: 9 7 5 3 4 8 12. 2) If changed to (ii), what is the output for the same input. y=0; k=7; while(k>=1) cin>>x; if(k%2==1) y=y+(x+k+1); cout<<x<< <<y<<endl ; k--; cout<<endl; cout<< k= <<endl; y=0; k=7; while(k>=1) cin>>x; if(k%2==1) y=y+(x+k+1); cout<< x= <<x<< y= <<y; k--;

The For Loop This structure allows a certain statement or a set of statements to be repeated, a fixed number of times. The general form for an ascending for loop is: for(counter = value1 ; counter <= value2 ; counter++) counter = value1 statement 1; statement 2;. statement n; Next sequential statement Loop Body counter <= value2 T Loop Body F counter = counter + 1 Next sequential statement

1- if value1 < value2 the loop will be executed number of times equal to : (value2 value1) +1 2- if value1 = value2 the loop will be executed once. 3- if value1 > value2 the loop will not be executed at all.

Nested loops Nested loops consist of an outer loop with one or more inner loops. The inner and outer loops need not to be of the same type. Example Consider the following code segment

Ex 6.35 pg 242 Consider the following code segment: for(i=1;i<=3; i++) for(j=1;j<=3; j++) cout<<endl; cout<<i*j<< ; 1. What is the output? 2. Convert to while, what do you notice? 3. Rewrite to obtain the following O/P 1 2 3 2 4 6 3 6 9 3 6 9 1 3 6 9 2 4 6 2 4 2 4 1 2 3 3 6 9 1

Example : A class consists of m students. Given for each student n scores in n exams. Write a code segment to calculate the average for each student. Modify to find the average of all grades.(what if we remove braces after inner loop). int i, j ; float x,sum ; cin>>m>>n; for( i =1; i<=m ; i++) sum = 0; for( j = 1 ; j <= n ; j++) cin>>x; sum = sum + x ; cout<< for student number "<<i<< avg= "<<sum/n<<endl;