Professional Documents
Culture Documents
Comprog11 Oct. 18
Comprog11 Oct. 18
Comprog11 Oct. 18
CLASS AGENDA
• Attendance
• Midterm Departmental Exam
Wednesday, October 26, 2022 (Face-to-Face)
Prepare “Mongol 2” pencils.
Coverage: Module 2 (C Expressions & Selection Structures)
• Discussion: Selection Structures (Multiway)
• Seatwork
Nested if statement
Syntax:
3
Example
Write a program that
prompts the user to
input two numbers. It
will then determine if
the first number is less
than, equal to, or
greater than the
second number.
Program Tracing #1
What is the output of the code below?
6
Program Tracing #2
What is the output of the code below?
int i= 5, j=6;
if(i==6)
if(j==5)
printf("%d \n",i+j);
else
printf("%d \n",i-j);
else
if (j > 6)
printf("%d \n",i*j);
else
printf("%d \n",i+j*5); 7
Program Tracing #2
What is the output of the code below?
int i= 5, j=6;
if(i==6)
if(j==5)
printf("%d \n",i+j);
else ANSWER:
else
printf("%d \n",i-j);
35
if (j > 6)
printf("%d \n",i*j);
else
printf("%d \n",i+j*5);
8
if…else if…else statement
Syntax:
if (expression)
statement;
else if (expression)
statement;
else if (expression)
statement;
else
statement; 9
if…else if…else statement
10
switch statement
Syntax:
11
EXAMPLES
char letter='E';
int x=1;
switch(letter) {
case 'A':
switch(x) case 'a': printf("Letter A/a \n");
{ break;
case 0: printf("case 0 \n"); case 'E':
case 1: printf("case 1 \n"); case 'e': printf("Letter E/e \n");
case 2: printf("case 2 \n"); break;
default: printf("Another letter here \n");
} }
12
Program Tracing #1
What is the output of the code below?
int m, n, x = 12;
m = x % 2;
switch(m)
{
case 0 : n = m + x;
break;
case 1 : n = m + 2 * x;
break;
case 2 : n = m;
}
printf("%d", n); 13
Program Tracing #1
What is the output of the code below?
int m, n, x = 12;
m = x % 2;
switch(m)
{
case 0 : n = m + x; ANSWER:
break;
case 1 : n = m + 2 * x; 12
break;
case 2 : n = m;
}
printf("%d", n); 14
Program Tracing #2
What is the output of the code below?
int x=0;
switch(x)
{
case 1: printf ("One");
int x=0;
switch(x)
{
case 1: printf ("One");
ANSWER:
case 0: printf ("Zero");
ZeroHello World
case 2: printf ("Hello World");
}
16