Lecture 3 Control Structures Selection Statements 07032023 103638am

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

3/7/2023

LECTURE

3 Control Structures – (Selection Statements)


CSC 113
Computer Programming
Spring 2023

Abrar Ahmed
abrarahmed.buic@bahria.edu.pk

Department of Computer Science


Bahria University, Islamabad

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 1

Outline

• if statement
• Use of if else
• else-if Statement
• Switch statement

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 2

1
3/7/2023

Program Flow of Control


• Sequential execution
• By default, statements executed in sequential order

• Non Sequential Transfer of control


• Next statement executed not next one in sequence
• It can result from a forced event like (goto statement, system call, function call etc.)
• Or it can be due to a condition resulting in a ‘True’ or ‘False’ state.

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 3

Control Structures
• 3 control structures
(Bohm and Jacopini Theorem)
• Sequence structure
• Executing one subprogram, and then another subprogram (sequence)

• Selection structures (if, if/else, switch)


• Executing one of two subprograms according to the value of a boolean expression (selection)

• Repetition structures (while, do/while, for loops)


• Executing a subprogram as long as a boolean expression is true (iteration)

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 4

2
3/7/2023

Control Structures
• 3 control structures
• Sequence structure
• Programs executed sequentially by default
• Selection structures
• If (Single Selection Statement)
• if/else (Double Selection Statement)
• switch (Multiple Selection Statement)
• Repetition structures
• For
• While
• do/while

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 5

Control Structures

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 6

3
3/7/2023

if, if else and else if Statements

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 7

Selection Structures

• If • Use is based on situation


• If-else but not restricted
• Multiple if/else-if/Switch • For single condition
• Nested if check
• If
• If-else
• For multiple condition
check
• If with Boolean operators
• Else-if
• switch

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 8

4
3/7/2023

if Statement
• C++ uses keyword if to implement the decision control instruction.
if ( this condition is true )
execute this statement;

• The if keyword is followed by a test expression in parenthesis.

• If the condition is true than compiler will execute the immediate statement. But if the condition is false
than the immediate statement will not execute, and the compiler will skip the immediate statement and
execute the next statement.

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 9

The If Selection Structure


• Selection structure
• Choose among alternative courses of action
• Pseudocode example:
If student’s grade is greater than or equal to 60
Print “Passed”

◼ If the condition is true


Print statement executed, program continues to next statement
◼ If the condition is false
Print statement ignored, program continues

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 10

10

5
3/7/2023

The If Selection Structure


• Translation into C++
If student’s grade is greater than or equal to 60
Print “Passed”

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 11

11

Multiple Statements in the if body


if ( condition )
{
statement 1;
statement 2
.
.
statement n;
}

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 12

12

6
3/7/2023

if Statement Example

int main ( )
{
int x;
cout<<“Enter a number”;
cin>> x;
if ( x > 100 )
cout<<“the number is greater than 100”;
return 0;
}

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 13

13

Example

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 14

14

7
3/7/2023

if/else Selection Structure

• if
• Performs action if condition true
• if/else
• Different actions if conditions true or 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";

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 15

15

if-else Example
#inlcude<iostream>
using namespace std;
int main ()
{
int num;
cout<<"Enter any number\n";
cin>>num;
if (num>0)
cout<<“The number you entered is positive\n”;
else
cout<<“The number you entered is negative\n”;
return 0;
}

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 16

16

8
3/7/2023

So this means!!
if ( TRUE )
{
// Execute these statements if TRUE
}
else
{
// Execute these statements if FALSE
}

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 17

17

The If Selection Structure

• Compound statement
• Set of statements within a pair of braces

if ( grade >= 60 )
cout << "Passed.\n"; Statement1;
else
Statement2;
{
cout << "Failed.\n"; Statement3;
cout << "You must take this course again.\n";
}
• Without braces,
cout << "You must take this course again.\n";
always executed

• Block
• Set of statements within braces

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 18

18

9
3/7/2023

Example

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 19

19

The If Selection Structure


• Nested if/else structures
• One inside another, test for multiple cases
• Once condition met, other statements skipped
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”

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 20

20

10
3/7/2023

The If Selection Structure

Example
if ( grade >= 90 ) // 90 and above
cout << "A";
else if ( grade >= 80 ) // 80-89
cout << "B";
else if ( grade >= 70 ) // 70-79
cout << "C";
else if ( grade >= 60 ) // 60-69
cout << "D";
else // less than 60
cout << "F";

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 21

21

else..if example

int main ( )
{ int month;
cout<<“ Enter Month in Number= ”
cin>>month;
if ( month == 1 )
cout<<“ January”;
else if ( month == 2 )
cout << “ February ”;
else if ( month == 3 )
cout<< “ March ”;
:
else
cout << “ December ”;
return 0;
}

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 22

22

11
3/7/2023

The If Selection Structure


• You can assign an int type variable a non zero value for true or zero for false.
• Example
even = (n%2 == 0);
if(even) { do something }

• Some people prefer following for better readability.

if(even == 0) { do something }

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 23

23

The If Selection Structure


• Beginning programmers sometime prefer to use a sequence of if statements rather than a single nested if
statement

if (x > 0)
num_pos = num_pos + 1;
if (x < 0)
num_neg = num_neg + 1;
if (x == 0)
num_zero = num_zero +1;

• This is less efficient because all three of the conditions


are always tested.

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 24

24

12
3/7/2023

The If Selection Structure


• Nested ‘if’ statements

if(x > 0)
num_pos = num_pos + 1;
else if(x < 0)
num_neg = num_neg + 1;
else
num_zero = num_zero + 1;

• In the nested if statement, only the first condition is tested


when x is positive.

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 25

25

The If Selection Structure


• Nested if statements can become quite complex. If there are more than three alternatives and indentation is
not consistent, it may be difficult for you to determine the logical structure of the if statement.

• You can code the nested if as the multiple-alternative decision.

if ( condition_1 )
statement_1
else if ( condition_2 )
statement_2
.
.
.
else if ( condition_n )
statement_n
else
statement_e

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 26

26

13
3/7/2023

The If Selection Structure


• Order of Conditions
• When more than one condition in a multiple-alternative decision is true, only the task following the first true
condition executes.

• Therefore, the order of the conditions can affect the outcome.

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 27

27

The Conditional Operator

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 28

28

14
3/7/2023

The If Selection Structure

• Ternary conditional operator (?:)


– Three arguments (condition, value if true, value if false)
• Code could be written:
cout << ( grade >= 60 ? “Passed” : “Failed” );

false true
grade >= 60

print “Failed” print “Passed”

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 29

29

Conditional Operator

• The conditional operator ? and : is sometime called as ternary operator since


it takes three arguments

expression1 ? expression2 : expression3

• The above statement means that


“ if expression1 is true (i.e. if its value is non-zero),
then the value returned will be expression2
otherwise the value returned will be expression3”

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 30

30

15
3/7/2023

Example
int x, y;
cin>>x;
y = (x>5 ? 3 : 4);
cout<< y;

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 31

31

Points to be noted about Conditional Operators


• It is not necessary that the conditional operators should be used only in arithmetic statements

int i;
cin>> i;
(i==1 ? cout<<“ hello < 1”: cout<<“hello world != 1”);
• The conditional operators can be nested as shown below
int k, num = 30;
k=(num>5 ? (num <=10 ? 100 : 200) : 500);
cout<< k;

• The limitation of the conditional operator is that after the ? Or after the : only one C++ statement can
occur.

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 32

32

16
3/7/2023

Switch Statement

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 33

33

Switch Statement in C++


• Syntax
switch (selector)
{ case L1: statements1; break;
case L2: statements2; break;

default: statements_n;
}
• Semantics:
This statement has the same meaning as in the algorithmic language.

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 34

34

17
3/7/2023

Switch Statement
• If you have a large decision tree, and all the decisions depend on the value of the same variable, switch statement is suitable instead of
if..else or else..if statement.
switch ( integer expression )
{
case constant_1:
statement(s);
case constant_2:
statement(s);
:
:
case constant_n:
statement(s);
default:
statement;
}
CSC 113 – Computer Programming
Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 35

35

Switch Selection Structure


• Test variable for multiple values
• Series of case labels and optional default case

switch (choice) {
case 1 : do_option_one(); break;
case 2 :
case 3 : do_2_3_a ();
do_2_3_b (); break;
default : do_something_else (); }

• Value of the switch expression matched with one of the labels attached
to a branch
switch (choice) {
case 1 : do_option_one(); break;
case 2 :
case 3 : do_2_3_a ();
do_2_3_b (); break;
default : do_something_else (); }

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 36

36

18
3/7/2023

switch Structures

• Switch expression => the expression in parentheses whose value determines which switch label is
selected
• cannot be floating point
• usually is int or char
• Identifiers
following case
must be constants

switch (choice) {
case 1 : do_option_one(); break;
case 2 :
case 3 : do_2_3_a ();
do_2_3_b (); break;
default : do_something_else (); }

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 37

37

Switch Statement
• The integer expression following the keyword switch is an expression that will yield an integer value. It could
be an integer constant like 1, 2,or 3, or an expression that evaluates to an integer.

• The keyword case is followed by an integer or a character constant. The constant in each case must be
different from other.

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 38

38

19
3/7/2023

Example
int main ( )
{
int i = 2;
switch (i)
{
case 1:
cout<<“case 1 \n”;
case 2:
cout<<“case 2 \n”;
case 3:
cout<<“case 3 \n”;
default:
cout<<“default \n”;
} Output of the program
return 0;
} Case 2
Case 3
default

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 39

39

switch Structures
• The break causes control to be shifted to first statement after the switch statement

• The default statement is executed if the value of the switch expression is NOT found among switch labels

switch (choice) {
case 1 : do_option_one();
break;
case 2 :
case 3 : do_2_3_a ();
do_2_3_b ();
break;
default : do_something_else
();
}
// next statement

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 40

40

20
3/7/2023

Break Statement
• If we want that only particular case should be executed then break statement should be used to get out of
the control structure.
int i = 2;
switch (i)
{
case 1:
cout<<“case 1 \n”;
break;
case 2:
cout<<“case 2 \n”;
break;
case 3:
cout<<“case 3 \n”;
break;
default:
cout<<“default \n”;
}

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 41

41

Tips of using Switch Statement


a) As in the previous examples the cases are arranged in ascending order 1,2,3 and default. You can in fact
put the cases in any order you want.

b) You are also allowed to use char values in case and switch.

c) You can write a case in switch, which is not followed by any statement. As shown in the next example

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 42

42

21
3/7/2023

Tips of using Switch Statement

char ch;

cout<<“enter any of the alphabet a, b, or c”;

cin>>ch

switch(ch)
{
case ‘a’:
case ‘A’:
cout<<“you entered a”;
break;
case ‘b’:
case ‘B’:
cout<<“you entered b”;
break;
case ‘c’:
case ‘C’:
cout<<“you entered c”;
}

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 43

43

Tips of using Switch Statement


d) Even if there are multiple statements to be executed in each case there is no need to enclose these within
a pair of braces.
e) If we have no default case, then the program simply falls through the entire switch and continues with the
next instruction that follows the control structure.
f) The disadvantage of switch is that one cannot have a case in a switch which looks like
case i<=20:
All that we can have after the case is an int constant or a char constant. Even a float is not allowed.
g) The break statement when used in a switch and get executed takes the control outside the switch.

h) In principle, a switch may occur within another, but in practice it is rarely done.

i) The switch statement is very useful while writing menu driven programs.

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 44

44

22
3/7/2023

Example
char character; char character;
cout << "Enter a character : "; cout << "Enter a character : ";
cin >> character;
cin >> character; switch (character)
switch (character) {
{ case 'a’:
case 'a’: case ‘A’:
cout << " Australia " << endl; cout << " Australia " << endl;
cout << " Afaghanistan " << endl;
break; break;
case 'b': case 'b’:
cout << " Bangladesh " << endl; case ‘B’:
break; cout << " Bangladesh " << endl;
case 'c': break;
case 'c’:
cout << " Chille " << endl; case ‘C’:
break; cout << " Chille " << endl;
case 'd': break;
cout << " Denmark " << endl; case 'd’:
break; case ‘D’:
cout << " Denmark " << endl;
case 'e': break;
cout << " England " << endl; case 'e’:
break; case ‘E’:
case 'f': cout << " England " << endl;
cout << " France " << endl; break;
case 'f’:
break; case ‘F’:
case 'g': cout << " France " << endl;
cout << " Gana " << endl; break;
break; case 'g’:
default: case ‘G’:
cout << " Gana " << endl;
cout << " I dont know more countries " << endl; break;
} default:
cout << " I dont know more countries " << endl;
}
CSC 113 – Computer Programming
Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 45

45

Example

switch (character)
{
case 'a':
area = 3.14f * radius * radius;
cout << " Area = " << area << endl;
break;

case 'c':
circum = 2 * radius * 3.14f;
cout << " Circumference = " << circum << endl;
break;

default: cout << " Invalid letter was read " << endl;
}

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 46

46

23
3/7/2023

cout << "Enter the grade of student : "; cin >> character;
switch (character)
{
case 'A':
case 'a':
cout << "Excellent";
break;
case 'B':
case 'b':
cout << "Good";
break;
case 'C':
case 'c':
cout << "O.K";
break;
case 'D':
case 'd':
case 'F':
case 'f':
cout << "poor";
break;
default: cout << "invalid letter grade";
}

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 47

47

Example

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 48

48

24
3/7/2023

If-else vs. switch


char h; char h;
if (h == 'a')
switch (h)
cout << "h is a";
else {
if (h == 'b') case 'a': cout << "h is a";
cout << "h is b"; break;
else case 'b': cout << "h is b";
if (h == 'c') break;
//...and so on
case .....
}

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 49

49

Alternatives for switch


Multiple if Multiple if-else

If ( grade >=90)
cout<<“A”;
If ( grade >=80)
cout<<“B”;
If ( grade >=70)
cout<<“C”;
If ( grade > =60)
cout<<“D”;
If ( grade<60)
cout<<“F”;
CSC 113 – Computer Programming
Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 50

50

25
3/7/2023

break and continue Statements


• break statement
• Immediate exit from while, for, do/while, switch
• Common uses
• Escape early from a loop
• Skip the remainder of switch

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 51

51

switch Multiple-Selection Structure


.

true
case a case a action(s) break

false

true
case b case b action(s) break
false

.
.
.

true
case z case z action(s) break
false

default action(s)

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 52

52

26
3/7/2023

Example

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 53

53

Example

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 54

54

27
3/7/2023

Example
/* Determines the class of Ship given its class ID */
void main(void) {
char classID;

cout<<"Enter class id b, c or d:";


cin>>classID;

switch (classID) {
case 'B':
case 'b':
cout<<"Battleship”<<endl;
break;
case 'C':
case 'c':
cout<<"Cruiser" <<endl;
break;
case 'D':
case 'd':
cout<< "Destroyer"<< endl;
break;

default:
cout<< “Unknown class” << endl;
}
return 0;
}
CSC 113 – Computer Programming
Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 55

55

Exaplanation of Example
• This takes the value of the variable classID and compares it to each of the cases in a top down approach.
• It stops after it finds the first case that is equal to the value of the variable class.
• It then starts to execute each line of the code following the matching case till it finds a break statement or
the end of the switch statement.
• If no case is equal to the value of class, then the default case is executed.
• default case is optional. So if no other case is equal to the value of the controlling expression and there is a
default case, then default case is executed. If there is no default case, then the entire switch body is skipped
• The statements following a case label may be one or more C statements, so you do not need to make
multiple statements into a single compound statement using braces.
• It is important to remember that type int and char values may be used as case labels, type double values
cannot be used.
• Another very common error is the omission of the break statement at the end of one alternative.
• In such a situation, execution ”falls through” into the next alternative.
• Forgetting the closing brace of the switch statement body is also easy to do.
CSC 113 – Computer Programming
Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 56

56

28
3/7/2023

If versuses switch
• Advantages of if:
• It is more general than a switch
• It can be a range of values such as x < 100
• A switch can not compare doubles

• Advantages of switch:
• A switch is more readable

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 57

57

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 58

58

29
3/7/2023

Example – Missing break


void main ( )
{
int i ;
cin>>i;
switch (i)
{
case 1:
cout<<“case 1 \n”;
case 2:
cout<<“case 2 \n”;
case 3:
cout<<“case 3 \n”; Output of the program
default: (Assume user enters 2)
cout<<“default \n”;
Case 2
}
Case 3
}
default
CSC 113 – Computer Programming
Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 59

59

Multiple Selections
• Contrast
• A sequence of
if … else if … statements

• A sequence of separate if statements

• What happens in each case when it is the first if condition that is true?
• if … else if sequence will jump out of the structure whenever match is found
• sequence of separate if's – each if is checked, no mater where the match is

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 60

60

30
3/7/2023

Checking Multiple Conditions with Boolean Operators

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 61

61

Points to Remember – switch statement


• The expression followed by each case label must be a constant expression.
• No two case labels may have the same value.
• Two case labels may be associated with the same statements.
• The default label is not required.
• There can be only one default label, and it is usually last.

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 62

62

31
3/7/2023

Example
• Write a program that takes a character from user and prints if the character is vowel or consonant.

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 63

63

Example
char myChar;
cout << "Enter an Alphabet : ";
cin >> myChar;

if (myChar >= '0' && myChar <= '9’)


cout << "ERROR!!\nYou have entered a digit";

else if ((myChar >= 97 && myChar <= 122) || (myChar >= 65 && myChar <= 90))
{
if (myChar == 'a' || myChar == 'e' || myChar == 'i' || myChar == 'o' || myChar ==
'u' || myChar == 'A' || myChar == 'E' || myChar == 'I' || myChar == 'O' || myChar ==
'U’)
cout << "You have entered a VOWEL";
else
cout << "You have entered a CONSONANT";
}
else
cout << "ERROR!!\nYou did not enter an Alphabet";

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 64

64

32
3/7/2023

Example

int myNumber;
cout << "Enter a Postive Number : ";
cin >> myNumber;

if (myNumber > 0)
{
if (myNumber % 2 == 0)
{
cout << "Number is EVEN number " << endl;
}
else
cout << "Number is ODD number " << endl;
}
else
{
cout << "You have entered a -ve number " << endl;
}

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 65

65

Activity
• Create the equivalent of a four-function calculator. The program should ask the user to enter a number, an
operator, and another number. (Use floating point.) It should then carry out the specified arithmetical
operation: adding, subtracting, multiplying, or dividing the two numbers. Use a switch statement to select
the operation. Finally, display the Result.
• Some sample interaction with the program might look like this:
Enter first number, operator, second number: 12 + 100
Answer = 112

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 66

66

33
3/7/2023

Task 1
• Write a temperature-conversion program that gives the user the option of converting Fahrenheit to Celsius
or Celsius to Fahrenheit. Then carry out the conversion. Use floating-point numbers. Interaction with the
program might look like this:

Type 1 to convert Fahrenheit to Celsius,


2 to convert Celsius to Fahrenheit: 1
Enter temperature in Fahrenheit: 70
In Celsius that’s 21.111111

°C x 9/5 + 32 = °F
(°F - 32) x 5/9 = °C

CSC 113 – Computer Programming


Department of Computer Sciences
3. Control Structures
Bahria University, Islamabad
Slide: 67

67

34

You might also like