• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Control flow statements in C

Control flow statements in C Language in Detail:-

Control flow statements always control the   execution flow of the program.

  • Control flow statements are classified in to three types.

i.e

  1. Selection statement :-2 eg :-if,else,elseif,switch
  2. Interaction statements:- 2 eg:- While,for,do-while
  3. Jumping statements:- 2 eg:- break,continue,goto

 

1. Selection statement :-

Selection statements are also called as decision making statements

  • When we are working with selection statements, if the condition is true, then control will pass within the body, if it is false control will pass outside the body

Syntax to if:

If (condition)

{

Statement 1;

Statement 2;

Statement 3;

———————-

———————–

}

 

  • Constructing the body is always optional
  • For a single statement, doesn’t require to specify the body
  • If the body is not specified, then automatically,

Condition part will terminals with next semicolon(;)

 

 

 

Else :- It is a keyword, by using this keyword, we can create alternate block for if condition.

  • Using else is always optional, it is recommended to use, when we are having alternate block.
  • When we are working with if and else, among those two blocks, only one block will be executed.
  • Else block will be executed whenever the if condition i.e false

 

  • If(condition)

{

Statement 1;

Statement 2;

}

Else

{

Statement 3;

Statement 4;

}

 

 

 

 

Void main()

{

Printf(“a”);

Printf(“b”);

If(5>2!=1)

{

Printf(“welcome”);

Printf(“Hello”);

}

Printf(“c”);

Printf(“D”);

o/p: ABCD

 

false, so it doesn’t execute the satatements which are present inside the block

  • If(condition)

Statement1;

Else

Statement 2;

 

4) if(condition)

Statements;

Else

{

Statement2;

Statement3;

}

  • If (condition)

{

Statement1;

Statement2;

}

Else

Statement3;

 

 

 

Generally when we are working with constant expressions, we will get warning message from compiler i.e condition is always false or true.

  • Warnings can be ignored automatically at the time of execution.

 

Void main()

Printf(“NIT”);

Printf(“welcome”);

If(15!=5!=0)     // if(0!=5!=0) // if(1!=0)

{

Printf(“A”);

Printf(“B”);

}

Printf(“c”);

}

o/p:-NIT welcome ABC        “Every non Zero is true”

 

 

 

 

  • Void main()

{

Printf(“welcome”);

Printf(“NIT”);

If(!5)

Printf(“A”);

Printf(“B”);

Printf(“c”);

 

O/P:  WelcomeNITBC.

 

When we are constructing multiple statements without using body then first statement only will consider within the “if”, because scope will be terminated with next semicolon.

  • Void main()

{

Printf(“A”);

If(5>2 && 8<5) // if(1 && 0)

{

Printf(“B”);

Printf(“c”);

}

Else

{

Printf(“welcome”);

Printf(“Hello”);

}

Printf(“D”);

}

o/p: a welcome Hello

 

 

  • Void main()

Printf(“welcome”);

If(1<2!=0 && 1=5>2)   // if(1!=0 && 1==1)

{

Printf(“A”);

Printf(“B”);

}

Else

{

Printf(“x”);

Printf(“y”);

}

Printf(“NIT”);

}

 

o/p:- Welcome AB NIT.

 

 

  • Void main()

{

Printf(“A”);

If(8!=8>2 or 4!=1<4)

{

Printf(“B”);

Printf(“C”);

}

Else

Printf(“NIT”);

Printf(“Y”);

Printf(“Z”);

}

o/p: ABCYZ.

 

When the body is not specified for else part then automatically scope will terminated with next statement only. Ie. Under the else part, first statement will place

 

  • Void main()

Printf(“Hello”);

If(15!=2>5)

Printf(“A”);

Printf(“B”);     on necessary because else should start at the

else                 immediate ending of if statement. If is not

{                       having the body.

Printf(“X”);

Printf(“Y”);

}

}

o/p:  Error misplaced error

 

when we are using the else part, it should start immediately of if scope only i.e where the scope of the if is terminated there itself.

 

Else part must be started.

Summary
Review Date
Reviewed Item
Control flow statements in C
Author Rating
5

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Control flow statements in C"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Site Disclaimer, Copyright © 2016 - All Rights Reserved.

    Support


    Please leave a message and we'll get back to you soon.

    I agree to be contacted via e-mail.