Search This Blog

Friday, January 3, 2020

c programming conditional branching | codeswithabhi

Mr. abhijeet dwivedi

C programming conditional branching

  Conditional branching.

 

 

1. Decision making:-

The decision making is use for the weather condition is true or false .
The decision making have a following types:-

*if:-

It is used for testing one condition. if condition is true then back will be executed.

Syntax:-


if(condition test)
{
statememt:
}


For e.g:-

Write a program to check the number is zero or non zero.


#include<stdio.h>
#include<stdio.h>
void main()
{
int n;
printf("enter no");
scanf("%d",&n);
if(n==0)
{
printf("number is non-zero");
}
getch();
}

Output

enter no 4
number is non zero

* if else:-

If else is based for two condition either it will be true or false.

Syntax:

if(condition test)
{
statement;
}
else
{
statement;
}

For e.g:-

Write a prgram to chech weather no is zero or non zero


#include<stdio,h>
#include<conio.h>
void main()
{
int n;
printf("enetr no");
scanf("%d",&n);
if(n==0)
{
printf("number is non zero");
}
else
{
printf("number is zero");
}
getch();
}

Output

enter no 6
number is no zero

* Ladder if:-

The ladder if is use for checking the multiple condition of the program.it also check the condition of the program.

Syntax:-

if(condition test)
{
statement;
}
else if(condition test)
{
statement;
}
else if(condition test)
{
statement;
}
else
{
statement;
}
getch();
}

For e.g:-

write a program to find a greater three digit no...


                                              #include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf("enter the value of a,b and c");
scanf("%d%d%d",&a,&b,&c);
if(a<b && b<c)
{
printf("a is greater");
}
else if(b<a && b<c)
{
printf("b is greater");
}
else if(c<a && c<b)
{
printf("c is greater");
}
else
{
printf("wrong choice");
}
getch();
}

Output

enter a value of a,b,c  2
3
4
C is greater

*  Nested if:-

 nested if is an if statement that is target of another of another if statement.
Nested if statement means an if statement inside another if statement.

Synatx:-

if(condition test)
{
else if(condition test)
{
else if9condition test)
{
statement;
}
else
{
statement;
}
}

}


* Switch case:-

Switch case statement are a substitute for long if statement that compare a variable to several integrals value. it is use for to select M.C.Q as for given situation.

Syntax:-

switch()
{
case 1:
statement:
break;
case 2:
statement;
break;
case 3:
statement;
break;
default;
break;
}

Break:-

Break is a keyword which is use for to exit from current location,current block,current loop.



                                           2. Iteration/repetition

In iteration, we talk about the loop. so,
There are mainly three types of loops following are:-

While loop: -

A while loop is a control flow of statement that allow code to be executed again and again based on a given boolean expansion.
it is also a entry control loop.

Syntax:-


initial value;
while(condition test)
{
expression;
statement;
updation;
}

For e.g:-write a program to print a no series from 1 to 5.

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
while(i<=5)
{
printf("%d",i);
i++;
}
getch();
}


Output

1
2
3
4
5



Do while:-


Do while loop is also known as a exit control loop. Do while loop execute and check and test condition at last so firstly, it not check the condition and executed the statement.

Syntax:-


initiial value
do
{
expression;
statement;
updation;
}
while(condition test);


For e.g:-


#include<,stdio.h>
#include<conio.h>
void main()
{
int i;
do
{
printf("%d",i);
i++;
}
while(i<=4);
getch();
}

Output

1
2
3
4


For loop:-


A for loop is a control flow statement for specifying iteration which allow code to be executed again and again.

Syntax:-

for(initial value;condition test;updation)
{
expression;
statement;
}


For e.g:
-


#include<stdio.h>
#include<conio.h>
void  main()
{
int i;
for(i=1;i<=4;i++)
{
printf("%d",i);
}
getch();
}

Output

1
2
3
4



Nested loop:-


You can create code that embeds one loop inside another loop. This is known as nested loop.

Syntax:-
ROW----------for(initial value;condition test;updation)
{
column-----for(initial value;condition test;updation)
{
statement 1;
}
statement 2;
}

For e.g:-

To print  this pattern:-
***
***
***


#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;j=1;
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf("*");
}
printf("\n");
}
getch();
}

Output
***
***
***







c programming language
c programming for prime number
c programming basics
c programming structure
c programming tutorial
c programming array
c programming questions for interview
c programming pdf
for c programming example
c programming examples
c programming questions
c programming interview questions
c programming hello world
what c programming
c programming online compiler
c programming functions
c programming string
c programming online
c programming com…
c programming language
c programming for prime number
c programming basics
c programming structure
c programming tutorial
c programming questions for interview
c programming pdf
for c programming example
c programming examples
c programming questions
c programming interview questions
c programming hello world
what c programming

Thursday, January 2, 2020

c programming preprocessor directives | codeswithabhi

Mr. abhijeet dwivedi

c programming preprocessor directives

In this tutorial, you will learn:

Preprocessor Directives:-

 

Preprocessor directives is a program that processes the code before compilation.
It represents by start with symbol [#] processor directives is a line of code which is terminated by a semicolon(;) 
The source code written by programmer is stored in the file program.c . This file is then processed by preprocessor and and expanded source code file general named program.
Preprocessor program provide processor directive which tell the compiler to process the source code before compilation

Types of preprocessor  directives:-

There are main three preprocessor directives :-

1. File Inclusion.
2. Macro substitution.
3. compilation control.

1. File Inclusion:-

File inclusion means including or using a header file in a program by using include keyword with preprocessor directives 
For e.g:-        #include<header  file>

* Header file or standard file:-

These file contain definition of predefined function like, printf( ); ,scanf(); ,etc.

User defined function:-

When a program became very large, it's good practise to divide it into smallar files and include whenever 
for example:- #include<filename>

 2. Macro substitution:- 

Macro substitution is the shortcut that is always or name which is replaced by a value & string.
Macro substitution creates by using a defined keyword which also begins with its symbol.
Macro substitution holds any kind of value we can not update the value of macro during the program.
For example:-
#incluede<stdio.h>
#include<conio.h>
# define Line 5
void main()
{
int i ;
for(i=1;i<=line;i++)
{
printf("%d",i);
}
getch();
}

OUTPUT:-

1
2
3
4
5

To chech which variable may be update or not:-

#include<stdio.h>
#include<conio.h>
#define x 100
void main()
{
int z=10;
printf("%d",z);
printf("%d",x);
z=z+1;  ---------------------------------[it is possible because it is local variable]
x=x+1;----------------------------------[it is not possible because it is macro]
getch();

3. Compilation Control:-

The conditional directives are a type of directives that helps to compile a specific portion of the program or to skip compilation of some specific part of the program based on some condition.
This can be done with the help of two preprocessing commands ['ifdef'  and  'endif']

Syntax

#ifdef  macro_name
statement 1:
statement 2:
statement 3:
.
.
.
.
.
.
statement n;
#endif

Explaination of some preprocessor diectives :

* #undef

To undefine a macro means to cancel its definition. This is done with the #undef directives.
syntax:  #undef token

* #ifdef

The ifdef preprocessor directives checks if macro is defined by #define . If yes , it excutes the code.

syntax:

#ifdef MACRO
//code
#endif

* #ifndef

The #ifndef preprocessor directives checksif macro isn't defined by #define. If yes, it executes the code.

syntax:-

#ifndef  MACRO
//code
#endif

* #if

The #if preprocessor directivs evalutes the expressiion  or condition if condition of #if is true, it execute the code.

Syntax:

#if expression
//code
#endif

* #else

The #else preprocessor directive evalutes the expression or condition if condition is #if is false.

Syntax:

#if expression
// if code
 #else
//else code
#endif

* #Error

The #error preprocessor directive indicates error. The compiler gives fatal error if #error directive is found.

c programming language

c programming for prime number

c programming basics

c programming structure

c programming tutorial

c programming array

c programming questions for interview

c programming pdf

for c programming example

c programming examples

c programming questions

c programming interview questions

c programming hello world

what c programming

c programming online compiler

c programming functions

c programming string

c programming online

c programming com…

c programming language

c programming for prime number

c programming basics

c programming structure

c programming tutorial

c programming questions for interview

c programming pdf

for c programming example

c programming examples

c programming questions

c programming interview questions

c programming hello world

what c programming

c programming online compiler

c programming functions

c programming string

c programming online

c programming compiler

c programming in vs code

how learn c programming

c programming software

c programming reverse number

c programming for loop

c programming practice

c programming for beginners

c programming examples with output

c programming recursion