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

Lecture 4

The document discusses various C programming concepts including operators, control structures, and algorithms/flowcharts. Specifically, it covers: 1) Operators like increment/decrement, conditional, bitwise, and special operators. 2) Control structures like if-else statements, nested if-else, and chaining of if-else statements to control program flow. 3) Algorithms and flowcharts to represent the steps to solve problems through graphical representations with symbols. Exercises are provided to write programs to calculate average marks, solve quadratic equations, and more.

Uploaded by

Manaswini Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Lecture 4

The document discusses various C programming concepts including operators, control structures, and algorithms/flowcharts. Specifically, it covers: 1) Operators like increment/decrement, conditional, bitwise, and special operators. 2) Control structures like if-else statements, nested if-else, and chaining of if-else statements to control program flow. 3) Algorithms and flowcharts to represent the steps to solve problems through graphical representations with symbols. Exercises are provided to write programs to calculate average marks, solve quadratic equations, and more.

Uploaded by

Manaswini Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 33

Today’s Agenda

Operators
– Increment and Decrement Operators
– Conditional Operator
– Bitwise Operators
– Special Operators
Control Structure
– if statement
Exercise
1. Write a program which takes marks of five
students as input and computes the
average marks. Print the average mark of
five students.

2. Write a program to compute roots of a


quadratic equation
ax2 + bx + c = 0
Exercise
3 . Write a program to solve two linear
equations
2x + 8y – 10 = 0
5x + 6y – 5 = 0
Increment and Decrement Operators

++ Adds one to the operand


-- Subtracts one from the operand
Both are unary operators

++i is equivalent to: i = i+1


--i is equivalent to: i = i-1

Is ++i and i++ are same??


What are Postfix and Prefix Expressions?

In postfix, the expression is evaluated first using the original


value of the variable and then the variable is incremented (or
decremented) by one.
Ex. m = n++; or m = n--;

In prefix, the variable is incremented (or decremented) first


and then the expression is evaluated using the new value of
the variable.
Ex. m = --n; or m = ++n;
Postfix and Prefix Examples

j =10; Output

i =j++; i = 10 j = 11

What is the values of i and j?

Output
j =10;
i=9 j=9
i=--j;
What is the values of i and j?
Conditional Operator

Also called Ternary operator


Syntax
Variable = expression1 ? expression2 :
expression3

Example: Output
a=5;
X = 10
b=10;
x =(a>b) ? a : b;
What will be the value of x??
Bitwise Operators
Operator Meaning

& Bitwise AND


| Bitwise OR
^ Bitwise exclusive OR
<< Shift left
>> Shift right
Manipulation of data at bit level
Not be applied to float or double values
Special Operators
The Sizeof Operator
– Returns the number of bytes the operand
occupies
Examples:
– p=sizeof(value);
– q=sizeof(float);
– r=sizeof(long int);
Comma Operator
Used to link the related expressions together.
Evaluation starts from left to right.
And the value of the right most expression is
the value of the combined expression.
Example:
1. value = (a = 6, b = 7, c = 8, a+b+c);
Value contains 21
2. scanf (“%f”, &miles);
Operators Associativity Rank
(),[] L to R 1
+, -, ++, --, !, ~, *, &, sizeof, R to L 2
(type)
*, /, % L to R 3
Binary plus(+) and Binary minus (-) L to R 4
<<, >> L to R 5
<, <=, >, >= L to R 6
==, != L to R 7
& L to R 8
^ L to R 9
| L to R 10
&& L to R 11
|| L to R 12
?: R to L 13
=, *=, /=, %=, +=, -=, &=, ^= R to L 14
, L to R 15
ALGORITHMS AND FLOW CHARTS
Algorithms
– An Algorithm is just a detailed sequence of simple steps
that are needed to solve a problem

Flow Charts
– Flow Charts are maps or graphical representations of a
process.
– Steps in a process are shown with symbolic shapes, and
the flow of the process is indicated with arrows
connecting the symbols
Symbols in Flowchart

START , END

INPUT

PROCESS INPUT DATA

DECISION

CONNECTOR

CONNECTOR
Example Flow Chart
START Flow chart to find
whether the the marks of
Marks = 50
a student is above avg or
Avg = 45 below avg

NO
Marks > avg Print below Avg

YES

Print above Avg

END
Example on flowchart

Draw a flow chart for calculating the gross


salary of a employee if his basic salary is
input through the keyboard. if basic salary is
less than Rs.1500 then HRA = Rs. 500 and
DA = 90% of basic. If salary is equal or
greater than 1500 then HRA = 10% and DA
= 95% of basic salary.
START

INPUT bs

YES is NO
bs < 1500

Hra = 500 Hra = bs * 10/100


Da = bs * 90/100 Da = bs * 95/100

Gs = bs + hra + da

PRINT STOP
GS
Control Structures
It controls the flow of execution in a
program.
It enables you to combine individual
instructions into a single logical unit with
one entry and one exit point.
Control Structure
Sequential Control Structure
– It’s a sequential flow of execution of
instructions.
Selection Control Structure
– This control structure chooses among
alternative program statements.
Repetition Control Structure
– This control structure repeats a group of
program statements till it satisfies some
condition.
if Statement

Selection Control Structure


If Statement

F
if (condition) condition
{
T
statement-block;
} Statement -block
next-statement;
Similar to BR instruction in LC-3

Condition is a C expression, which evaluates to


TRUE (non-zero) or FALSE (zero).
Statement-Block can be a single statement
Example If Statements
if (x <= 10)
y = x * x + 5;

if (x <= 10) {
compound statement;
y = x * x + 5;
both executed if x <= 10
z = (2 * y) / 3;
}

only first statement is conditional


if (x <= 10)
second statement is
y = x * x + 5; always executed
z = (2 * y) / 3;
More If Examples
if (age >= 0 && age <= 11)
kids += 1;
if (month == 4 || month == 6 ||month == 9 ||
month == 11)
printf(“The month has 30 days.\n”);

if (x = 2)
y = 5; always true,
so y = 5 is always executed!

This is a common programming error (= instead of ==),


not caught by compiler because it’s syntactically
correct.
If - else Statement
if (condition)
true block statement(s); T F
condition
else
false block statement(s);
True Block False Block
statement statement

else allows choice between two mutually


exclusive actions without re-testing condition.
If – else Statement example
if (i>j)
k = i++ + --j;
else if (i>j && j>k)
k = ++i + j--; { i++;
j++;
}
else
{ k++;
j--;
}
Chaining if’s and else’s
if(condition 1)
statement-1;
else if(condition 2)
statement-2;
else if(condition 3)
statement-3;
else if(condition 4)
statement-4;
else
default-statement;
next_statement;
Example
if (month == 4 || month == 6 || month == 9 || month == 11)
printf(“Month has 30 days.\n”);
else if (month == 1 || month == 3 ||
month == 5 || month == 7 ||
month == 8 || month == 10 ||
month == 12)
printf(“Month has 31 days.\n”);
else if (month == 2)
printf(“Month has 28 or 29 days.\n”);
else
printf(“Don’t know that month.\n”);
Program using if-else ladder
/* Checking a character whether it is capital letter or small letter
or special symbol*/
#include<stdio.h>
int main()
{
char ch;
printf("Enter a char:");
scanf("%c",&ch);
if(ch>='A' && ch<='Z')
printf("\n Capital letter");
else if(ch>='a' && ch<='z')
printf("\n Small letter");
else if(ch>='0' && ch<='9')
printf("\n Digit");
else
printf("\n Special Symbol");
return 0;
}
if (condition 1)
{
if (condition 2)
{
statement(s) 1;
Nesting of
} if –else statements
else
{
statement(s) 2;
}
}
else
{
statement(s) 3;
}
next_statement;
Nested if examples
if (x == 3)
if (y != 6) {
z = z + 1;
w = w + 2;
}

is the same as...


if ((x == 3) && (y != 6)) {
z = z + 1;
w = w + 2;
}
Matching else with If
else is always associated with closest
unassociated if.
if (x != 10)
if (y > 3)
z = z / 2;
else
z = z * 2;
is the same as... is NOT the same as...
if (x != 10) { if (x != 10) {
if (y > 3) if (y > 3)
z = z / 2; z = z / 2;
else }
z = z * 2; else
} z = z * 2;
int main() {
float a, b, c;
printf(“Enter three values\n”);
scanf(“%f %f %f”,&a, &b, &c);
printf(“Largest value is”);
if(a>b)
{ if(a>c)
printf(“%f”,a);
else Example: nesting of if-else
printf(“%f”,c);
program to find largest of
}
else the three numbers
{ if(c>b)
printf(“%f”,c);
else
printf(“%f”,b);
}
return 0;
}
Exercise: on if-else
1. Write a program to determine whether a number is odd or
even and print the appropriate message.

2. Write a program to find the smallest of the three numbers.

3. Write a program to determine whether the inputted year is


a leap year or not.
Exercise: on if-else
4. Write a program that takes the x-y
coordinates of a point in the Cartesian
plane and prints e message telling either
an axis on which the point lies or the
quadrant in which it is found.

You might also like