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

PF-Week 04

programming fundamentals

Uploaded by

lachman das
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

PF-Week 04

programming fundamentals

Uploaded by

lachman das
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

Programming

Fundamental

Week 04

Sobia Iftikhar
[email protected]
Class 10
Example

– How to perform logical operation on two different variables.


Conditional Operator

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

– // printf("Enter phy subjects marks: "); – // (per >= 90)?


– // printf("//////////// Grade A ////////////\n"):
– // scanf("%d", &phy);
– // (per >= 80)?
– // printf("Enter chem subjects marks: ");
– // printf("//////////// Grade B ////////////\n"):
– // scanf("%d", &chem);
– // (per >= 70)?
– // printf("Enter bio subjects marks: ");
– // printf("//////////// Grade C ////////////\n"):
– // scanf("%d", &bio);
– // (per >= 60)?
– // printf("Enter math subjects marks: "); – // printf("//////////// Grade D //////////// \n"):
– // scanf("%d", &math); – // (per >= 40)?
– // printf("Enter comp subjects marks: "); – // printf("//////////// Grade E //////////// \n"):
– // scanf("%d", &comp); – // printf("//////////// Grade F //////////// \n");
Prefix and Postfix Increment

– Prefix increment – Postfix increment


– c = 5; – c = 5;
– x = ++c; /* value of c is 6 and x – x = c++;
– is 6 */ – /* value of c is 6 but x is 5 */
Example

 x=4 X = ? and y =? Reason


 y = ++x
 PRINT x
 PRINT y
 x=3 X = ? and y =? Reason
 y = x++
 PRINT x
 PRINT y
 x=3 X = ? and y =? Reason
 y = --x
 PRINT x
 PRINT y
 x=3 X = ? and y =? Reason
 y = x--
 PRINT x
 PRINT y
Bitwise operators

Operators Meaning of operators

& Bitwise AND

| Bitwise OR

^ Bitwise XOR

~ Bitwise complement

<< Shift left

>> Shift right


& -AND Operator

a b a&b a|b ~b a^b


0 0 0 0 1 0
0 1 0 1 0 1
1 0 0 1 1 1
1 1 1 1 0 0
& -AND Operator

a b a&b a|b ~b a^b


0 0 0 0 1 0
0 1 0 1 0 1
1 0 0 1 1 1
1 1 1 1 0 0
& -AND Operator

a b a&b a|b ~b a^b


0 0 0 0 1 0
0 1 0 1 0 1
1 0 0 1 1 1
1 1 1 1 0 0
Shift Operator- left and Right

–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

Introduction to conditional statements


If structure
If –else structure
If-else-if structure
Switch statements
If statement
The syntax of the if statement in C programming is:

The if statement
evaluates the test
expression inside
the parenthesis ().

If the test If the test


expression is expression is
evaluated to evaluated to
false, true,
statements statements
inside the inside the
body of if are body of if are
not executed. executed.
Example
Example 1: if statement

#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);

// true if number is less than 0


if (number < 0) {
printf("You entered %d.\n", number); Enter an integer: 5
} The if statement is easy.
printf("The if statement is easy.");

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);

// True if the remainder is 0


if (number%2 == 0) { Enter an integer: 7
printf("%d is an even integer.",number); 7 is an odd integer.
}
else {
printf("%d is an odd integer.",number);
}
return 0;
}
If else-if

The if...else statement executes two different


codes depending upon whether the test
expression is true or false. Sometimes, a choice
has to be made from more than 2 possibilities.

The if...else ladder allows you to check between


multiple test expressions and execute different
statements.
Flowchart
#include <stdio.h>
int main() {
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);

//checks if the two integers are equal.


if(number1 == number2) {
printf("Result: %d = %d",number1,number2);
}

//checks if number1 is greater than number2. Example


else if (number1 > number2) {
printf("Result: %d > %d", number1, number2); Enter two integers: 12 23
}
Result: 12 < 23
//checks if both test expressions are false
else {
printf("Result: %d < %d",number1, number2);
}

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.

If there is a match, the corresponding statements after the matching label


are executed. For example, if the value of the expression is equal to
constant2, statements after case constant2: are executed until break is
encountered.
If there is no match, the default statements are executed.
Flowchart
switch(operator)
{
case '+':
printf("%.1lf + %.1lf = %.1lf",n1, n2, n1+n2);
break;

case '-': Example: Simple


printf("%.1lf - %.1lf = %.1lf",n1, n2, n1-n2);
break; Calculator
#include <stdio.h>
case '*':
int main() {
printf("%.1lf * %.1lf = %.1lf",n1, n2, n1*n2);
char operator;
break;
double n1, n2;
case '/':
printf("Enter an operator (+, -, *, /): ");
printf("%.1lf / %.1lf = %.1lf",n1, n2, n1/n2);
scanf("%c", &operator);
break;
printf("Enter two operands: ");
scanf("%lf %lf",&n1, &n2);
// operator doesn't match any case constant +, -, *, /
default:
printf("Error! operator is not correct");
}

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

#include <stdio.h> #include <stdio.h>


int main() int main()
{ {
int a = 10; int a =
int b = 20; 10;
switch (a == 10 && b > 30) switch (a == 10)
{ {
case 1: case 1:
printf("CaseD "); printf("CaseD ");
break; break;
case 0: case 0:
printf("CaseB"); printf("CaseB");
break; break;
default: default:
printf("Default "); printf("Default ");
} return 0;} } return 0;}
Important

– 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?

Case2 Case3 Case4 Default


switch Versus if-else Ladder

– 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

You might also like