Module 1
Module 1
Module 1
main( )
{
int num ;
printf ( "Enter a number " ) ;
scanf ( "%d", &num ) ;
if ( num > 0 ) /*outer if*/
printf ( “ You entered a positive number" ) ;
else
{
if(num == 0) /*inner if*/
printf ( “ You entered zero" ) ;
else
printf ( “ You entered a negative number" ) ;
}
}
Nested if Statement
It contains multiple if else condition. It is used to check the
multiple conditions. This statement is like executing an if
statement inside an else statement.
Syntax
if(Boolean_expression1) {
//Body of if statement
if(Boolean_expression2)
{ //Body of nested if
}
}…
else
{ //Body of else statement
}
/* Demonstration of Nested if statement */
if ( i< 30 )
{ printf ("Value of i is less than 30");
if ( j == 10 )
{ printf ("Value of j is equal to 10");
}
}else
{ printf ("Value of i is not less than 30");
}
getch();
}
switch Statement
• It contains a number of cases with different conditions.
When a variable value is matched with the case, then that
case is executed.
Syntax
switch(expression)
{ case value1 : //Statements
break;
case value2 : //Statements
break;
casevaluen : //Statements
break;
default : //Optional
//Statements
}
/* Demonstration of Switch statement */
The first two operators (&& and ||) allow two or more
conditions to be combined in an if statement.
Operator Meaning of Operator Example
If c = 5 and d = 2 then,
Logial AND : True only if
&& expression ((c == 5) &&
all operands are true
(d > 5)) equals to 0.
Logical OR : True only if If c = 5 and d = 2 then,
|| either one operand is expression ((c == 5) || (d
true > 5)) equals to 1.
Operator precedence
! High
&& Medium
|| Low
Conditional Operators
A conditional operator is a ternary operator, that is, it
works on 3 operands.
It used in decision making process.
In C Programming, Conditional Operator returns the
statement depends upon the given expression result.
Syntax
ConditionalExpression ? Expression 1 : Expression 2
This means “if conditional expression is true, then the
value returned will be expression 1, otherwise the value
returned will be expression 2”.
Conditional Operator : Example
int x, y ;
scanf ( "%d", &x ) ;
y=(x>5?3:4);
This statement will store 3 in y if x is greater than 5,
otherwise it will store 4 in y.
The equivalent if statement will be,
if ( x > 5 )
y=3;
else
y=4;
Looping Control Structures
Loop is used to repeat a block of code until the specified condition is
met.
It allows to execute a statement or group of statements multiple
times.
# include <stdio.h>
int main()
{ int x;
x = 1;
while (x <= 10)
{
printf(“%d \n”, x);
x = x + 1;
}
return 0;
}
for Loop
Syntax
for(initialization; condition; increment/decrement)
{
statement – block;
}
The for loop is executed as :
1. It first evaluates the initialization code.
2. Then it checks the condition expression
3. If it is true, it executes the for-loop body.
4. Then it evaluate the increment/decrement condition and again
follows from step 2.
5. When the condition is false, it exits the loop.
Example – Program to print even numbers from 10
to 50
# include <stdio.h>
int main()
{ int x;
for(x = 10; x <=50; x = x +2)
{
printf(“%d \n”, x);
}
return 0;
}
do - while Loop
It is similar to while statement, except that it test the
condition at the end of the loop body. So, it is called exit
control loop.
The body of the loop is executed at least once, then the test
expression is evaluated.
Syntax
do
{
statements;
} while (expression);
Do - while Loop …..
} while( Condition);
while loop can execute 0 to N times do-while loop executes 1 to N times.
Break & Continue Statement
• "break" statement is used inside loops to terminate a loop
and exit it (with a specific condition).
• In below example loop execution continues until
either num>=20 or entered score is negative.
while(num<20)
{
printf("Enter score: ");
scanf("%d",&scores[num]);
if(scores[num]<0)
break;
}
Break & Continue Statement ….
• Continue statement can be used in loops. Like break
command continue changes flow of a program.
• It does not terminate the loop however.
• It just skips the rest of current iteration of the loop and
returns to starting point of the loop.
Example:
while((ch=getchar())!='\n')
{
if(ch=='.')
continue;
putchar(ch);
}
Operators in C
• C language offers many types of operators.
They are,
1. Arithmetic operators
2. Assignment operators
3. Relational operators
4. Logical operators
5. Bit wise operators
6. Conditional operators (ternary operators)
7. Increment/decrement operators
8. Special operators
ARITHMETIC OPERATORS IN C:
Arithmetic Example
Operators/Operation
+ (Addition) A+B
– (Subtraction) A-B
* (multiplication) A*B
/ (Division) A/B
% (Modulus) A%B
EXAMPLE PROGRAM FOR C ARITHMETIC OPERATORS
int a=40, b=20, add, sub, mul, div, mod;
add = a+b;
sub = a-b;
mul = a*b;
div = a/b;
mod = a%b;
printf("Addition of a, b is : %d\n", add);
printf("Subtraction of a, b is : %d\n", sub);
printf("Multiplication of a, b is : %d\n", mul);
printf("Division of a, b is : %d\n", div);
printf("Modulus of a, b is : %d\n", mod);
ASSIGNMENT OPERATORS IN C
Operators Example/Description
= sum = 10; 10 is assigned to variable sum
Operators Example/Description
&& (logical (x>5)&&(y<5)
AND) It returns true when both conditions are true
|| (logical OR) (x>=10)||(y>=10)
It returns true when at-least one of the condition is true
! (logical NOT) !((x>5)&&(y<5))
It reverses the state of the operand “((x>5) && (y<5))”
If “((x>5) && (y<5))” is true, logical NOT operator makes
it false
BIT WISE OPERATORS IN C: