Flow of Control I

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

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

View Full Document Right Arrow Icon
CS 115 Chapter 2 Flow of Control (Selection)
Background image of page 1

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

View Full DocumentRight Arrow Icon
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.
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
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; }
Background image of page 7

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

View Full DocumentRight Arrow Icon
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; }
Background image of page 8
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.
Background image of page 9

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

View Full DocumentRight Arrow Icon
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
Background image of page 10
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
Background image of page 11

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

View Full DocumentRight Arrow Icon
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
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 45

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

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online