0% found this document useful (0 votes)
11 views

Week 04

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Week 04

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

CE-116

COMPUTER PROGRAMMING
Muzammil Ahmad Khan
[email protected]

Computer Engineering Department


WEEK NO:04

2
The Controlling Statement
Switch statement
• A switch statement's controlling statement must return one
of these basic types:
• -A bool value
• An int type
• A char type
• switch will not work with strings in the controlling statement.

Better Way͙ Using switch


• An alternative for constructing multi-way branches
3
The Controlling Statement
Switch statement

4
Can I Use the break Statement in a Loop?

5
C++ Switch Statements

• Use the switch statement to select one of many code blocks to be executed.
• Syntax
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
6
How it works:

• The switch expression is evaluated once


• The value of the expression is compared with the values of
each case
• If there is a match, the associated block of code is executed
• The break and default keywords are optional, and will be
described later in this chapter

7
The example below uses the weekday number
to calculate the weekday name:

8
The break Keyword

• When C++ reaches a break keyword, it breaks out of the switch block.

• This will stop the execution of more code and case testing inside the block.

• When a match is found, and the job is done, it's time for a break. There is
no need for more testing.

• A break can save a lot of execution time because it "ignores" the execution
of all the rest of the code in the switch block.

9
The default Keyword

10
The default Keyword

11
Blocks

12
Local vs. Global Variables

13
Local vs. Global Variables - Example

14
Local vs. Global Variables - Example

Output??

15
Local vs. Global Variables - Example

Output??
CS 98.CS 98.CS 98

(there’s a newline character at the end)

16
goto Statement

• In C++ programming, the goto statement is used for altering the


normal sequence of program execution by transferring control
to some other part of the program.
• Syntax of goto Statement
goto label;
... .. ...
... .. ...
... .. ...
label:
statement;
... .. ...
• In the syntax above, label is an identifier. When goto label; is
encountered, the control of program jumps to label: and
17
executes the code below it.
Example: goto Statement
// This program calculates the average of numbers entered by the user.
// If the user enters a negative number, it ignores the number and
if(num < 0.0)
// calculates the average number entered before it.
{
// Control of the program move to jump:
# include <iostream>
using namespace std; goto jump;
int main() }
{ sum += num;
float num, average, sum = 0.0; }
int i, n;
cout << "Maximum number of inputs: ";
jump:
cin >> n;
average = sum / (i - 1);
for(i = 1; i <= n; ++i)
{
cout << "\nAverage = " << average;
cout << "Enter n" << i << ": "; return 0;
cin >> num; }
18
Output

Maximum number of inputs: 10


Enter n1: 2.3
Enter n2: 5.6
Enter n3: -5.6
Average = 3.95

• You can write any C++ program without the use of goto statement and
is generally considered a good idea not to use them.

19
Reason to Avoid goto Statement

• The goto statement gives the power to jump to any part of a program but,
makes the logic of the program complex and tangled.

• In modern programming, the goto statement is considered a harmful


construct and a bad programming practice.

• The goto statement can be replaced in most of C++ program with the use of
break and continue statements.

20
Enumeration
• An enumeration is a user-defined data type that consists of integral
constants. To define an enumeration, keyword enum is used.
• enum season { spring, summer, autumn, winter };
• Here, the name of the enumeration is season.
• And, spring, summer and winter are values of type season.
• By default, spring is 0, summer is 1 and so on. You can change the default
value of an enum element during declaration (if necessary).
enum season
{ spring = 0,
summer = 4,
autumn = 8,
winter = 12
};
21
Enumerated Type Declaration

• When you create an enumerated type, only blueprint for the variable is created. Here's
how you can create variables of enum type.

enum boolean { false, true };

// inside function
enum boolean check;

• Here is another way to declare same check variable using different syntax.
enum boolean
{
false, true
} check;
22
Example 1: Enumeration Type
#include <iostream>
using namespace std;

enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };

int main()
{
week today;
today = Wednesday;
cout << "Day " << today+1;
return 0;
}

Output
Day 4
23
Example2:
Changing Default Value of Enums
#include <iostream>
using namespace std;

enum seasons { spring = 34, summer = 4, autumn = 9, winter = 32};


int main() {
seasons s;
s = summer;
cout << "Summer = " << s << endl;
return 0;
}

Output
Summer = 4
24
Example2:
Changing Default Value of Enums
#include <iostream>
using namespace std;

enum seasons { spring = 34, summer = 4, autumn = 9, winter = 32};


int main() {
seasons s;
s = summer;
cout << "Summer = " << s << endl;
return 0;
}

Output
Summer = 4
25

You might also like