[go: up one dir, main page]

0% found this document useful (0 votes)
5 views26 pages

Unit 1 (Third)

The document discusses control flow in programming, specifically focusing on decision-making and branching statements in C. It covers various types of control statements such as if, if-else, nested if, if-else ladder, and switch statements, as well as looping constructs like for, while, and do-while loops. Additionally, it introduces transfer statements like break, goto, and continue, explaining their usage in controlling program execution.

Uploaded by

shreyas70792006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views26 pages

Unit 1 (Third)

The document discusses control flow in programming, specifically focusing on decision-making and branching statements in C. It covers various types of control statements such as if, if-else, nested if, if-else ladder, and switch statements, as well as looping constructs like for, while, and do-while loops. Additionally, it introduces transfer statements like break, goto, and continue, explaining their usage in controlling program execution.

Uploaded by

shreyas70792006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Unit – 1

(Third One)

Mr. Harjender Singh


Asstt. Professor
FLOW OF CONTROL OR DECISION MAKING &
BRANCHING
• A program’s control flow is the order in which
the program’s code executes.
• There are 03 control flow statements :
– Sequential - default mode
– Selection - used for decisions and branching
– Repetition - used for looping, i.e., repeating a
piece of code multiple times.
• Decision Statements : Decision making statement is
depending on the condition.
• if the condition is "true" statement of first block will be
executed,
• and if condition is "false" then statement of second
block will be executed.
• DIFFERENT TYPES OF DECISION MAKING STATEMENTS
ARE :
• If(a>25)
• {
• Statement 1;
• Else
• Statement 2;
• }
Control Description
Statements

If statements If statement is used to execute the code if


condition is true

If else if else statement is used to execute a code when


statements condition is true otherwise it execute another
code in else section

Nested if We can use if statement inside another if statement,


statements if we want to execute a code when multiple
conditions become true

If else if ladder If else ladder is used, If we want to check for


multiple conditions one after another

Switch It selects one of the several alternative based on


statements fixed value for a given expression.
Simple if statement

• if the expression returns true, then the statement-inside will be executed,


otherwise statement-inside is skipped and only the statement-outside is
executed.
• #include <stdio.h>
• void main( )
• {
• int x, y;
• x = 15;
• y = 13;
• if (x > y )
• {
• printf("x is greater than y");
• }
• }
if...else statement

• If the expression is true, the statement-block1 is executed,


else statement-block1 is skipped and statement-block2 is executed.
• Syntax :
• if(expression)
• {
• statement block1;
• }
• else
• {
• statement block2;
• }
• #include <stdio.h>
• void main( )
• {
• int x, y;
• x = 15;
• y = 18;
• if (x > y )
• {
• printf("x is greater than y");
• }
• else
• {
• printf("y is greater than x");
• }
• }
Nested if....else statement

• if expression is false then statement-block3 will be executed, otherwise the execution


continues and enters inside the first if to perform the check for the next if block, where
if expression 1 is true the statement-block1 is executed otherwise statement-block2 is
executed.
• if( expression ) a>b if(100>20)
• {

• {
• if( expression1 ) if(a<b)
• {
• statement block1;
• }
• else
• {
• statement block2;
• }
• }
• else
• {
• statement block3;
• }
• #include <stdio.h>

• void main( )
• {
• int a, b, c;
• printf("Enter 3 numbers...");
• scanf("%d%d%d",&a, &b, &c);
• if(a > b)
• {
• if(a > c)
• {
• printf("a is the greatest");
• }
• else
• {
• printf("c is the greatest");
• }
• }
• else
• {
• if(b > c)
• {
• printf("b is the greatest");
• }
• else
• {
• printf("c is the greatest");
• }
• }
• }
If else ladder
• The expression is tested from the top(of the ladder) downwards.
• As soon as a true condition is found, the statement associated with it is
executed.
• Syntax :
if(expression1) expression means - condition
{
statement block1;
}
else if(expression2)
{
statement block2;
}
else if(expression3 )
{
statement block3;
}
else
default statement;
• #include <stdio.h>

• void main( )
• {
• int a;
• printf("Enter a number...");
• scanf("%d", &a);
• if(a%5 == 0 && a%8 == 0)
• {
• printf("Divisible by both 5 and 8");
• }
• else if(a%8 == 0)
• {
• printf("Divisible by 8");
• }
• else if(a%5 == 0)
• {
• printf("Divisible by 5");
• }
• else
• {
• printf("Divisible by none");
• }
• }
Switch Statement

• C switch statement is used when you have multiple possibilities for the if statement.
• Switch case will allow you to choose from multiple options.
• After the end of each block it is necessary to insert a break statement because if the
programmers do not use the break statement, all consecutive blocks of codes will get
executed from every case
• switch(variable)
• {
• case 1:
• //execute your code
• break;

• case n:
• //execute your code
• break;

• default:
• //execute your code
• break;
• }
• #include<stdio.h>
• void main()
• {
• int a;
• printf("Please enter a no between 1 and 5: ");
• scanf("%d",&a);
• switch(a)
• {
• case 1:
• printf("You chose One");
• break;
• case 2:
• printf("You chose Two");
• break;
• case 3:
• printf("You chose Three");
• break;
• case 4:
• printf("You chose Four");
• break;
• case 5:
• printf("You chose Five.");
• break;
• default :
• printf("Invalid Choice. Enter a no between 1 and 5");
• break;
• }
– Repetitive Statements: Looping is a powerful technique through
which a group of statements are executed repeatedly until
specified condition is satisfied. These are also known as iterative
statement. ‘C’ provides three types of loop control structure.

• For Loop : It is used when the programmer known in
advance the number of times a statement or block of
statement will be executed.
• The syntax of for loop is:
• for (initialization; condition; update Statement)
• { // codes
• }
for(a=5; a<=10; a=a+2)
for(a=10; a>=1;a--)
• While Loop: The while loop is used when the
number of times the loop has to be executed
is not known in advance. The syntax of while
loop is
• Initialization;
• while (;)
• {
• Statement;
• inc/dec;
• }
Do while syntax

• do
• {
– Statemets—1
– Staements – 2
– Statements – 3
– Inc/dec(++ , --)
}while(condition);
Transfer Statements

• It is also called as jump statements.


• It allow to interrupt the normal execution of statements.
• Break
• Goto – this statement is used to transfer control from one
point in a program to another. It allows you to jump to a
labeled section of code within the same function, bypassing
normal sequential execution.unconditional control transfer
statements.
– Goto - Label(Destination)
– Two type : forward jump
» Backward Jump

• Continue
• #include<stdio.h>
• void main()
• { int age;
• g: //label name
• printf("you are Eligible\n");
• s: //label name
• printf("you are not Eligible");
• printf("Enter you age:");
• scanf("%d", &age);
• if(age>=18)
• goto g; //goto label g
• else
• goto s; //goto label s
• }
• #include <stdio.h>
• int main()
• {
• int i = 0;
• start:
• if (i < 5)
• {
• printf("%d ", i);
• i++;
• goto start; // This jumps back to the 'start' label }
• return 0;
• }
• Goto label;
– --
– ---
– ---
– Lable : statement, xyz
– Goto xyz;

– Xyz:
continue statement
• The continue statement in C language is used to bring the program control to the
beginning of the loop.
• The continue statement skips some lines of code inside the loop and continues with the
next iteration.
• It is mainly used for a condition so that we can skip some code for a particular
condition.
Example :
• #include<stdio.h>
• int main(){
• int i=1;//initializing a local variable
• //starting a loop from 1 to 10
• for(i=1;i<=10;i++){
• if(i==5){//if value of i is equal to 5, it will continue the loop
• continue;
• }
• printf("%d \n",i);
• }//end of for loop
• return 0;
• }
• #include <stdio.h>
int main()
{
int i;
for(i=0;i<20;i++)
{
if(i%2==0)
{
continue;
}
printf("%d ",i);
}
}
• #include <stdio.h>
void checkEven(int num)
{
if (num%2 == 0)
goto even_no;
else
goto odd_no;
even_no:
printf("The number is even.\t");
goto even_no;
odd_no:
printf("The number is odd.\t");
goto odd_no;
}
void main() {
int i = 10;
checkEven(i);
}
• #include <stdio.h>
• int main ()
• {
• /* local variable definition */
• int a = 10;
• LOOP:
• do
• {
• if( a == 15)
• { /* skip the iteration */
• a = a + 1;
• //goto
• //LOOP;
• continue;
• //break;
• }
• printf("value of a: %d\n", a);
• a++;
• }while( a < 15 );
• return 0;

You might also like