22. && - Logical AND
C allows usage of three logical operators, namely, &&, || and !
These are to be read as ‘AND’ ‘OR’ and ‘NOT’ respectively.
Don’t use the single symbol | and &. These single symbols also have a
meaning.
The first two operators, && and ||, allow two or more conditions to be
combined in an if statement.
Vinay Arora
CSED
29. ! - Logical NOT
This operator reverses the result of the expression it operates on.
For example, if the expression evaluates to a non-zero value, then
applying ! operator to it results into a 0.
Vice versa, if the expression evaluates to zero then on applying !
operator to it makes it 1, a non-zero value.
Vinay Arora
CSED
34. ? : - Conditional Operator
The conditional operators ? and : are sometimes called Ternary
Operators since they take three arguments.
They form a kind of foreshortened if-then-else.
Their general form is, expression 1 ? expression 2 : expression 3
If expression 1 is true (that is, if its value is non-zero), then the value
returned will be expression 2, otherwise the value returned will be
expression 3.
Vinay Arora
CSED
38. goto Statement
goto is used to switch the control flow.
In a difficult programming situation it seems so easy to use a goto to
take the control.
In most of the scenarios use of goto is depreciated.
goto can be replaced by if-else, switch, for.
Vinay Arora
CSED
42. switch Statement
The control statement that allows us to make a decision from the number
of choices is called a switch.
The integer expression following the keyword switch is any C expression that
will yield an integer value.
Vinay Arora
CSED
47. Loops
Repetitive instructions is done through a Loop control instruction.
This involves repeating some portion of the program either a
specified number of times or until a particular condition is being
satisfied.
There are three methods by way of which we can repeat a part of a
program. They are:
(a) Using a while statement
(b) Using a do-while statement
(c) Using a for statement
Vinay Arora
CSED
55. While Loop – Program 2
//Program to demonstrate simple while loop with decrement operator
#include<stdio.h>
#include<conio.h>
void main()
{
int i=5;
clrscr();
while (i>=1)
{
printf("%dn",i);
i=i-1;
}
getch();
}
Vinay Arora
CSED
57. While Loop – Program 3
/* Program to demonstrate simple while loop taking incremental value
as float */
#include<stdio.h>
#include<conio.h>
void main()
{
float i=10.0;
clrscr();
while (i<=10.5)
{
printf("nCivil Engineering at Thapar");
i=i+.1;
}
getch();
}
Vinay Arora
CSED
59. While Loop – Program 4
//Demonstrating simple while loop with integer value out of range
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
while (i<=32767)
{
printf("%dn",i);
i=i+1;
}
getch();
}
Vinay Arora
CSED
63. While Loop – Program 6
//Program to demonstrate post increment operator in while loop
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
while (i<=10)
{
printf("%dn",i);
i=i++;
}
getch();
}
Vinay Arora
CSED
65. While Loop – Program 7
//Program to demonstrate compound assignment operator within while loop
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
while (i<=5)
{
printf("%dn",i);
i+=1;
}
getch();
}
Vinay Arora
CSED
67. While Loop – Program 8
//Program to demonstrate post increment operator with while loop
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0;
clrscr();
while (i++ < 5)
{
printf("%dn",i);
}
getch();
}
Vinay Arora
CSED
69. While Loop – Program 9
//Program to find out even numbers between 1-10
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
while (i<=10)
{
if (i%2==0)
printf("%dn",i);
i=i+1;
}
getch();
}
Vinay Arora
CSED
72. Do-while Loop – Program 10
//Program to demonstrate DO-WHILE loop
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
/*
while(i<1)
{
printf("hello i am at Thapar");
}
*/
do
{
printf("hello i am at Thapar");
}
while(i<1);
getch();
}
Vinay Arora
CSED
81. For Loop (Program-3)
//Program to demonstrate simple For loop
//Print numbers from 1 to 5
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
for (;i<=5;i=i+1)
{
printf("%dn",i);
}
getch();
}
Vinay Arora
CSED