[go: up one dir, main page]

0% found this document useful (0 votes)
11 views9 pages

Lecture 06 Bscs

F.g

Uploaded by

bc240222245rsh
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 views9 pages

Lecture 06 Bscs

F.g

Uploaded by

bc240222245rsh
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/ 9

LECTURE NOTES - CC-112 PROGRAMMING FUNDAMENTALS PROF. DR.

SYED WAQAR UL QOUNAIN

2022-09-01 LECTURE 06 – PROGRAM CONTROL – I

 contents
o do while control structure
o operators
 assignment operator
 increment and decrement operator
 conditional operator
o for control structure
o switch multiple selection statement
o break and continue statement
o concepts discussed in the lecture
o exercises

 do while control structure


o the do…while repetition statement is similar to the while statement
o the while repetition statement tests its loop-continuation condition before executing the
loop body
o the do…while repetition statement tests its loop-continuation condition after executing
the loop body,
o the loop body in the do…while repetition statement always executes at least once
 syntax of a counter controlled repetition is as follows
define a counter or sentinel variable
do {
set-of-statements which should be executed if the condition in
parentheses is true

increment in counter variable or set value of sentinel variable


} while (write a condition on counter/sentinel variable);

 example (using do…while counter controlled repetition): a program that


prompts user to enter a number and displays numbers starting form 0 till that
number at console.
// L06-C01
1. int main (void) {
2. int a;
3. printf(“enter a number\t”);
4. scanf(“%d”, &a);
5. int counter = 0;
6. do {
7. printf(“%d\t”, counter);
8. counter = counter + 1;
9. } while (counter <= a);
10. }
 input: enter a number 5

DEPARTMENT OF INFORMATION TECHNOLOGY UNIVERSITY OF THE PUNJAB, LAHORE Page 1 of 9


LECTURE NOTES - CC-112 PROGRAMMING FUNDAMENTALS PROF. DR. SYED WAQAR UL QOUNAIN

 output: 0 1 2 3 4 5

 Line 5: define and initialize a counter variable named counter with value 0.
 Line 6: starts the body of do…while repetition control statement
 Line 7: displays the value of counter at console as required
 Line 8: is an increment statement which increases the value of counter variable
by 1 on each repetition
 Line 9: defines a condition on the counter variable which would be true (1) if the
value in counter variable would be less than or equal to the value in variable a
(value entered by the user in this program)

 example (using while sentinel-controlled repetition): a program that prompts


user to enter marks of students in a subject till user enters a 0 or a negative
number, then display average of students marks at console.
// L06-C02
1. int main (void) {
2. int a, sum=0;
3. float average;
4. int counter = 0;
5. int sentinel = 1;
6. do {
7. printf(“enter marks of a student:\t”);
8. scanf(“%d”, &a);
9. if ( a > 0 ) {
10. sum = sum + a;
11. counter = counter + 1;
12. }
13. else {
14. sentinel = 0;
15. }
16. } while ( sentinel != 0 );
17. average = (float) sum / counter;
18. printf(“average marks of the class are: \t%.2f”, average);
19. }
 output/input: enter marks of a student: 65
 output/input: enter marks of a student: 92
 output/input: enter marks of a student: 73
 output/input: enter marks of a student: 67
 output/input: enter marks of a student: 0
 output: average marks of the class are: 74.25

 Line 3: a float type variable named average is defined to average of the class
 Line 4: define and initialize a counter variable named counter with value 0
 Line 5: define and initialize a sentinel variable named sentinel with value 1

DEPARTMENT OF INFORMATION TECHNOLOGY UNIVERSITY OF THE PUNJAB, LAHORE Page 2 of 9


LECTURE NOTES - CC-112 PROGRAMMING FUNDAMENTALS PROF. DR. SYED WAQAR UL QOUNAIN

 Line 6: starts the body of do…while repetition control statement


 Line 7&8: prompts user to enter student makers, takes input and store it in
variable a
 Line 9-15: (line 9) checks whether the value entered by the user a is greater than
0, if it is true then (line 10&11) add this value in sum and increase the value of
counter variable, otherwise in else block (line 12-15) set the value of sentinel
variable equal to zero (0) which will terminates this repetition structure before
next repetition
 Line 16: defines a condition on the sentinel variable which would be true (1) if
the value in sentinel variable is not equal to 0
 Line 17: to avoid integer division and convert this division of two integer numbers
into a float explicit type casting of expression sum / counter is used with an
expression (float) in front it
 Line 18: average marks of the class are displayed using format specifier %.2f
which will display the value of floating point variable average till to decimal places
 operators
o assignment operator
 assignment operator “=” is used to assign value of a variable or constant to
another variable
 C provides several assignment operators for abbreviating assignment
expressions. For example, the statement
a = a + 3;
can be abbreviated with the addition assignment operator += as
c += 3;
similarly a = a – 3 can be written a -= 3, a = a * 3 can be written a *= 3, a = a / 3
can be written a /= 3 and a = a % 3 can be written a %= 3
o increment and decrement operator
 The unary increment operator (++) and the unary decrement operator (--) add
one to and subtract one from an integer variable, respectively
 for example a++ and a—will add on to and subtract on from the variable a
 increment operator has two versions which are pre-increment (++a) and post-
increment (a++)
 ++a: increment a by 1, then use the new value of the variable a in the
expression in which a resides
 a++: use the current value of variable a in the expression in which a
resides, then increment a by 1.
 decrement operator has two versions which are pre-decrement (--a) and post-
decrement (a--)
 --a: decrement a by 1, then use the new `value of the variable a in the
expression in which a resides
 a--: use the current value of variable a in the expression in which the
variable a resides, then decrement a by 1.
o conditional operator
 conditional operator closely relates with if – else statement

DEPARTMENT OF INFORMATION TECHNOLOGY UNIVERSITY OF THE PUNJAB, LAHORE Page 3 of 9


LECTURE NOTES - CC-112 PROGRAMMING FUNDAMENTALS PROF. DR. SYED WAQAR UL QOUNAIN

 syntax of the conditional operator is


 (condition) ? first-expression : second-expression
 it is the only ternary operator in C language which means it has three operands
 first operand is a condition
 second operand is a conditional expression which means if the condition
provided in first operand would be true (1) then the expression in second
operand would be executed
 third operand is also a conditional expression which means if the
condition provided in first operand would be false (0) then the expression
in third operand would be executed
 a = 10
 b = (a >10) ? 10 : 0;
 as the condition (a>10) is false hence in the above statement 0 would be
stored in variable b
 example (using assignment, increment, decrement and conditional operators):
a program that uses addition assignment, increment, decrement and conditional
operators on variable, and display resultant values at console.
// L06-C03
1. int main (void) {
2. int a=5;
3. a+=5;
4. printf(“the value of a is: \n”, a);
5. printf(“the value of a in preincrement is: \n”, ++a );
6. printf(“the value of a in postincrement is: \n”, a++ );
7. printf(“the value of a in predecremnet is: \n”, --a );
8. printf(“the value of a in postidecrement is: \n”, a-- );
9. printf(“the value of a in conditional operator is: \n”, (a>10)? 10 : 0 );
10. }

 output: the value of a is: 10


 output: the value of a in preincrement is: 11
 output: the value of a in postincrement is: 11
 output: the value of a in predecremnet is: 11
 output: the value of a in postidecrement is: 11
 output: the value of a in conditional operator is: 0

 Line 3: 5 is added in the value of variable a using addition assignment operator


(the result is assigned (store in) to variable a), the value of variable a becomes 10
 Line 4: current value of variable a which 10 is printed at console
 Line 5: a preincrement operator is used with variable a which increased the value
of a variable by 1 and then the printf function has used the newly assigned value
to print at console, hence 11 is printed
 Line 6: a postincrement operator is used with variable a which will increase the
value of a variable by 1 after the printf function used the existing value of the

DEPARTMENT OF INFORMATION TECHNOLOGY UNIVERSITY OF THE PUNJAB, LAHORE Page 4 of 9


LECTURE NOTES - CC-112 PROGRAMMING FUNDAMENTALS PROF. DR. SYED WAQAR UL QOUNAIN

variable a to print at console, hence 11 is printed, and after printing the value of
variable a will become 12
 Line 7: a predecrement operator is used with variable a which decrease the value
of variable a by 1 and then the printf function will use the newly assigned value
to print at console, hence 11 is printed
 Line 8: a postdecrement operator is used with variable a which will decrease the
value of variable a by 1 after the printf function used the existing value of the
variable a to print at console, hence 11 is printed, and after printing the value of
variable a will become 10
 Line 9: a conditional operator is used with a condition (a>10) on variable a, as
the current value of the variable a is 10, which is not greater than 10, hence the
conditional operator will execute and return third operand and 0 will be printed
at console

 for control structure
o counter-controlled iteration requires:
 the name of a control variable,
 the initial value of the control variable,
 the increment (or decrement) by which the control variable is modified in each
iteration, and
 the loop-continuation condition that tests for the final value of the control
variable to determine whether looping should continue
 the for iteration statement handles all the details of counter controlled iteration
for readability, try to fit the for statement’s header on one line
 syntax of a for counter controlled repetition is as follows
for (initialization of counter; condition on counter; incr./decr. on counter) {

set-of-statements which should be executed if the condition on counter


variable is true
}

 example (using for counter controlled repetition): a program that prompts user
to enter a number and displays numbers starting form 0 till that number at
console.
// L06-C04
1. int main (void) {
2. int a;
3. printf(“enter a number\t”);
4. scanf(“%d”, &a);
5. for (int counter = 0; counter <= a; counter = counter + 1) {
6. printf(“%d\t”, counter);
7. }
8. }
 input: enter a number 5
 output: 0 1 2 3 4 5

DEPARTMENT OF INFORMATION TECHNOLOGY UNIVERSITY OF THE PUNJAB, LAHORE Page 5 of 9


LECTURE NOTES - CC-112 PROGRAMMING FUNDAMENTALS PROF. DR. SYED WAQAR UL QOUNAIN

 Line 5: contains three statements initialization, condition and increment as


follows
 define and initialize a counter variable named counter with value 0
 defines a condition on the counter variable which would be true (1) if the
value in counter variable would be less than or equal to the value in
variable a (value entered by the user in this program)
 is an increment statement which increases the value of counter variable
by 1 on each repetition
 Line 6: displays the value of counter at console

 example (using for counter controlled repetition): a program that prompts user
to enter two numbers and displays all numbers between them at console.
// L06-C05
1. int main (void) {
2. int a, b;
3. printf(“enter two numbers\t”);
4. scanf(“%d”, &a);
5. scanf(“%d”, &b);
6. for (int counter = a + 1; counter < b; counter = counter + 1) {
7. printf(“%d\t”, counter);
8. }
9. }
 input: enter two numbers 5 9
 output: 6 7 8
 Line 6: for control controlled repetition header has three statements as follows
 define and initialize a counter variable named counter with value one
greater than the value in variable a (first value entered by the user)
 defines a condition on the counter variable which would be true (1) if the
value in counter variable would be less than to the value in variable b
(second value entered by the user)
 an increment statement which increases the value of counter variable by
1 on each repetition
 Line 7: displays the value of counter at console as required
 switch multiple selection statement
o recall that the if single-selection and the if – else double-selection statements are used to
introduce selection control structure in a C program
o sometimes a programmer needs to write a program that contain a series of decisions
based on conditions on a single variable to perform different actions which is called
multiple selection and could be achived using if – else if – else if – else staements
o alternatively for a multiple selection C language provides a selection control structure
named switch
o the switch multiple selection statement consists of a series of case labels, an optional
default label and statements to execute for label
o syntax of a switch statement is as follows

DEPARTMENT OF INFORMATION TECHNOLOGY UNIVERSITY OF THE PUNJAB, LAHORE Page 6 of 9


LECTURE NOTES - CC-112 PROGRAMMING FUNDAMENTALS PROF. DR. SYED WAQAR UL QOUNAIN

switch (expression or variable) {

case value1:
set-of-statements to be executed for value1;
break;

case value2:
set-of-statements to be executed for value1
break;

. ..

default:
set-of-statements to be executed when expression or variable does not have
values value1, value2 etc.
}
 in above syntax the value produce by the expression or the value in the variable
would be matched with different cases written inside body of the switch
statement
 if it is match then respective case would be executed till the first break statement
is encountered
 otherwise the statements written in the default label would be executed
 note that the default label is optional in the switch statement
 example (using switch multiple selection statement): a program that prompts user to
enter two numbers and an arithmetic operator, performs respective operation on user
inputted numbers and displays the result at console, if user does not enter a correct
operator it displays a message stating “you entered an invalid operator”
// L06-C06
1. int main (void) {
2. int a, b;
3. char op.
4. printf(“enter a number, operator (+, -, *, /, %), a number like:\t a+b \n”);
5. scanf(“%d”, &a);
6. op = getchar();
7. scanf(“%d”, &b);
8. switch (op) {
9. case ‘+’:
10. printf(“%d”, a+b);
11. break;
12. case ‘-’:
13. printf(“%d”, a-b);
14. break;
15. case ‘*’:
16. printf(“%d”, a*b);
17. break;
18. case ‘/’:

DEPARTMENT OF INFORMATION TECHNOLOGY UNIVERSITY OF THE PUNJAB, LAHORE Page 7 of 9


LECTURE NOTES - CC-112 PROGRAMMING FUNDAMENTALS PROF. DR. SYED WAQAR UL QOUNAIN

19. printf(“%d”, a/b);


20. break;
21. case ‘%’:
22. printf(“%d”, a%b);
23. break;
24. defualt:
25. printf(“you entered an invalid operator”);
26. }
27. }
 output: enter a number, operator (+, -, *, /, %), a number like: a+b
 input: 5+9
 output: 14

 Line 2-7: three variables are defined and input is taken from the user
 Line 8: character type variable op is written inside parenthesis of the switch
statement, which means that the values stored in variable op would be passed to
switch statement
 Line 9-11: if the value passed to the switch statement is a character ‘+’ then the
statements written below the case ‘+’ (line 10) would be executed till a break
statement at line 11 is reached, on executing the break statement the program
control will come out of the body of switch statement
 Line 12-14: incase the value passed to the switch statement is not ‘+’ but rather it is
‘-‘ then the statements written below the case ‘-’ (line 13) would be executed till a
break statement at line 14 is reached, on executing the break statement the program
control will come out of the body of switch statement
 Line 15-23: the same process mentioned above would be executed for character
values ‘*’, ‘/’ and ‘%’
 Line 24-25: incase the users did not enter either of characters ‘+’, “-‘, ‘*’, ‘/’ or ‘%’,
then the statements written under default label would be executed which will print
“you entered an invalid operator” at console and the program will terminate
 break and continue statement
o the break and continue statements are used to alter the flow of program control
o break is used to exits control of a program from the body of a control structure
 as it is seen in switch multiple selection statement that a break statement exits
control of a program from the body of the switch statement immediately when
encountered
 the break statement, when executed in a while, for, do…while or switch
statement, causes an immediate exit from that statement
 program execution continues with the next statement after that while, for,
do…while or switch.
 the break statement is to escape early from a loop or skip the remainder of a
switch statement
o continue statement skips the remaining statements of repetition control structure and
perform the next repetition

DEPARTMENT OF INFORMATION TECHNOLOGY UNIVERSITY OF THE PUNJAB, LAHORE Page 8 of 9


LECTURE NOTES - CC-112 PROGRAMMING FUNDAMENTALS PROF. DR. SYED WAQAR UL QOUNAIN

 the continue statement, when executed in a while, for or do…while statement,


skips the remaining statements in that control statement’s body and performs
the next iteration of the loop
 in while and do…while statements, the loop-continuation test is evaluated
immediately after the continue statement executes
 in the for statement, the increment expression executes, then the loop-
continuation test is evaluated
 example (using break and continue statements): a program that uses break and
continue statements
// L06-C07
1. int main (void) {
2. int a, b;
3. printf(“enter two numbers \n”);
4. scanf(“%d”, &a);
5. scanf(“%d”, &b);
6. printf(“\n break in while \t”);
7. while (a < b) {
8. printf(“%d\t”, a);
9. a = a + 1;
10. if ( b % a == 0) {
11. break;
12. }
13. }
14. printf(“\n continue in for\t”);
15. for (a = 1; a < b ; a++) {
16. if ( b % a == 0) {
17. continue;
18. }
19. printf(“%d\t”, a);
20. }
21. }

 output: enter two numbers


 input: 3
 input: 12
 output: break in while 3
 output: continue in for 5 7 8 9 10 11
 concepts discussed in the lecture
o do…while, arithmetic assignment operators, increment operator, decrement operator,
preincrement operator, predecrement operator, postincrement operator,
postdecrement operator, conditional operator, for repetition control structure, switch
multiple selection statement, case, default, getchar(), break statement, continue
statement

DEPARTMENT OF INFORMATION TECHNOLOGY UNIVERSITY OF THE PUNJAB, LAHORE Page 9 of 9

You might also like