Welcome Guest   Login   Sep 21, 2017
Tutorials
C
HTML
Java
<?php include("../../config.inc.php"); $section="Tutorials - Programming in C - Chapt 3"; include("../../title.inc.php"); ?>
Chapter-3
Programming Constructs & Operators

A. Sequence : The normal sequence of program is from left to right and from top to bottom.
 
 

B. Selection : Selection construct allows programmer to write a program which is selectively executed, which means if the condition is satisfied one part of the program will be executed otherwise some other part will be executed. It is also called branching

In ‘C’ there are two selection constructs if... else & switch … case
 
 

C. Iteration : Iteration construct allows programmer to write a program with some statements, which repetetively execute some statements again and again. It is also called looping

In ‘C’ there are three iteration constructs : for, while & do …. while.


Selection Constructs

if... else example
 
 
main()
{
int i;
printf("\nEnter a number : ");
scanf("%d",&i);

if( I > 7 )
printf("I is greater than seven");

else
printf("I is not greater than seven");

}


 


swith… case example
 
 
main()
{
char grade;
printf("Enter your grade : ");
grade=getche();

switch(grade)
{

case ‘a’ : printf(" A grade gets Rs.700 Bonus ");
break;

case ‘b’: printf(" B grade gets Rs. 600 Bonus");
break;

case ‘c’: printf("C grade gets Rs. 500 Bonus");
break;

default : printf("Other than these grades gets Rs. 200 Bonus");

}


Iteration Constructs

for loop

when you know how many times the loop is to be executed, make use of for loop.
 
main()

{
int i;

for(i=0;i<10;I++)
{

printf("\n%d",I);
}

}

while loop

when you don’t know how many times the loop is to be executed, make use of while loop.
 
main()

{
char ch=’ ’;

while( ch !=’n’)
{

printf("\nAre you intelligent :");
ch=getche();
}

printf("\nI am more intelligent than you");

}


 

do… while loop

when you want to execute the loop at least once even if the condition is false, make use of while loop.
 
main()

{
int var=5;

do
{
printf("\n var=%d",var);
var++;

}while(var > 10);

}

in above example even if the while condition is false at least once the loop is executed. So you get the output var=5;
 
 

break statement :

break statement breaks the loop, jumps out of the loop & rest of the code is executed normally.
 
main()
{
int i;

for(I=1;i<10;i++)
{
printf("\ni=%d",i);

if(i==5)
break;
}
printf("\nLoop ends here…");

}

This program uses the loop which should display numbers from 1 to 10, but in between if statement checks for number equals to 5 & if it is found breaks the loop. But other code is executed normally. so it also displays the message Loop ends here…
 
 

exit()

where break breaks the loop, exit() statement terminates the program. If you want to terminate the program abnormally in between only it can be done by putting exit statement. After exit nothing is executed.
 
Main()

{
int i;

for(i=1;i<10;i++)
{
printf("\ni=%d",i);

if(i==5)
exit();

}
printf("\nLoop ends here…");
}

continue statement:

continue statement can be given only in loops, after the continue statement nothing is executed, the control is passed back to beginning of loop with a next counter.
 
main()
{
int var;

for(var=1;var<=100;var++)
{
if(var%7==0)
{
printf(" * \t ");
continue; 
}
printf(" %d \t",var);
}
}


 

this program displays the numbers from 1 to 100 and if the number is divisible with 7 it displays * and continues the work i.e. not printing the number and continuing the loop with the next counter.

Operators

Operators can be classified in different ways. This is the simplest way of understanding them.

In expression ( A + B ) : + is operator & A & B are operands.

Mathmetical
Relational
Logical
Unary
Binary
Ternary
+
+
? & :
>
&& ( And)
-
-
>=
|| ( Or)
++ 
(increment)
*
<
! ( Not )
--
(decrement)
/
<=
%
==
!=

 

Using Simple Mathematical Operators
 
main()
{
int x=20;
int y=30;
printf("x = %d, y = %d",x,y);
printf("Addition : %d",(x+y));
printf("Subtraction :%d",(x-y));
printf("Division :%d",(x/y));
printf("Multiplication : %d",(x+y));
}

Above example describes how to make use of Binary Operators. Simple operations like addition, subtraction, division, multiplication are demonstrated in this.

Using Increment and decrement operator
 
main()
{
int x=20;
int y=30;
printf("x = %d, y = %d",x,y);
x++;
y--;
printf("x = %d, y = %d",x,y);
}

Above example shows increment and decrement unary operators.
Where :
x++ is nothing but x = x + 1

&

x-- is nothing but x = x - 1
 
 

Using Ternary operator
 
main()
{
int x=20;
int y=30;
int max = ( x > y ) ? x : y;
printf("x = %d, y = %d",x,y);
printf("max = %d ",max);
}

Ternary operator is used to put some comparision test. If true assign some value if false assign other.

(Condition) ? true : false;


 
 

My New Blog

My Award winning Whitepaper

Contradicting Light
My Personal Blog


Trace Mobile Location



My Book Reviews
 




Tech Jokes Worth Reading
 
Top
Home
www.deepjava.com 1999-2017 (C) Developed and maintained by D-Kay Consultancy