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

Lecture4 Cs

Uploaded by

super gamer TV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lecture4 Cs

Uploaded by

super gamer TV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Computing Fundamentals

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

printf("%d\n",i>=10 && j<4);


printf("%d\n",i+k<j || 30-i >=j);
printf("%d\n",!(j-20));
printf("%d\n",!(k>j));
Remember
return 0; ==, >,<,||,&&,!

}
Why do we need loops
The eighth triangular number is 36

• int main (void)


{
int triangularNumber;
triangularNumber = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8;
printf ("The eighth triangular number is %i\n", triangularNumber);
return 0;
What happens if we need the 200th triangular
} number?
Looping
• While loop
• Syntax:

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>

int main () { value of a: 10


value of a: 11
value of a: 12
/* local variable definition */ value of a: 13
int a = 10; value of a: 14
value of a: 15
value of a: 16
/* while loop execution */ value of a: 17
while( a < 20 ) { value of a: 18
value of a: 19
printf("value of a: %d\n", a);
a++;
}

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

Remember: A slide with a question with


no code is the time to try it yourself
before you check the solution. You can
have a look at the solution only if you
are stuck and only have a look at the
part you are stuck in
Greatest common divisor
#include <stdio.h> Please type in two nonnegative integers.
150 35
int main (void)
Their greatest common divisor is 5
{
int u, v, temp;
printf ("Please type in two nonnegative integers.\n");
scanf ("%i%i", &u, &v);
while ( v != 0 )
{
temp = u % v;
u = v;
v = temp;
}
printf ("Their greatest common divisor is %i\n", u);
return 0;
}

You might also like