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

C Programming: Operators Expressions, Control Statements: R. Jothi Scse Vit

This document provides an overview of various C programming concepts including data types, variables, operators, and control structures. It discusses the basic C variable types of char, int, float, and double. It describes how variables are declared and assigned values in C. It also covers C operators like arithmetic, relational, logical, and bitwise operators. Finally, it discusses common control structures in C like if-else statements, switch-case statements, and the goto statement.

Uploaded by

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

C Programming: Operators Expressions, Control Statements: R. Jothi Scse Vit

This document provides an overview of various C programming concepts including data types, variables, operators, and control structures. It discusses the basic C variable types of char, int, float, and double. It describes how variables are declared and assigned values in C. It also covers C operators like arithmetic, relational, logical, and bitwise operators. Finally, it discusses common control structures in C like if-else statements, switch-case statements, and the goto statement.

Uploaded by

sisil amose
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

C Programming : Operators

Expressions, Control
Statements

R. Jothi
SCSE VIT
Overview
• Data Types
• Variables and Constants
• Operators
• Control Structures
– If statement
– Break and Goto
– Switch case
• Programming Exercises
Basic C variable types
• There are four basic data types in C:
– char
• A single byte capable of holding one character in the
local character set.
– int
• An integer of unspecified size
– float
• Single-precision floating point
– double
• Double-precision floating point
Data types in C
//Demo for data types
void main()
{
int i1=23.6,i2=9;
float f1=12,f2=6.123456789;
double d1=12345678.9765,d2=123.45678;
char c1='a',c2='A';
clrscr();
printf("\n Integer values %d %d",i1,i2);
printf("\n Floating values %f %f",f1,f2);
printf("\n Double values %lf %lf",d1,d2);
printf("\n Character values %c %c",c1,c2);
printf("\n Integer equivalent of Character values %d %d",c1,c2);
getch();
}
Variables in Programming
• Represent storage units in a program
• Used to store/retrieve data over life of program
• Type of variable determines what can be placed in
the storage unit
• Assignment – process of placing a particular value in
a variable
• Variables must be declared before they are assigned
• The value of a variable can change; A constant always
has the same value
C Variable Names
• Variable names in C may only consist of
letters, digits, and underscores and may not
begin with a digit
• Variable names in C are case sensitive
• ANSI standard requires only 31 or fewer
characters.
• Should be very descriptive – readability
• Good: customer_name, ss_number;
• Bad : cn, ss;
Variable Declaration
• All variables must be declared in a C program
before the first executable statement! Examples:

void main()
{
int j=10;
float d;
/* Do something here */
}
int j=10;

&j

scanf(“%d”,&j)
Variable assignment
• After variables are declared, they must (should) be
given values. This is called assignment and it is done
with the ‘=‘ operator. Examples:

float a, b;
int c;
int x=-6,y=5;
b = 2.12;
c = 200;

Unassigned variables have garbage values!


Area of a Triangle-1
/* This program computes area of a triangle */

#include <stdio.h>
void main()
{
float height, length, area;
clrscr();
//read input height and length of the triangle
printf("Enter height and length of the triangle\n");
scanf("%f %f", &height, &length);
area=0.5 * height*length;
printf("Area of the triangle is %f", area);
getch();
}
Area of a Triangle -2
/* This program computes area of a triangle given 3 sides */

#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
float a, b,c, area;
float s;
clrscr();
//read input 3 sides
printf("Enter three sides of the triangle\n");
scanf("%f %f %f", &a, &b,&c);
s=(a+b+c)/2.0;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("Area of the triangle is %f",area);
getch();
}
Constants
• Constants refer to fixed values that the program may
not alter during its execution. These fixed values are
also called literals.
• Defining Constants
– Using #define preprocessor
#define MAX_LENGTH 100
#define PIE 3.14
– Using const keyword
const int MAX_LENGTH=100;
const float PIE=3.14;
It is a good programming practice to define
constants in CAPITALS
Area of a Circle
/* This program computes area of a circle */
#include <stdio.h>
#define PIE 3.14
void main()
{
float radius,area;
clrscr();
//read radius of circle
printf("Enter radius of circle\n");
scanf("%f", &radius);
area=PIE*radius*radius;
printf("Area of the circle is %f", area);
getch();
}
Operators
• Expression
c = a + b;
a,b,c are operands; = and + are operators
• Types of operators
– Arithmetic
– Assignment
– Logical
– Relational
– Bit-wise
Arithmetic Operators
Operator Description Example
A=10;
B=20;
+ Adds two operands. A + B = 30
− Subtracts second operand from the first. A − B = -10
* Multiplies both operands. A * B = 200
/ Divides numerator by de-numerator. B/A=2
% Modulus Operator and remainder of after B%A=0
an integer division. 66 % 4 =2
31%10=1
++ Increment operator increases the integer A++ = 11
value by one.
-- Decrement operator decreases the integer B-- = 19
value by one.
Relational Operators
Operator Description Example
A=10, B=20
== Checks if the values of two operands are equal or (A == B) is not true.
not. If yes, then the condition becomes true.
!= Checks if the values of two operands are equal or (A != B) is true.
not. If the values are not equal, then the condition
becomes true.
> Checks if the value of left operand is greater than (A > B) is not true.
the value of right operand. If yes, then the
condition becomes true.
< Checks if the value of left operand is less than the (A < B) is true.
value of right operand. If yes, then the condition
becomes true.
>= Checks if the value of left operand is greater than (A >= B) is not true.
or equal to the value of right operand. If yes, then
the condition becomes true.
<= Checks if the value of left operand is less than or (A <= B) is true.
equal to the value of right operand. If yes, then
the condition becomes true.
Logical Operators
• There are three kinds of logical operators.
– &&: and
– ||: or
– !: not
• Logical expression is an expression which uses
one or more logical operators, e.g.,
– (temperature > 90.0 && humidity > 0.90)
– !(n <= 0 || n >= 100).
The Truth Table of Logical Operators
Op 1 Op 2 Op 1 && Op2 Op 1 || Op2
1 1 1 1
1 0 0 1
0 1 0 1
0 0 0 0

Op 1 ! Op 1
1 0
0 1
Here 1 is any non zero value
Post and Pre Increment
/* Post and Pre Increment Operator */
void main()
{
int a=10;
clrscr();
printf("Beginning a = %d",a);
printf("\na++ = %d",a++);
printf("\na = %d",a);
++a;
printf("\n++a = %d",a);
printf("\n a++= %d a= %d ++a= %d",a++,a,++a);
getch();
}
Post and Pre Decrement
/* Post and Pre Decrement Operator */
void main()
{
int b=20;
clrscr();
printf("\nBeginning b = %d",b);
printf("\nb-- = %d",b--);
printf("\nb = %d",b);
--b;
printf("\n--b = %d",b);
printf("\nb--= %d b= %d --b= %d",b--,b,--b);
getch();

}
Operator Precedence
• An operator’s Operator Precedence
precedence determines
function calls highest
its order of evaluation.
Unary
• Unary operator is an
operator that has only * / %
one operand. + -
– !, +(plus sign), -(minus
< <= >= >
sign), and &(address of)
– They are evaluated == !=
second only after function &&
calls.
||
= lowest
Control Structures in C
• If – to make decision
• Goto – to go to specific line number
• Break – to break current statement block;
• Switch-case- to compare a variable to several
"integral" values
If statement
if( your_grade > 60 )
printf(“You are passed!\n”);
printf(“End”);

Output
You are passed!
End

if( your_grade > 60 )


{
printf(“You are passed!\n”);
printf(“You will be promoted!\n”);
}
printf(“End”);

Output
You are passed!
You will be promoted!
End
Greatest of two numbers
void main()
{ enter two values 3 5
int a,b; b is greater
clrscr();
printf("\n enter two values");
scanf("%d %d", &a,&b);
if(a>b)
printf(“\n a is greater");
else
printf(“\n b is greater");
getch();
}
If Else statement
if( your_grade > 60 )
printf(“You have passed!\n”);
else
printf”(“Sorry, You have not passed!\n”);

printf(“End”);

Output (for your_grade =70)


You have passed!
End

Output (for your_grade =50)


Sorry, You havenot passed!
End
Nested If statement
Nested If statement

if(mark>=80)
printf("\n\n Your Grade : A+");
else if(mark>=75)
printf("\n\n Your Grade : A");
else if(mark>=60)
printf("\n\n Your Grade : B"); else
if(mark>=45)
printf("\n\n Your Grade : C");
else if(mark>=35)
printf("\n\n Your grade : D"); else printf("\n\n
You Are Fail");
Goto
#include <conio.h>
void main()
{
int i=1;
clrscr();
printf("Your count down starts");
lineA: printf(" %d \n",i);
getch();
i++;
if(i<=10)
goto lineA;
else
printf(" \n Hero Revenge begins.......");
getch();
}
Switch Case : A switch statement allows a variable to be tested
for equality against a list of values. Each value is called a case, and the
variable being switched on is checked for each switch case.
#include <stdio.h>
void main ()
{ U have typed two
int choice; Your choice is 2
printf(“Enter choice”);
scanf(“%d”,&choice);
switch(choice)
{
case 1 : printf(“U have typed one\n" );
break;
case 2 : printf(“U have typed two\n”);
break;
case 3 : printf(“U have typed three\n" );
break;
default : printf(“U have typed Invalid choice\n" );
}
printf("Your choice is %d\n", choice );
}
#include <stdio.h>
void main ()
{ Well done
char grade = 'B'; Your grade is B
switch(grade)
{
case 'A' : printf("Excellent!\n" );
break;
case 'B' :
case 'C' : 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 );
}
Swapping two values
/* This program swaps 2 values using 3rd intermediate
varaible*/

#include <stdio.h>
#include <conio.h>
void main()
{
int a=3, b=5, c;
clrscr();
printf(“Before Swap a = %d, b = %d”, a,b);
c=a;
a=b;
b=c;
printf(“After Swap a = %d, b = %d”, a,b);
getch();
}
Remember

Always start your program this way


#include <stdio.h>
void main()
{
// variable declaration
clrscr(); // to clear prev o/p from screen
// actual computation
getch()
}

You might also like