Flow of Control I

# Flow of Control I - CS 115 Chapter 2 Flow of...

This preview shows pages 1–13. Sign up to view the full content.

CS 115 Chapter 2 Flow of Control (Selection)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Flow of Control (Selection) 2 Control Structures z 3 ways to program execution: z Sequentially z Selectively by making a choice z Repetitively in a loop z Control Structures are used to alter the sequential flow of execution either by selection or repetition.
Flow of Control (Selection) 3 Control Structures z E.g. If (grade equal to ‘A’) gpa is 4 If (salary greater than \$70,000) accept job Otherwise ask for more benefits

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Flow of Control (Selection) 4 Control Structures I (Selection) z Relational Operators z Logical (Boolean) Operators and Logical Expressions z Selection: if and if …else z switch Structures
Flow of Control (Selection) 5 Relational Operators Operator Description == equal to != not equal to < less than <= less than or equal to > greater than >= greater than or equal to

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Flow of Control (Selection) 6 Relational Operators z Each of the relational operators is a binary operator z Also known as comparison operators z Result of comparison is true or false
Flow of Control (Selection) 7 Relational Operators Integer Example #include <iostream.h> int main() { int x, y; x = 20; y = 10; cout<<"x = "<<x<<" and y = "<<y<<endl; //comparisons cout<<"\nIs x > y ? "<<(x > y)<<endl; cout<<"Is y + 10 == x ? "<<(y + 10 == x)<<endl; cout<<"Is x <= y ? "<<(x <= y)<<endl; return 0; }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Flow of Control (Selection) 8 Relational Operators Character Example #include <iostream.h> int main() { cout<<"Comparisons on char type data\n"<<endl; //comparisons cout<<"Is 'A' < 'a' ? "<<('A' < 'a')<<endl; cout<<"'A' = "<<int('A')<<" and 'a' = "<<int('a')<<endl; cout<<"\nIs '+' < '*' ? "<<('+' < '*')<<endl; cout<<"'+' = "<<int('+')<<" and '*' = "<<int('*')<<endl; cout<<"\nIs '6' >= '\$' ? "<<('6' >= '\$')<<endl; cout<<"'6' = "<<int('6')<<" and '\$' = "<<int('a')<<endl; return 0; }
Flow of Control (Selection) 9 Relational Operators String Example z E.g. string str = “Hello”; z String type variables are compared character by character, starting from the first character and using the ASCII collating sequence.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Flow of Control (Selection) 10 Relational Operators String Example z E.g. string str1 = “Hello”; string str2 = “Hi”; Expression: str1 > str2 is false str1 == “hello” is false “Bill” < “Billy” is true
Flow of Control (Selection) 11 Control Structures I (Selection) z Relational Operators z Logical (Boolean) Operators and Logical Expressions z Selection: if and if …else z switch Structures

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
12 Logical (Boolean) Operators and Logical Expressions Operator Description # of operands !n o t1 and 2 || or 2 Logical operators take only logical values as operands and yield only logical values as results. The operator
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 04/14/2008 for the course CS 115 taught by Professor Ryon during the Fall '06 term at NJIT.

### Page1 / 45

Flow of Control I - CS 115 Chapter 2 Flow of...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online