slides pst 2a
DESCRIPTION
Viewing of components in rdbmsTRANSCRIPT
1
2
3
4
5
6
7
8
9
10
11
Some examples :-
int iN1,iN2,iN3 ,iN =0;
iN1=1,iN2=2,iN3=3;
1) if(iN > iN2)
printf(“iN1 is larger”);
else
printf(“iN2 is larger”);
2) if(iN1 > iN2)
printf(“iN1 is larger”);
else if(iN1 < iN2)
printf(“iN2 is larger”);
else
printf(“Both iN1 and iN2 are equal”);
12
3) if(iN==1) /* prints iN is not one*/
printf(“iN is one”);else
printf(“iN is not one”);4) if(iN=1) /* prints iN1 is one*/
printf(“iN is one”);else
printf(“iN1 is not-one”);In the above example the expr that’s evaluated is an assignment expr and not a conditional expr. (becoz of = and not ==). The expr will assign a non-zero value to iN and then iN is evaluated. Since it contains a non-zero value (indicating true evaluation) the if part is executed5) if(iN1=0) /*prints iN1 is non-zero*/
printf(“iN1 is zero”);else
printf(“iN1 is non-zero”);In the above example the expr that’s evaluated is an assignment expr and not a conditional expr. (becoz of = and not ==). The expr will assign a zero value to iN1 and then iN1 is evaluated. Since it contains a zero value (indicating false evaluation) the else part is executed6) if(5) /*prints true.*/
printf(“True’); /*becoz expr evaluates to a non-zero value.*/
else printf(“false”);
13
7) if(0) /*prints false*/printf(“True”); /* becoz the expr evaluates to zero value*/
elseprintf(“False”);
8) if(iN==3); /*gives a compilation as if terminates after expr.*/printf(“true”); /* this means there is no matching if for the else below*/
else /* hence a error at compilation time is reported*/printf(“false”);
9) if(iN1==1 && iN2 <3) /*prints true as the expr evaluates to true – T && T*/printf(“True”);else
printf(“false”);10) if(!4) /* not of a non zero value is zero hence false is printed*/
printf(“true”);elseprintf(“false”);
14
15
Guidelines while writing if statements:
(1)Write the nominal path first then write the exceptions- put the nominal case in the if part rather than in the else part.
(2)Make sure that you branch correctly on equality-using > instead of >= makes an off-by-one error.
(3)Follow the if clause with a meaningful statement - Do not use if statement as shown below:
Ex: if (somecondition) ;
else
/* do something*/
Instead use:
if ( ! Somecondition)
/* do something */
16
17
18
19
20
21
22
23
24
•The switch statement is a multi directional conditional control statement.
•Whenever a choice is to be made among a of alternatives, we use switch statement.
•The switch block is used as an alternative to if construct when the value in a variable needs to be matched against a set to values.
•The expression needs to be of integral type only. All case values need to be unique. number
•The switch control construct helps in controling complex conditional and branching operations. The expr value is matched against all case values. If a match is found the case block executes and the control comes out of the switch . In absence of a break statement the execution continues with the next case block till it encounters a break / the switch block terminates .
•If expression does not match with any of the listed case values then the default block executes. The default is an optional statement and need not be at the end of switch.
•This is one of the code tuning techniques in which we place cases according to the frequency in which they occur to reduce the number of comparisons. More about this we discuss in code tuning techniques in the later day slides
The break statement has two uses:
•To terminate the case in the switch statement (refer to switch slides)
•To termination a loop. More on this we discuss in looping constructs in the later slides
25
26
27
28
29
30
31
When to use what?
1. If there are simple and small number of comparison statements then using if construct is a good idea
2. If there is a list of values against which a variable has to be compared then using a switch construct is a good idea
3. If there are expressions against which comparisons are to be made and there are many comparisons to be done , and if..else if construct can be used
32
33
34
35
36
37
38
39
40
41
42
system(cls) is used for clearing the screen by invoking an OS command cls. This is present in stdlib.h header file. The getch() is used for waiting for a key press. This is present in conio.h header file. Both the cls and getch() run on windows platform
43
44
45
46
47
48
49
50
51
52
break statement:
A break statement can be used inside a loop or a switch case construct. We havealready seen the use of break statement inside the switch case construct.
When a break statement is encountered inside a loop the execution of the loop stops.
53
54
55
56
57
58
59
60
61
62
63
1. for loop2. A do..while loop as the menu has to be shown atleast once and then depending on the choice of the user appropriate
action can be taken3. Yes For example: for(iNum=0;iNum<10;iNum++);4. Only the inner loop is terminated 5. while(1) or for(;;) or do -- while(1)
64
65
66
67