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

C Programming

The document discusses conditionals like if-else statements and switch-case statements in C programming. It provides examples of using if-else to check conditions and print outputs accordingly. It also explains the use of else if to chain multiple conditions and switch-case as an alternative to if-else for multi-way decisions. Key points covered include the matching of else with the closest if, using break to break out of code blocks, and that switch expressions can only be of type int.

Uploaded by

Aditya Tiwari
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

C Programming

The document discusses conditionals like if-else statements and switch-case statements in C programming. It provides examples of using if-else to check conditions and print outputs accordingly. It also explains the use of else if to chain multiple conditions and switch-case as an alternative to if-else for multi-way decisions. Key points covered include the matching of else with the closest if, using break to break out of code blocks, and that switch expressions can only be of type int.

Uploaded by

Aditya Tiwari
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

More about conditionals

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 ((a != 0) && (b != 0))


OUTPUT for a = 5, b = 0
if (a * b >= 0)
NO OUTPUT!!
OUTPUT for a = 5, b = -5 printf (“positive”);
negative else
printf(“negative”);
8/18/19 Esc101, Programming 8
Unmatched if and else
• An else always matches closest unmatched if
– Unless forced otherwise using { … }

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

General form of if-else-if-else…


} else { else if (cond2)
if (cond2) { stmt-block2
stmt2 else if (cond3)
} else { stmt-block3
if (cond3) { else if (cond4)
…. stmt-block4
} else if …
} else
} last-block-of-stmt
8/18/19 Esc101, Programming 12
Example

• Given an integer , where , print the name of


the weekday corresponding to .
1: Sunday
2: Monday

7: Saturday

8/18/19 Esc101, Programming 13


Printing the day

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

• Given an integer , where , print Weekday, if


the corresponds to weekday, print Weekend
otherwise.
1, 7: Weekend
2,3,4,5,6: Weekday

8/18/19 Esc101, Programming 15


Weekday - version 1

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);
}

8/18/19 Esc101, Programming 18


Switch-case statement
• Multi-way decision
• Checks whether an
expression matches one Oh man!
Missed
out of a number of Today is Esc101
constant integer values Friday major quiz!

• Execution branches based


on the match found

8/18/19 Esc101, Programming 19


Printing the day, version 2

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

b) Run time crash

c) I don’t know / I don’t care


The correct answer is
d) 0

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

!( (2>5) && (3/0) ) || (4/0)


Evaluates to 1
8/18/19 Esc101, Programming 28
3 Factors for Expr Evaluation
• Precedence
– Applied to two different class of operators
– + and *, - and *, && and ||, + and &&, …
• Associativity
– Applied to operators of same class
– * and *, + and -, * and /, …
• Order of evaluation
– Precedence and associativity identify the operands for
each operator (Parenthesization)
– Not which operand/expr is evaluated first
• In C, order of evaluation of operands is defined
only for && and || 29
Next class
• Loops (6.3-6.5)
– While statement
– Do-while statement
– For statement

You might also like