0% found this document useful (0 votes)
86 views41 pages

Operators and Control Statement

This document provides an overview of operators and control statements in C programming. It discusses various arithmetic, relational, and logical operators. It also covers the different decision making control statements like if, if-else, and nested if statements. Examples are given to illustrate how to use these operators and control statements to write basic C programs that perform calculations, comparisons, and conditional execution of code. Exercises are included at the end to help practice applying these concepts.

Uploaded by

Salim Isyaku
Copyright
© © All Rights Reserved
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)
86 views41 pages

Operators and Control Statement

This document provides an overview of operators and control statements in C programming. It discusses various arithmetic, relational, and logical operators. It also covers the different decision making control statements like if, if-else, and nested if statements. Examples are given to illustrate how to use these operators and control statements to write basic C programs that perform calculations, comparisons, and conditional execution of code. Exercises are included at the end to help practice applying these concepts.

Uploaded by

Salim Isyaku
Copyright
© © All Rights Reserved
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/ 41

1

LECTURE 2

OPERATORS AND CONTROL STATEMENT


compilation
2

 C is a high-level language.

 Writing a C code. {editors like gedit, vi}

 Compiling a C code. {gcc –c test.c –o test}

 Executing the object code. {./test}


Some more basics
3

 Keywords
 char, static, if , while, return ..................... Total= about 32

 Data Types
 int , char, float,
 Double, char
 Arithmetic Operators
 + (Plus), - (Minus), *
(Multiplication),
/(Division),(Modulus) %
My first C program!
4

#include <stdio.h>
// program prints hello world
int main() {
printf ("Hello world!");
return 0;
}

Output: Hello world!


Example 1
5

#include <stdio.h>
// program prints a number of type int
int main() {
int number = 4;
printf (“Number is:”, number);
return 0;
}

Output: Number is 4
Example 2
6

#include <stdio.h>
// program reads and prints the same thing
int main() {
int number ;
printf (“ Enter a Number: ”);
scanf (“%d”, &number);
printf (“Number is %d\n”, number);
return 0;
}

Output : Enter a number: 4


Number is 4
more and more
7

#include <stdio.h>
int main() {
int a = 4; //first number
int b = 5; //second number
int sum;
Scanf(“%d”,&a);
Scanf(“%d”,&b);
sum= a + b;
Printf(“the sum=%d”,sum);
}
Assignment Operator
8
Note
9

Errors
Compilation
Compiler generally gives the line number at
which the error is present.
Run time
C programs are sequential making the
debugging easier.
Some more Data Types
10

 Primary : int, float, char


 int (signed/unsigned)(2,4Bytes): used to store integers.
 char (signed/unsigned)(1Byte): used to store characters

 float, double(4,8Bytes): used to store a decimal number.

 User Defined:
 typedef: used to rename a data type
 typedef int integer; can use integer to declare an int.
 enum, struct, union
Some more Arithmetic Operators
11
Some more Arithmetic Operators
12

 Prefix Increment : ++a


 example:
 int a=5;
 b=++a; // value of b=6; a=6;

 Postfix Increment: a++


 example
 int a=5;
 b=a++; //value of b=5; a=6;
Example 1
13

int main( )
{

int x=10;

printf(x++)=? What will be printed here?

printf(x)=? What will be printed here?

}
Example 2
14

int main( )
{

int x=10;

printf(++x)=? What will be printed here?

printf(x)=? What will be printed here?

}
Contd…
15

 Modulus (remainder): %
 example:
 12%5 = 2;

 Assignment by addition: +=
 example:
 int a=4;
 a+=1; //(means a=a+1) value of a becomes 5

Can use -, /, *, % also


Contd…
16

 Comparision Operators: <, > , <=, >= , !=, ==, !,


&&, || .
 example:
 int a=4, b=5;
 a<b returns a true(non zero number) value.
The Relational Operators, Assume variable A holds 10 and
variable B holds 20
The Logical Operators
The Assignment Operators
Operator Precedence
20

 Meaning of a + b * c ?
is it a+(b*c) or (a+b)*c ?
 All operators have precedence over each other
 *, / have more precedence over +, - .
 If both *, / are used, associativity comes into
picture. (more on this later)
 example :
 5+4*3 = 5+12= 17.
Precedence Table
21

Highest on top
++ -- (Postfix)
++ -- (Prefix)
* / %
+ -
<< >>

< >
&
|
&&
||
Input / Output
22

 printf (); //used to print to console(screen)


 scanf (); //used to take an input from
console(user).
 example:printf(“%c”, ’a’); scanf(“%d”, &a);
 More format specifiers
%c The character format specifier.
%d The integer format specifier.
%i The integer format specifier (same as %d).
%f The floating-point format specifier.
%s The string format specifier.
Some more geek stuff
23

 & in scanf.
 Itis used to access the address of the variable used.
 example:
 scanf(%d,&a);
 we are reading into the address of a.

 Data Hierarchy.
 example:
 int value can be assigned to float not vice-versa.
 Type casting
Typecasting
24

 Converting an expression of a given type into


another type is known as type-casting .
typecasting is more use in c language
programming.
Example
25

int main( )
{
int value1 = 10, value2 = 3;
float result;
result = (float)value1 / value2;
printf("Result with type casting: %f", result);
return 0;
}
Decision control Statement
26

 In decision control statements (if-else and


nested if), group of statements are executed
when condition is true. If condition is false, then
else part statements are executed.

 There are 3 types of decision making control


statements in C language. They are,
27

 if statements
 if else statements
 nested if statements
the if statement
28
Example
29

int main()
{
int m=40,
int n=40;
if (m == n)
{
printf("m and n are equal");
}
}
The if else statement
30
Example
31

#include <stdio.h>
int main()
{
int m=40,n=20;
if (m == n)
{
printf("m and n are equal");
}
else
{
printf("m and n are not equal");
}
}
The nested if
32
Example
33

#include <stdio.h>
int main()
{
int m=40,n=20;
if (m>n) {
printf("m is greater than n");
}
else if(m<n) {
printf("m is less than n");
}
else {
printf("m is equal to n");
}}
Exercise 1: what would be the output of the following
programs
34

(a) main( )
{
int x = 10, y = 20 ;
if ( x == y ) ;
printf ( "\n%d %d", x, y ) ;
}

(b) main( )
{
int x = 3, y = 5 ;
if ( x == 3 )
printf ( "\n%d", x ) ;
else ;
printf ( "\n%d", y ) ;
}
35

(c) main( )
{
int x = 3 ;
float y = 3.0 ;
if ( x == y )
printf ( "\nx and y are equal" ) ;
else
printf ( "\nx and y are not equal" ) ;
}
36

 (d)main( )
{
int i = 65 ;
char j = ‘A’ ;
if ( i == j )
printf ( “C is WOW” ) ;
else
printf( "C is a headache" ) ;
}
Exercise 2
37

 Any integer is input through the keyboard. Write


a program to find out whether it is an odd
number or even number.
Exercise 3
38

 Write a program in C that uses double variables


to compute the area of a circle. The user will
enter the radius and pi from the keyboard.
Exercise 4
39

 If the ages of Mr.X, Mr.Y and Mr.Z are input


through the keyboard, write a program to
determine the youngest of the three.
Exercise 5
40

 The marks obtained by a student in 5 different


subjects are input through the keyboard. The
student gets a
division as per the following rules:
Percentage above or equal to 60 - First division
Percentage between 50 and 59 - Second division
Percentage between 40 and 49 - Third division
Percentage less than 40 – Fail
 Note: use your know of if-else condition
41

QUESTIONS

You might also like