Lecture 06 Bscs
Lecture 06 Bscs
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
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)
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
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) {
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
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
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 ‘/’:
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