Lecture 4
Lecture 4
(CS116)
Switch statement - continued
Loops (while)
The switch statement
• Handles complex conditional and branching operations
• Case Label must be unique
• Case Labels must end with a colon (:)
• Case labels must have constants / constant expression (Not
variables)
• Case label must be of integral Type ( Integer, Character)
• Case label should not be ‘floating point number ‘
• Switch case can optionally have at most one default label
• Two or more cases may share one break statement
• Nesting ( switch within switch ) is allowed.
Flow-Chart Representation of switch statement
Example with Numerical Cases
#include <stdio.h>
int main(void)
{
int num = 2;
Case1: Value is: 2
switch( (num + 2) % 3 )
{
case 1:
printf("Case1: Value is: %d", num);
break;
case 2:
printf("Case2: Value is: %d", num);
break;
case 3:
printf("Case3: Value is: %d", num);
break;
default:
printf("Default: Value is: %d", num);
}
return 0;
}
#include <stdio.h>
int main (void) Example with Symbol Cases
{
float value1, value2;
char operator;
printf ("Type in your expression:\n");
scanf ("%f %c %f", &value1, &operator, &value2);
switch (operator)
{
case '+': Type in your expression:
printf ("%.2f\n", value1 + value2);
break; 178.99 - 326.8
case '-':
printf ("%.2f\n", value1 - value2); -147.81
break;
case '*':
printf ("%.2f\n", value1 * value2);
break;
case '/':
if ( value2 == 0 )
printf ("Division by zero.\n");
else
printf ("%.2f\n", value1 / value2);
break;
default:
printf ("Unknown operator.\n");
break;
}
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
Example with Characters (char) as Cases
int main()
{
char myinput;
Which option will you choose:
printf("Which option will you choose:\n"); a) Program 1
printf("a) Program 1 \n");
printf("b) Program 2 \n"); b) Program 2
scanf("%c", &myinput);
switch (myinput) C
{
case 'a':
Invalid choice
printf("Run program 1\n");
break;
case 'b': // multiple lines, curly brackets are optional
{
printf("Run program 2\n");
Can we have braces
printf("Please Wait\n"); within?
break;
}
default:
printf("Invalid choice\n");
break;
}
return 0;
}
#include <stdio.h>
int main () What is The Output Here?
{
char grade = 'B';
switch(grade)
{
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
case 'C' : Well done
printf("Well done\n" );
break; Your grade is B
case 'D' :
printf("You passed\n" );
break;
case 'F' :
printf("Better try again\n" );
break;
default :
printf("Invalid grade\n" );
}
printf("Your grade is %c\n", grade );
return 0;
}
You can have if statement within the switch statement
switch (myinput)
#include<stdio.h>
{ // begin switch
case 'a’:
int main() printf("Run program 1\n");
{ // begin main if( i == 5)
char myinput; printf("OK\n");
int i = 5; break;
printf("Which option will you choose:\n"); case 'b':
printf("a) Program 1 \n"); {
printf("Run program 2\n");
printf("b) Program 2 \n");
printf("Please Wait\n");
scanf("%c", &myinput);
break;
}
default: Can we
printf("Invalid choice\n"); have
break; conditional
} // end switch statements
within?
return 0;
} // end main
Now let’s learn how to deal with logical
operations
What Do You Think?
#include <stdio.h>
int main() 1
{ int i = 10, j = 20, k = 30; 0
printf("%d\n", i == 10); 1
printf("%d\n", i >= 10 && j < 4); 1
printf("%d\n", i + k < j || 30 – i >= j); 0
printf("%d\n", !( j – 20 ));
printf("%d\n", !( k > j));
Remember
return 0; ==, >,<,||,&&,!
}
Why Do We Need Loops (Repetition)?
Variable initialization;
while (condition)
{
Something to do;
Variable update (increment/decrement);
}
The 200th triangular number using while loop
#include <stdio.h>
int main( void ) The 200th triangular number is 20100
{
int n = 1, triangularNumber=0;
while (n < 201)
{
triangularNumber = triangularNumber + n;
n++;
}
printf("The 200th triangular number is %i\n", triangularNumber);
return 0;
}
What if I want the triangularNumber to be chosen by the user?
Triangular Number from The User
#include <stdio.h>
int main( void )
{
int i = 1, sum = 0, limit;
printf(“Please enter the triangular number you want to calculate:\n");
scanf("%d", &limit);
while (i < (limit +1))
{
sum = sum + i;
i++;
}
printf("The %dth triangular number is %i\n", limit, sum);
return 0;
}
What is The Output Here?
#include <stdio.h>
return 0;
}
How to Find The Greatest Common Divisor
U V
• Step 1 150 35
• Step 2 35 10
• Step 3 10 5
• Step 4 5 0