Control Statements Paritosh Srivastava.

Slides:



Advertisements
Similar presentations
More on Algorithms and Problem Solving
Advertisements

 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
Chapter 3 - Structured Program Development
4 Control Statements.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Structured Program Development in C
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Examples from “c++ how to program” book. SELECTIONS WITH IF-ELSE Example: if ( grade >= 60) cout = 60) cout
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1: Selection statements: if, if…else, switch.
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
C Lecture Notes 1 Structured Program Development.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
1 Lecture 3 Control Structures else/if and while.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
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.
Introduction to Computer Programming
Chapter 4 – C Program Control
The if…else Selection Statement
Control Structures Sequential execution Transfer of control
REPETITION CONTROL STRUCTURE
- Standard C Statements
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Control Statements: Part 2
Chapter 2.1 Control Structures (Selection)
TMF1414 Introduction to Programming
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals
Chapter 2.2 Control Structures (Iteration)
Control Statements Kingdom of Saudi Arabia
JavaScript: Control Statements.
JavaScript: Control Statements I
- Additional C Statements
Additional Control Structures
Structured Program
Chapter 4 - Program Control
Chapter 3 - Structured Program Development
3 Control Statements:.
Chapter 2.2 Control Structures (Iteration)
Chapter 6 Control Statements: Part 2
Chapter 3 - Structured Program Development
2.6 The if/else Selection Structure
Dale Roberts, Lecturer IUPUI
Chapter 4 - Program Control
Branching statements Kingdom of Saudi Arabia
Dale Roberts, Lecturer IUPUI
Dale Roberts, Lecturer IUPUI
Structural Program Development: If, If-Else
Control Statements:.
Presentation transcript:

Control Statements Paritosh Srivastava

Pre-incrementing and post- incrementing When variable is not in an expression then Pre-incrementing and post-incrementing have the same effect Example int c = 5; ++c; cout << c; // c = 6 and c++; cout << c; // c= 6 are the same

Increment and Decrement Operators (Cont.) int c = 5; cout << ++c; c is changed to 6 Then prints out 6 cout << c++; Prints out 5 (cout is executed before the increment) c then becomes 6

control structures Three control structures Sequence structure Programs executed sequentially by default Selection structures if, if…else, switch Repetition structures while, do…while, for

if Selection Statement Selection statements Pseudocode example If student’s grade is greater than or equal to 60 Print “Passed” If the condition is true The print statement executes then the program continues to next statement If the condition is false The print statement ignored then the program continues

if Selection Statement Selection statements (Cont.) Translation into C++ if ( grade >= 60 ) cout << "Passed"; Any expression can be used as the condition

if Selection Statement Logical AND (&&) Operator Consider the following if statement if ( gender == 1 && age >= 65 ) Females++; Combined condition is true If and only if both simple conditions are true Combined condition is false If either or both of the simple conditions are false

if Selection Statement Logical OR (||) Operator Consider the following if statement if ( ( semesterAverage >= 90 ) || ( finalExam >= 90 ) cout << “Student grade is A” << endl; Combined condition is true If either or both of the simple conditions are true Combined condition is false If both of the simple conditions are false

if Selection Statement Example if ( payCode == 4 ) cout << "You get a bonus!" << endl; If paycode is 4, bonus is given if ( payCode = 4 ) cout << "You get a bonus!" << endl; paycode is set to 4 (no matter what it was before) Condition is true (since 4 is non-zero) Bonus given in every case

if…else Double-Selection Statement Performs action if condition true if…else Performs one action if condition is true, a different action if it is false Pseudocode If student’s grade is greater than or equal to 60 print “Passed” Else print “Failed” C++ code if ( grade >= 60 ) cout << "Passed"; else cout << "Failed";

if…else Double-Selection Statement (Cont.) Ternary conditional operator (?:) Three arguments (condition, value if true, value if false) Code could be written: cout <<( grade >= 60 ? “Passed” : “Failed” ); Condition Value if true Value if false

if…else Double-Selection Statement (Cont.) Nested if…else statements One inside another Once a condition met, other statements are skipped Example If student’s grade is greater than or equal to 90 Print “A” Else If student’s grade is greater than or equal to 80 Print “B” Else If student’s grade is greater than or equal to 70 Print “C” Else If student’s grade is greater than or equal to 60 Print “D” Else Print “F”

if…else statements (Cont.) Nested if…else statements (Cont.) Written In C++ if ( studentGrade >= 90 ) cout << "A"; else if (studentGrade >= 80 ) cout << "B"; else if (studentGrade >= 70 ) cout << "C"; else if ( studentGrade >= 60 ) cout << "D"; else cout << "F";

if…else statements (Cont.) Nested if…else statements (Cont.) Written In C++ (indented differently) if ( studentGrade >= 90 ) cout << "A"; else if (studentGrade >= 80 ) cout << "B"; else if (studentGrade >= 70 ) cout << "C"; else if ( studentGrade >= 60 ) cout << "D"; else cout << "F";

if…else statements (Cont.) else problem Compiler associates else with the immediately preceding if Example if ( x > 5 ) if ( y > 5 ) cout << "x and y are > 5"; else cout << "x is <= 5"; Compiler interprets as if ( x > 5 ) if ( y > 5 ) cout << "x and y are > 5"; else cout << "x is <= 5";

if…else statements (Cont.) else problem (Cont.) Rewrite with braces ({}) if ( x > 5 ) { if ( y > 5 ) cout << "x and y are > 5"; } else cout << "x is <= 5"; Braces indicate that the second if statement is in the body of the first and the else is associated with the first if statement

if…else statements (Cont.) Compound statement Also called a block Set of statements within a pair of braces Used to include multiple statements in an if body Example if ( studentGrade >= 60 ) cout << "Passed.\n"; else { cout << "Failed.\n"; cout << "You must take this course again.\n"; } Without braces, cout << "You must take this course again.\n"; always executes

if Statement Example: if (u > v) { if (expression) a = 1; b = 2; if ( u > z) x =11; y = 12; } if (expression) { statements; { statements; } }

while Repetition Statement Action repeated while some condition remains true Pseudocode While there are more items on my shopping list Purchase next item and cross it off my list while loop repeats until condition becomes false Example int product = 3; while ( product <= 100 ) product = 3 * product;

Example: Finds the total of 4 numbers using while loop int count =1; int total = 0; while (count <=4) { cout << “\nEnter a number: “; cin >> num; total = total + num; cout “The total is now “ << total << endl; count++; }

for Repetition Statement Specifies counter-controlled repetition details in a single line of code

for Repetition Statement Not Valid: X for (j = 0, j < n, j = j + 3) // semicolons needed for (j = 0; j < n) // three parts needed

for Repetition Statement Example 1: j = 1; sum = 0; for ( ; j <= 3; j = j + 1){ sum = sum + j; cout<<"sum = "<<sum<<"\n"; } Output : 1, 3, 6 Example 2: j = 1; sum = 0; for ( ; j <= 3; ){ sum = sum + j;} Output : infinite loop Example 3: j = 1; sum = 0; for ( ; ; ) { sum = sum + j; j++; cout << "\n" << sum; } Output : infinite loop

for Repetition Statement (Cont.) General form of the for statement for ( initialization; loopContinuationCondition; increment ) statement; Can usually be rewritten as: initialization; while ( loopContinuationCondition ) { statement; increment; } If the control variable is declared in the initialization expression It will be unknown outside the for statement

for Repetition Statement (Cont.) for statement examples Vary control variable from 1 to 100 in increments of 1 for ( int i = 1; i <= 100; i++ ) Vary control variable from 100 to 1 in increments of -1 for ( int i = 100; i >= 1; i-- ) Vary control variable from 7 to 77 in steps of 7 for ( int i = 7; i <= 77; i += 7 ) Vary control variable from 20 to 2 in steps of -2 for ( int i = 20; i >= 2; i -= 2 ) Vary control variable over the sequence: 2, 5, 8, 11, 14, 17, 20 for ( int i = 2; i <= 20; i += 3 ) Vary control variable over the sequence: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0 for ( int i = 99; i >= 0; i -= 11 )

for Repetition Statement (Cont.) Using a comma-separated list of expressions for ( int number = 2;number <= 20;total += number, number += 2 ) ; This is equal to : for ( int number = 2;number <= 20;number += 2 ){ total += number }

do…while Repetition Statement do…while statement Similar to while statement Tests loop-continuation after performing body of loop Loop body always executes at least once C++ Code: do { cout<<x<<“ “; x++; } while (x<10) ;

The switch Statement Similar to if statements Can list any number of branches Used in place of nested if statements Avoids confusion of deeply nested ifs

The switch Statement switch (expression) { case value1: statement1; break; case value2: statement2; case valuen: statementn; default: statement; }

The switch Statement switch (grade) { case ‘A’: cout << “Grade is between 90 & 100”; break; case ‘B’: cout << “Grade is between 80 & 89”; case ‘C’: cout << “Grade is between 70 & 79”; break; case ‘D’: cout << “Grade is between 60 & 69”; case ‘E’: cout << “Grade is between 0 & 59”; default: cout << “You entered an invalid grade.”; }

The switch Statement * * * * Menu * * * * 1. Nablus 2. Rammallah 3. Tolkarm 4. Jenien Choose either 1, 2, 3 or 4:

The switch Statement switch (choice) switch (choice) { { case 1: cout << “Nablus”; break; case 2: cout <“Rammallah”; case 3: cout << “Tolkarm”; case 4: cout << “Jenien”; default: cout<<“ invalid choice”; } switch (choice) { case 1: cout << “Nablus”; case 2: cout << “Rammallah”; case 3: cout << “Tolkarm”; case 4: cout << “Jenien”; default: cout<<“ invalid choice”; }

The switch Statement #include<iostream> Void main ( ) { int value cout << “Enter 1- Palestine 2- Egypt 3- USA”; cin >> value; switch (value) case 1: cout << “No of population is 5 million”; break; case 2: cout << “No. of population is 70 million”; break; case 3: cout << “No. of population is 180 million”; break; default: cout<<“invalid choice”; }

switch Multiple-Selection Statement switch statement Used for multiple selections Tests a variable or expression Compared against constant integral expressions to decide on action to take

The switch Statement switch statement Controlling expression Expression in parentheses after keyword switch case labels Compared with the controlling expression Statements following the matching case label are executed Braces are not necessary around multiple statements in a case label A break statements causes execution to proceed with the first statement after the switch Without a break statement, execution will fall through to the next case label

The switch Statement switch statement (Cont.) default case Executes if no matching case label is found Is optional If no match and no default case Control simply continues after the switch

break and continue Statements break statement Causes immediate exit from the loop Used in while, for, do…while or switch statements continue statement Skips remaining statements in loop body and start from the beginning of loop Used in for loop, while , do…while loops

for void main ( ) { int k; for ( k= -5; k < 25; k= k+5) cout << k; cout << “ Good Morning” << endl; } Output = - 5 Good Morning 0 Good Morning 5 Good Morning 10 Good Morning 15 Good Morning 20 Good Morning

break void main ( ) { int k; for ( k= -5; k < 25; k= k+5) cout << k; break; cout << “ Good Morning” << endl; } Output = -5

continue Void main ( ) { int k; for ( k= -5; k < 25; k= k+5) cout << k; conitnue; cout << “ Good Morning” << endl; } Output = - 5 5 10 15 20

Examples int j =50; while (j < 80) { j += 10; if (j == 70) break; cout << “j = “ << j<< ‘\n’; } cout << “We are out of the loop.\n”; Output j = 60 We are out of the loop.

Example else cout << "DO SOMETHING\n"; } while (age <=0); do { cout << “Enter your age: “; cin >> age; if (age <=0) cout << “Invalid age.\n”; else cout << "DO SOMETHING\n"; } while (age <=0);

Example do { x = x + 5; y = x * 25; cout << y << endl; if ( x == 100) done = true; } while (!done);

Hw # 3 Write a program that computes the value of ex by using the formula:? ex = 1 + x/1! + x2/2 + x3/3! + x4/4!+…..

Hw # 3 Write a program that reads three nonzero integers and determines and prints if they could be the sides of a right triangle ?