c codes

Decrement operator in c with example

The decrement operators used to subtract 1 to previous value. Decrement operator is unary operator because only one variable is used.

There are 2 type in decrement operator. They are
1. Postfix decrement operator
2. Prefix decrement operator

Syntax:-

For postfix

variable--

For prefix

--variable

Both prefix and postfix decrement operator are same thing when they used independently(i-- or --i) ie subtract 1 to previous value. But they behave differently when they used in expression on the right hand side of assignment statement(num=i-- or num=--i)

Postfix decrement operator:-


Output:-

i=1
num=2

Execution:-

In postfix decrement operator, fist assign value to left side variable(num) than decrement by 1(i)

Here in our example first i is three(i=3) than i decrement by 1(i--). here i value is 2.

Next statement is num = i--;

Here postfix is used so first i value is assigned to num ie 2 than i is decrement by 1 ie i value becomes 1.

Prefix decrement operator


Output:-

i=1
num=1

Execution:-

In prefix decrement operator, first decrement(i) by 1 than result value is assigned to left side variable(num)

Here in our example first i is three(i=3) than i decrement by 1(--i). here i value is 2.

Next statement is num = --i;

Here prefix is used so first i is decrement by 1 ie i value becomes 1. Than value of i is assigned to num ie 1

Example:-

Program to fined sum of N odd number.


Previous Code:-
Increment operator

List Code:-
C Codes

Next Code:-
for loop

C Programs
List of c Programs

Increment operators in c with example

The increment operators used to adds 1 to previous value. Increment operator is unary operator because only one variable is used.

There are 2 type in increment operator. They are
1. Postfix increment operator
2. Prefix increment operator

Syntax:-

For postfix

variable++

For prefix

++variable

Both prefix and postfix increment operator are same thing when they used independently(i++ or ++i) ie adds 1 to previous value. But they behave differently when they used in expression on the right hand side of assignment statement(num=i++ or num=++i)

Postfix increment operator:-


Output:-

i=2
num=1

Execution:-

In postfix increment operator, fist assign value to left side variable(num) than increment by 1(i)

Here in our example first i is zero(i=0) than i increment by 1(i++). here i value is 1.

Next statement is num = i++;

Here postfix is used so first i value is assigned to num ie 1 than i is increment by 1 ie i value becomes 2.

Prefix increment operator



Output:-

i=2
num=2

Execution:-

In prefix increment operator, first increment(i) by 1 than result value is assigned to left side variable(num)

Here in our example first i is zero(i=0) than i increment by 1(++i). here i value is 1.

Next statement is num = ++i;

Here prefix is used so first i is increment by 1 ie i value becomes 2. Than value of i is assigned to num ie 2

Example:-

Program to fined sum of N odd number.


Previous Code:-
Condition Operators

List Code:-
C Codes

Next Code:-
Decrement operator

C Programs
List of c Programs

Conditional operator in c with example

Conditional operator is alternative to simple if-else statement

Syntax:-

expr1 ? expr2 : expr3

Execution:-

expr1 is always evaluated, if results is true than expr2 is executed otherwise expr3 is executed.

Example:-

Program to find given number as even or odd.


Previous Code:-
else if statement

List Code:-
C Codes

Next Code:-
Increment operator

C Programs
List of c Programs

Bitwise operators in c with example

Bitwise used for manipulation of data in bit level.

Operator Function
` bitwise NOT
<< left shift
>> right shift
& bitwise AND
^ bitwise XOR
| bitwise OR
&= bitwise AND assign
^= bitwise XOR assign
|= bitwise OR assign

Bitwise not applicable for float, double and char.

Syntax:-

syntax for & | and ^

value1 bitwise value2

Example:-
2 & 3
num1 & num2

Here value1 and value2 must be integer data type


syntax for >> and <<

value bitwise number_of_shift

Example:-
6 >> 2
num >> 3

Here value and number_of_shift  must be integer data type

Note:-
# Left shift by 2 is equal to multiply by 2
# Right shift by 2 is equal to division by 2


Example:-

Program for shift left


Output:-
Enter a number
5
answer: 20

5 is in binary 101 when we shift left we put zero at right side. Here we shifting by 2 so we put 2 zero at right side 10100 ie 20

Previous Code:-
Logical operators

List Code:-
C Codes

Next Code:-
if decision making statement

C Programs
List of c Programs

Logical operators in c with example

Operator Meaning
< less than
<= less than or equal to
> greater than
>= greater than or equal to
== equal to
!= not equal to
&& logical AND
|| logical OR
! logical NOT

The above 1st 6 are relational operators in java and last 3 are logical operators

The relation operators are used to compare 2 values. The output of relational operators is either true (1) or false (0)

Syntax:-

value1 relational_operator value2


Example:-

2 < 10     True
3 > 8       False
7.5>=5    True

value1 and value2 are can be constants, variable or combination of them.

Relation operator are used in if-else or in loop statement.

Example:-

Program to find Greatest number between 2


Logical operators:-

logical operators && and || are used to when we want to check 2 or more condition statement.

Example

8 > 7 && 5 < 8      True
8 < 5 || 8 < 9           False
7 > 2 && 19 < 2    False

AND (&&) operator output is true only when either condition statement is true.
OR (||) operator output is false only when either condition statement is false.

Truth table:-


A B AND
0 0 0
0 1 0
1 0 0
1 1 1


A B OR
0 0 0
0 1 1
1 0 1
1 1 1



NOT (!)  logical operator is used for compliment condition statement outputs.

Truth table:-

A NOT
0 1
1 0


Example:-

Program to solve following problem
The candidate is selected if his age and height is 18 and 5 and more respectively.



Java program for logical NOT
Program to find max number between 2




Previous Code:-
Arithmetic operators

List Code:-
C Codes

Next Code:-
Bitwise Operators

C Programs
List of c Programs

continue statement in c with example

Continue statement tell the compiler to skip the following statement and continue with the next iteration

Syntax:-

continue;

Continue statement in for loop

Here continue causes the control to go to increment section(3rd section in for loop) of the loop than condition statement(2nd section in for loop).

Example:-

c program for display even number using continue statement



Continue statement in while and do while loop

Here continue causes the control to go to condition statement of loop.

Example:-

c program display odd number using continue statement



Previous Code:-
else if statement

List Code:-
C Codes

Next Code:-
for loop

C Programs
List of c Programs

Break statement in c with example

Break statement is used to exit from loop

Break statement in loops

Break statement immediately transfer control to the out of loop and statement which is immediately after loop is executed. In other word break statement terminate loop.

Note:-
# Break is terminated only one loop in case of nested loop

Example:-

c program to count N number using break statement



Example:-

Break statement in nested loop



Break statement in switch

The break statement transfer the control to out of the switch statement and statement which is immediately after switch is executed.

Note:-
# break is optional, if 2 or more case labels belongs to same statement.

Program to accept day number of week and display the corresponding week day




Previous Code:-
Switch case in c with examples

List Code:-
C Codes

Next Code:-
Continue Statement

C Programs
List of c Programs

Nested loop in c with example

c allows for, while and do while loops to be nested. that is one loop is inside another loop.

Example:-



Output:-

.....
....
...
..
.

one type of loop can be nested with other type of loop. for example while loop is inside for loop.

Example:-



Previous Code:-
if-else statement

List Code:-
C Codes

Next Code:-
else if statement

C Programs
List of c Programs

Arithmetic operators in c with example

Different Arithmetic operators used c are shown in table.

Operator function
* Multiplication
/ Division
% Remainder
+ Addition
- Subtraction

* operator is used give product of 2 numbers.

/ Operator gives Quotient after dividing Divisor and Dividend. Divisor should be nonzero number.

% operator gives remainder after division of 2 numbers. % operator can be used only for integer data type.

+ operator gives sum of 2 numbers.

- operator used to subtract one number from another number.

Note:-

# If both number are positive than answer will be in positive number.
# If either or both number are negative than answer will be either in positive or in negative, it is machine dependent.

Example:-

Program to find sum of 2 number.



Program to find product of 2 number.


Program to find subtract 2 number.


Program to find division 2 number.



Program to find remainder.



Previous Code:-
Keyword used in c

List Code:-
C Codes

Next Code:-
Logical operators

C Programs
List of c Programs

List of keyword used in c

All keywords have fixed meaning and meaning can`t be changed. keyword are basic building blocks for program.

list of keyword used in c

auto double int struct
breack else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while


Note:-

# Keyword can`t be used as variable name or class name.

Previous Code:-


List Code:-
C Codes

Next Code:-
Arithmetic operators

C Programs
List of c Programs

else if statement in c with example

else if statement is used when multipath decision are involved.

Syntax:-

if(condition 1)
{
statement 1
}
else if (condition 2)
{
statement 2
}
else if (condition 3)
{
statement 3
}
.
.
.
else if (condition N)
{
statement N
}
statement

Note:-

# Condition can be expression or relation.
# If body of statement is only one statement than braces ({}) are not needed.
# Body of the statement may have one or more statement.
# Any one statement is executed not all.

Execution:-

The conditions are checked from top to bottom when true condition is found than statement related to that condition is executed and control is transfer to statement next to else is statement
when all condition are false than statement related to else is executed and control is transfer to statement next to else is statement

Examples:-

Program to solve following problem

Average marks Grade
100-75 1
74-50 2
49-30 3
29-30 4



Previous Code:-
Nesting of if statement

List Code:-
C Codes

Next Code:-
Condition Operators

C Programs
List of c Programs

Nesting of if – else statement in c with example

When a series of decision are involved, we may have to use more than one if else statement in nested form as shown below

Syntax

if(condition 1)
{
if(condition 2)
{
True - Block of Statement 1
}
else
{
false - Block of Statement 2
}
}
else
{
false - Block of Statement 3
}

Note:-

# Condition can be expression or relation.
# If body of statement is only one statement than braces ({}) are not needed.
# Body of the statement may have one or more statement.
# Either true – block of statement or false – block of statement is executed, not both.

Execution:-

When both condition 1 and condition 2 are true than True - Block of Statement 1 is executed.
If condition 1 is true and condition 2 is false than false - Block of Statement 2 is executed.
If condition 1 is false than false – blocked statement 3 is executed.
In each case control is moved to statement that is next to if statement.

Examples:-

Program to solve following problem
The candidate is selected if his age and height is 18 and 5 and more respectively.

Previous Code:-
if-else statement

List Code:-
C Codes

Next Code:-
else if statement

C Programs
List of c Programs

If – else statement in c with example

The if – else statement is an extension of the simple if statement.

Syntax:-

if(condition)
{
True - Block of Statement
}
else
{
False - Block of Statement
}
Statement;

Note:-

# Condition can be expression or relation.
# If body of statement is only one statement than braces ({}) are not needed.
# Body of the statement may have one or more statement.
# Either true – block of statement or false – block of statement is executed, not both.

Execution:-

If condition is true than true - block of statement is executed otherwise false - block statement is executed than control is moved to statement next to if statement.

Examples:-

Program to find even number



Program to find greatest number between 2.



Previous Code:-
if decision making statement

List Code:-
C Codes

Next Code:-
Nesting of if statement

C Programs
List of c Programs

If decision making statement in c with example

The if statement is a powerful decision making statement and used to control the flow of execution of statements.

Syntax

if(condition)
{
Block of Statement
}
statement;

Note:-

# Condition can be expression or relation.
# If body of statement is only one statement than braces ({}) are not needed.
# Body of the statement may have one or more statement.

Execution:-

If condition is true than block of statement is executed otherwise statement next to if statement is executed.

Example:-


Previous Code:-
Bitwise Operators

List Code:-
C Codes

Next Code:-
if-else statement

C Programs
List of c Programs

Do while loop in c with example

Do While loop is another loop in c

Syntax:-

do
{
Body of the loop
}
while(condition);
Statement;

Note:-

# If body of loop is only one statement than braces ({}) are not need.
# Body of the loop may have one or more statement.
# Semicolon is present after while.
# At least once body of loop is executed even condition is false.

Execution:-

In do while loop 1st body of the loop is executed than condition will check if condition is true than body of the loop will executed. After body of the loop is execute, the condition is again tested if condition is true than again body of loop is executed, this process is repeated until condition is true. When condition is false than statement next to while loop is executed.

Hence body is 1st executed before the condition check so body of loop is always executed at least once.

Example:-

Program to count N numbers



Program to display even number up to 20



Example:-

To count N number to show condition is checked at bottom


Output:-
Enter a number 6

2
4
6
8
10
12
14
16
18
20
22

Here 22 also displayed even we put condition (i =<20)because condition is checked at bottom of loop.

Previous Code:-
while loop

List Code:-
C Codes

Next Code:-
Nested loop

C Programs
List of c Programs

For loop in c with example

For loop is another loop in c.

Syntax:-

for(initialization; condition; increment)
{
Body of the loop
}
Statement;

Note:-

# If body of loop is only one statement than braces ({}) are not needed.
# Body of the loop may have one or more statement.
# Initialization and increment can be skip but semicolon should be there to separate 3 section.
# Condition section can`t be skip in for loop.

Execution:-

In for loop 1st variable is initialized than condition will check if condition is true than body of the loop will executed. After body of the loop is execute, variable is incremented and the condition is again tested if condition is true than again body of loop is executed, this process is repeated until condition is true. When condition is false than statement next to while loop is executed.

For loop can be written in another way by skipping initialization and increment condition.

Syntax:-

Initialization;
for( ; condition; )
{
Body of the loop
Increment;
}
statement

Variable can be declared in for loop

Example:-

for(int i=0; i< num; i++) Note:- for loop initial declaration are only allowed in c99 mode. Example:-

Program to count N numbers


Program to display even number up to 20



Previous Code:-
Decrement operator

List Code:-
C Codes

Next Code:-
while loop

C Programs
List of c Programs

While loop in c with example

While loop is simplest loop in c

Syntax:-

while(condition)
{
Body of the loop
}
Statement;

Note:-

# If body of loop is only one statement than braces ({}) are not needed.
# Body of the loop may have one or more statement.

Execution:-

In while loop 1st condition will check if condition is true than body of the loop will executed. After body of the loop is execute, the condition is again tested if condition is true than again body of loop is executed, this process is repeated until condition is true. When condition is false than statement next to while loop is executed.

Example:-

Program to count N numbers


Program to display even number up to 20





Previous Code:-
for loop

List Code:-
C Codes

Next Code:-
do-while loop

C Programs
List of c Programs

Switch Case in C with Examples

The Switch statement tests the value of a given expression against a list of case labels, when they get matches, block of statement associated with that case in executed.

The general form of switch case

switch (expression)
{
case label1:
block of statement
break;

case label2:
block of statement
break;

case label3:
block of statement
break;
.
.
.

case labeln:
block of statement
break;

default:
block of statement
break;
}


Note :-


# The Expression and label are integer or characters.
# floats are not allowing in case labels.
# labels much be unique.
# block of statement may be zero or more than one statements.
# Case labels end with colon(:).
# The break is followed by semicolon(;).
# Default case is optional case.
# break is optional, if 2 or more case labels belongs to same statement.

Default case is optional case.

How Case Statement Execute

The expression is made to compared with each case labels, if they matches each others than block of statements related to particular case is executed and than control is transferred to statement that following the switch case.

The break statement at the end of each block signals the end of a particular case and causes an exit from the switch statement, transferring the control to the statement that following the switch case.

If value of expression is does not match with case label than default statement is execute and than control is transferred to statement that following the switch case.

Examples

Program to accept day number of week and display the corresponding week day





Program to display position of vowel in alphabet




Note :- U is different from u.

Previous Code:-
Nested loop

List Code:-
C Codes

Next Code:-
Break Statement

C Programs
List of c Programs

Back to Top