Lecture4 Cs
Lecture4 Cs
Switch statement
Loops
The switch statement
• Handles complex conditional and branching operations
• Case Label must be unique
• Case Labels must end with 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.
The work diagram
Example with numerical cases
int main()
{
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)
{ Type in your expression.
case '+':
printf ("%.2f\n", value1 + value2); 178.99 - 326.8
break; -147.81
case '-':
printf ("%.2f\n", value1 - value2);
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;
}
#include<stdio.h>
int main() Example with characters as cases
{
char myinput;
Which option will you choose:
printf("Which option will you choose:\n"); a) Program 1
printf("a) Program 1 \n"); b) Program 2
printf("b) Program 2 \n");
scanf("%c", &myinput);
C
switch (myinput) Invalid choice
{
case 'a':
printf("Run program 1\n");
break;
case 'b':
{
printf("Run program 2\n");
printf("Please Wait\n");
break;
}
default: Can we
printf("Invalid choice\n");
break; have
} braces
return 0; within?
}
#include <stdio.h>
int main ()
{
What is the output here
char grade = 'B';
switch(grade) {
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
Well done
case 'C' :
Your grade is B
printf("Well done\n" );
break;
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 in the switch statement
switch (myinput)
#include<stdio.h>
{
case 'a':
int main() printf("Run program 1\n");
{ 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
} statements
within?
return 0;
}
Now lets learn how to deal with logical
operations
What do you think?
#include <stdio.h>
1
int main() 0
{ int i=10,j=20,k=30; 1
1
printf("%d\n",i==10); 0
}
Why do we need loops
The eighth triangular number is 36
initialization;
while(condition)
{
//Something to do
//incrementation;
}
The 200 th triangular number using while loop
#include <stdio.h>
int main() 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 TN to be chosen by the user?
Triangular number from user
#include <stdio.h>
int main()
{
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