C Programming
C Programming
ESC101
August 21st
Announcements
• Should have received minor quiz 1 sheets
• Should have received lab 2 scores
• Major quiz 1 on August 28th
– In class, during class hours
• Syllabus
– Everything up to the coming Friday’s lecture
• Tomorrow’s tutorial
– Spatial orientation test
• Not graded for course
• Voluntary. Don’t have to do it if you don’t want to.
Copying
• Copy at your own risk
in any component (lab/quiz/exams/lab exams).
• If you are caught, you get penalized on grade (most
likely F).
Will not be allowed to drop the course
Case reported to DoAA/SSAC
No warning or second chance
All parties involved in copying will be held equally
responsible. Copying from internet is penalized equally.
Policy may change on need basis
3
Esc101, Programming
This class
• More about if-else statements
• Switch-case
• Break
If-else review
#include<stdio.h>
int main() {
int i = 5, j = 6, k = 7;
if(i > j == k)
printf("%d %d %d", i++, ++j, --k);
else 567
printf("%d %d %d", i, j, k);
return 0;
}
#include<stdio.h>
int main() {
int i = 5;
if(i = i - 5 > 4) Inside else block
printf("inside if block");
else
printf("inside else block");
return 0;
}
If-else review
#include<stdio.h>
int main() {
if(sizeof(0))
printf(“Yes");
else Yes
printf(“No");
return 0;
}
#include <stdio.h>
int main()
{
char val=1;
if(val--==0) FALSE
printf("TRUE");
else
printf("FALSE");
return 0;
}
Trick questions
• Choice of compiler affects output
• Will not be on exam
• If in doubt, give your answer
– With justification
• Will mark with from now on
Unmatched if and else
X
if ((a != 0) && (b != 0)) OUTPUT for a = 5, b = 0
NO OUTPUT!!
if (a * b >= 0) OUTPUT for a = 5, b = -5
printf (“positive”); zero
else
printf(“zero”);
if (cond1) if (cond1) {
if (cond2) if (cond2)
… …
else else
… …
8/18/19 }
Esc101, Programming 9
Unmatched if and else
• An else always matches closest unmatched if
– Unless forced otherwise using { … }
if (cond1) if (cond1) {
if (cond2) AS if (cond2)
E
M
… SA …
T
else N
O }
… IS else
8/18/19 Esc101, Programming… 10
Else if
• A special kind of nesting is the chain of if-
else-if-else-… statements
if (cond1) { if (cond1)
stmt1 stmt-block1
int day;
scanf (“%d”, &day);
if (day == 1) { printf(“Sunday”); }
else if (day == 2) { printf (“Monday”); }
else if (day == 3) { printf (“Tuesday”); }
else if (day == 4) { printf (“Wednesday”); }
else if (day == 5) { printf (“Thursday”); }
else if (day == 6) { printf (“Friday”); }
else if (day == 7) { printf (“Saturday”); }
else { printf (“ Illegal day %d”, day); }
8/18/19 Esc101, Programming 14
Example 2
int day;
scanf (“%d”, &day);
if (day == 1) { printf(“Weekend”); }
else if (day == 2) { printf (“Weekday”); }
else if (day == 3) { printf (“Weekday”); }
else if (day == 4) { printf (“Weekday”); }
else if (day == 5) { printf (“Weekday”); }
else if (day == 6) { printf (“Weekday”); }
else if (day == 7) { printf (“Weekend”); }
else { printf (“ Illegal day %d”, day); }
8/18/19 Esc101, Programming 16
Weekday - version 2
int day;
scanf (“%d”, &day);
if ((day == 1) || (day == 7)) {
printf(“Weekend”);
} else if ( (day == 2) || (day == 3)
|| (day == 4) || (day == 5)
|| (day == 6)) {
printf (“Weekday”);
} else {
printf (“ Illegal day %d”, day);
}
8/18/19 Esc101, Programming 17
Weekday - version 3
int day;
scanf (“%d”, &day);
if ((day == 1) || (day == 7)) {
printf(“Weekend”);
} else if ( (day >= 2) && (day <= 6) ) {
printf (“Weekday”);
} else {
printf (“ Illegal day %d”, day);
}
switch (day) {
case 1: printf(“Sunday”); break;
case 2: printf (“Monday”); break;
case 3: printf (“Tuesday”); break;
case 4: printf (“Wednesday”); break;
case 5: printf (“Thursday”); break;
case 6: printf (“Friday”); break;
case 7: printf (“Saturday”); break;
default: printf (“ Illegal day %d”, day);
}
8/18/19 Esc101, Programming 20
Weekday, version 4
switch (day) {
case 1:
case 7: printf (“Weekend”); break;
case 2:
case 3:
case 4:
case 5:
case 6: printf (“Weekday”); break;
default: printf (“ Illegal day %d”, day);
}
8/18/19 Esc101, Programming 21
The break statement
• Syntax –
– break;
• Used to break out of current code branch
– Not to end the program
General Form of switch-case
switch (selector-expr) {
Expr only of
case label1: s1; break; type INT
case label2: s2; break; Execution starts at
the matching case.
...
case labelN: sN; break;
default : sD;
} • default is optional. (= remaining cases)
• The location of default does not matter.
• The statements following a case label are
executed one after other until a break is
8/18/19
encountered (Fall Through)
Esc101, Programming 23
Example
• Switch expressions can only be of type int
#include<stdio.h>
int main() {
char ch = 65;
switch(ch) {
case 'A': printf("Apple");
break; Apple
case 'B': printf("Bing");
break;
default: printf("Bye");
break;
}
return 0;
}
General Form of switch-case
switch (selector-expr) {
Expr only of type
case label1: s1; break; INT
case label2: s2; break; Execution starts at
the matching case.
...
case labelN: sN; break;
default : sD;
} • default is optional. (= remaining cases)
• The location of default does not matter.
• The statements following a case label are
executed one after other until a break is
8/18/19
encountered (Fall Through)
Esc101, Programming 25
Fall Through…
int n = 100;
int digit = n%10; // last digit
switch (digit) {
default : printf(“Not divisible by 5\n”);
break;
case 0: printf(“Even\n”);
case 5: printf(“Divisible by 5\n”);
break;
} What is printed by the Answer:
program fragment? Even
8/18/19 Divisible by 5;
Esc101, Programming 26
Class Quiz
• What is the value of expression:
(5<2) && (3/0)
a) Compile time error
8/18/19
e) 1 Esc101, Programming 27
Short-circuit evaluation
• Do not evaluate the second operand of binary
logical operator if result can be deduced from
first operand
– Arguments of && and || are evaluated from left to
right (in sequence)
– Also applies to nested logical operators
1 0 0 1