PF-Week 04
PF-Week 04
Fundamental
Week 04
Sobia Iftikhar
[email protected]
Class 10
Example
(condition)?
Printf():
Printf();
Example
– Int feedbackRate
– Int ExperienceRequried = 5;
– (we>=ExperienceRequried && feedbackRate == feedbackRate )?
– printf("Congratulation you get promotion"):
– printf(“sorry not eligible "):
Example
– Write a C program to print a grade sheet. For that program must take marks as
input of six subjects from user at run time and find percentage and grade.
Percentage must be shown with two value after point.
Solution
– // int phy, chem, bio, math, comp; – // per = (phy + chem + bio + math + comp) / 5.0;
– // float per; – // printf("Percentage of marks = %.2f\n", per);
| Bitwise OR
^ Bitwise XOR
~ Bitwise complement
–There
: are two shift operators in C programming – Left shift operator.
• Right shift operator
Example
– Two inputs from user a character data type and perform shift left
and right operator.
Operators Precedence in C
Example
Decision Concept
The if statement
evaluates the test
expression inside
the parenthesis ().
#include <stdio.h>
int main() { Enter an integer: -2
int number; You entered -2.
The if statement is easy.
printf("Enter an integer: ");
scanf("%d", &number);
return 0;
}
– Program to find the largest number of the three.
int main()
{
int a, b, c;
printf("Enter three numbers?");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
{
printf("%d is largest",a);
}
if(b>a && b > c)
Example
{
printf("%d is largest",b);
}
if(c>a && c>b)
{
printf("%d is largest",c);
}
if(a == b && a == c)
{
printf("All are equal");
}
}
C if...else Statement
H
staeo
w
m
e
n
tsif
i.
stae
n
.
sim
eIf.
d
t
en
e
tsh
ietsl
h
ex
senpbsi
resie
d
o
eo
d
ytn
iss
h
o
felsarevalt
u
skib
atea
o
p
t
d
d
p
eye
t
o
d
m
fiareo
frexce
tr
u
o
n
teu
m
e,
t
d
exc.
u
tw
io
o
r
n
.k
s
?
I
f
t
h
e
t
e
s
t
e
x
p
r
e
s
s
i
o
n
i
s
e
v
a
l
u
a
t
e
d
t
o
f
a
l
s
e
,
Work flow
Example
Example 2: if...else
statement
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
return 0;
}
– #include <stdio.h>
– int main () {
– int a = 100;
– if( a == 10 ) {
– printf("Value of a is 10\n" );
– } else if( a == 20 ) {
– printf("Value of a is 20\n" ); Example
– } else if( a == 30 ) {
– printf("Value of a is 30\n" );
– } else {
– printf("None of the values is matching\n" );
– }
– printf("Exact value of a is: %d\n", a );
– return 0;
– }
Difference between (if vs else
if)
Int num = 40;
//this first condition is checked, evaluates to `false`, the next condition will be checked
if(num < 40){
//do something
}
//the previous condition was evaluated to `false` so the following condition will be checked, and it
evaluates to `true` so code inside this `else if` block will execute
else if(num >= 10 && num <= 50) {
//do something else
}
//the following condition will not be checked as the previous condition evaluated to `true`
else if(num > 50) {
//do a thing
}
Difference between (if vs else
if)
let num = 40;
//the following condition evaluates to `false`, the next condition will be checked
if(num < 10){
//do something
}
//the following condition is the only on that evaluates to `true`, code inside this `if` block will execute
if(num >= 10 && num <= 50) {
printf('test');
}
//this condition will still be checked even though the previous condition was evaluated to `true`
if(num > 50) {
//do a thing
}
Switch statement
The expression is evaluated once and compared with the values of each
case label.
return 0;
}
Example
#include <stdio.h>
int main() {
int language = 10;
switch (language) {
case 1:
printf("C#\n");
break; Other programming language
case 2:
printf("C\n");
break;
case 3:
printf("C++\n");
break;
default:
printf("Other programming language\n");}}
Example
#include <stdio.h>
int main() {
int number=5;
switch (number) {
case 1:
case 2:
When working with switch case in C,
case 3:
you group multiple cases with unique
printf("One, Two, or Three.\n");
labels. You need to introduce a break
break;
statement in each case to branch at the
case 4:
end of a switch statement.
case 5:
case 6:
printf("Four, Five, or Six.\n");
break;
default:
printf("Greater than Six.\n");}}
Example
– 1) Case doesn’t always need to have order 1, 2, 3 and so on. They can have any
integer value after case keyword. Also, case doesn’t need to be in an ascending
order always, you can specify them in any order as per the need of the program.
– 2) You can also use characters in switch case
Why break;
Case 2
– Break statements are useful when you want your program-flow to come out of the
switch body. Whenever a break statement is encountered in the switch body, the
control comes out of the switch case statement.
– What if ? We forget to write “break” keyword?
Why break;
– Break statements are useful when you want your program-flow to come out of the
switch body. Whenever a break statement is encountered in the switch body, the
control comes out of the switch case statement.
– What if ? We forget to write “break” keyword?
– There are some things that you simply cannot do with a switch.
– A float expression cannot be tested using a switch
– The case keyword is followed by an integer or a character constant.
– Cases can never have variable expressions
(for example it is wrong to say case a +3 : )
The goto Keyword
int goals ;
printf ( "Enter the number of goals scored against India" ) ;
scanf ( "%d", &goals ) ;
if ( goals <= 5 )
goto sos ;
else
{
printf ( "About time soccer players learnt C\n" ) ;
printf ( "and said goodbye! adieu! to soccer" ) ;
exit( ) ; /* terminates program execution */
}
sos :
printf ( "To err is human!" ) ;
Drawback
What is the value of ‘i’ in this case? What’s getting added to j? Who
knows?
Task
There is IT company who started their startup few months ago, currently this organization
consist on two departments, 1) Software Department and 2) Development Department.
Initially this organization hired 6 six employs each employee has unique ID (ex: 11)
Three employees assign to each department on the basis of their skills.
Write a c program that will take input from a user as ID. And display the assigned
department. Using switch statement
Example
– Example 2.1: While purchasing certain items, a discount of 10% is offered if the
quantity purchased is more than 1000. If quantity and price per item are input
through the keyboard, write a program to calculate the total expenses.
Solution
Example
– Example 2.2: The current year and the year in which the employee joined the
organization are entered through the keyboard. If the number of years for
which the employee has served the organization is greater than 3 then a bonus
of Rs. 2500/- is given to the employee. If the years of service are not greater
than 3, then the program should do nothing
Solution
Write a program to calculate the salary as
per the following table:
Thankyou