0% found this document useful (0 votes)
11 views17 pages

Lecture 4

Uploaded by

zaidkhattab1012
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)
11 views17 pages

Lecture 4

Uploaded by

zaidkhattab1012
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

(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)?

#include <stdio.h> 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;
} How can we calculate the 200th triangular number?
Looping
• while-loop
• Syntax:

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>

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
The Greatest Common Divisor (GCD)
#include <stdio.h>
int main (void)
{
int u, v, temp;
printf ("Please type in two nonnegative integers.\n");
scanf ("%i%i", &u, &v);
while ( v != 0 )
Please type in two nonnegative integers.
{
150 35
temp = u % v;
u = v;
Their greatest common divisor is 5
v = temp;
}
printf ("Their greatest common divisor is %i\n", u);
return 0;
}

You might also like