PPC - Unit 2

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

PROGRAMMING PRINCIPLES USING C

Unit – II
Decision Making and Branching:  Decision making with If, simple IF, IF ELSE, nested IF ELSE,
ELSE IF ladder, switch, and GOTO statement. 
Decision Making and Looping: While, Do-While, For, Jumps in loops.

27/03/2023 Lecture Notes - Dr. Persis 1


Decision making with If - simple IF, IF ELSE, nested IF ELSE, ELSE IF ladder

 Branching is deciding what actions to take and looping is deciding how many times to take a
certain action.
 The if statement controls whether a program enters a section of code or not based on the
condition. One of the important functions of the if statement is that it allows the program to
select an action based upon the user's input. The program chooses to follow one branch or
another.

There are four different types of if statements in C.


They are:

1. Simple if Statement
2. if-else Statement
3. Nested if-else Statement
4. else-if Ladder
27/03/2023 Lecture Notes - Dr. Persis 2
1. Simple if statement
Definition: If the test expression is evaluated to true, the statement block will get
executed, or it will get skipped. Flow chart
The basic format of the if Statement is:
Syntax
if(test_expression)
{
statement 1;
statement 2;
...
}

27/03/2023 Lecture Notes - Dr. Persis 3


Example – Simple If Statement

#include<stdio.h>
#include<conio.h>

void main()
{
int a = 15,
b = 20;
if (b > a)
{
printf("b is greater");
}
getch();
}
27/03/2023 Lecture Notes - Dr. Persis 4
2. If…Else statement
Definition: "If else statements" executes some statement code block if the expression is true;
otherwise, it executes the else statement code block.
• The basic format of if else Statement is: Flow chart
• Syntax:

if(test_expression)
{
//execute your code
}
Else
{
//execute your code
}
27/03/2023 Lecture Notes - Dr. Persis 5
Example – If…Else Statement

#include<stdio.h>
if (a > b)
#include<conio.h>
{
printf("\n a is greater");
void main()
}
{ int a, b;
else
printf("Please enter the value for a:");
{
scanf("%d", &a);
printf("\n b is greater");
printf("\nPlease the value for b:");
}
scanf("%d", &b);
getch();
}

27/03/2023 Lecture Notes - Dr. Persis 6


3. Nested If…Else statement

Definition: Nested "if else statements" means the use of conditional statements inside
another conditional statement.
• The basic format of the Nested if-else statement is:
• Syntax:

if(test_expression one)
{
if(test_expression two)
{
//Statement block executes when the test expression two
is true.
}
}
else
{
//else statement block
27/03/2023 } Lecture Notes - Dr. Persis 7
Flow chart

27/03/2023 Lecture Notes - Dr. Persis 8


Example – Nested If…Else Statement
#include<stdio.h>
#include<conio.h>
void main()
{
int x=20,y=30;
if(x==20)
{
if(y==30)
{
printf("value of x is 20, and value of y is 30.");
}
}
getch();
27/03/2023
} Lecture Notes - Dr. Persis 9
4. If…Else Ladder Definition: if-else-if ladder helps user decide from among
multiple options. The statements are executed from the top
down.  Flow chart
• The basic format of the if-else ladder is:
• Syntax:
if(condition_expression_One)
 {
    statement1;
} else if (condition_expression_Two) 
{
    statement2;
} else if (condition_expression_Three) 
{
    statement3;
} else
 {
    statement4;
}
27/03/2023 Lecture Notes - Dr. Persis 10
Example –If…Else Ladder Statement

#include<stdio.h>
else if (marks >= 50 && marks < 70)
#include<conio.h>
{
       printf("YOUR GRADE : C\n");
int main()
    }
{
else
    int marks;
{
    printf("Enter marks between 0-100\n");
       printf("YOUR GRADE : Failed\n");
    scanf("%d", &marks);
    }
   
         getch();
    if(marks >= 90)
}
{
       printf("YOUR GRADE : A\n");
    }
else if (marks >= 70 && marks < 90)
{
       printf("YOUR GRADE : B\n");
    }
27/03/2023 Lecture Notes - Dr. Persis 11
Switch statement
Flow Chart
Definition: A switch statement allows a variable to be tested for
equality against a list of values. Each value is called a case, and
the variable being switched on is checked for each switch case.

Syntax
The syntax for a switch is 
switch (expression)
​{
case constant1:
// statements
break;
case constant2:
// statements
break;
...
default:
// default statements
} 27/03/2023 Lecture Notes - Dr. Persis 12
program to read weekday number and print weekday name using switch.

#include <stdio.h>
#include <conio.h>
case 4: printf("Thursday");
break;
Void main()
case 5: printf("Friday");
{
break;
int wDay;
case 6: printf("Saturday");
printf("Enter weekday number (0-6):");
break;   
scanf("%d",&wDay);
default: printf("Invalid weekday number");
switch(wDay)
printf("\n");
{
case 0: printf("Sunday");
getch();
break;
}
case 1: printf("Monday");
break; Output
case 2: printf("Tuesday"); Enter weekday number (0-6): 3
break; Wednesday
case 3: printf("Wednesday"); Enter weekday number (0-6): 9
break; Invalid weekday number.
27/03/2023 Lecture Notes - Dr. Persis 13
goto statement

Definition: The goto is used to transfer the program control to a predefined label.  It is a jump statement which is
sometimes also referred to as unconditional jump statement. The goto statement can be used to jump from anywhere
to anywhere

Syntax Flow Chart

The syntax for a goto statement is−


goto label;
.. .
label: statement;

27/03/2023 Lecture Notes - Dr. Persis 14


program to print a number table using goto.

#include <stdio.h>  
#include <conio.h>  
Enter the number table you want to print?10

10 x 1 = 10
int main()    10 x 2 = 20
{   10 x 3 = 30
  int num, i=1;    10 x 4 = 40
  printf("Enter the number table you want to print?");    10 x 5 = 50
  scanf("%d",&num);   10 x 6 = 60
  table:   printf("%d x %d = %d\n",num, i, num*i);   10 x 7 = 70
10 x 8 = 80
  i++;  
10 x 9 = 90
  if(i<=10)   10 x 10 = 100
  goto table;    
}  

27/03/2023 Lecture Notes - Dr. Persis 15


Advantages of using goto statement:

• It can alter the normal sequence of the program execution


• It has the power to jump to any part of program. 

Disadvantages of using goto statement:

• It makes the program logic very complex.


• The task of analyzing and verifying the correctness of programs becomes very difficult if used inside loops.
• Use of goto can be simply avoided using break and continue statements.

27/03/2023 Lecture Notes - Dr. Persis 16


Decision Making and Looping: While, Do-While, For, Jumps in loops.

while loop - Definition: - A while loop in C programming repeatedly


executes a target statement as long as a given condition is true. When the
condition becomes false, the program control passes to the following line Flow Chart
immediately.

While loop is also known as a pre-tested loop. It tests the condition


before executing the loop body.

Syntax
The syntax of a while loop is −
while(condition)
{
statement(s);
}
27/03/2023 Lecture Notes - Dr. Persis 17
Example Program to print numbers from 10 to 19 using while loop

#include <stdio.h> Output


#include <conio.h>
void main () value of a: 10
value of a: 11
{
value of a: 12
int a = 10; value of a: 13
value of a: 14
while( a < 20 ) value of a: 15
{ value of a: 16
value of a: 17
printf("value of a: %d\n", a);
value of a: 18
a++; value of a: 19
}
getch();
}
27/03/2023 Lecture Notes - Dr. Persis 18
Do … while loop - Definition: - The do…while loop is a Flow Chart
variant of the while loop and it is almost same as
while loop, except the condition, is checked at the end of
the loop, instead of at the beginning. 

Do…while loop is known as a post-tested loop. It tests the


condition after executing the loop body.

Syntax
The syntax of a do...while loop is −
do
{
statement(s);
}
while( condition );

27/03/2023 Lecture Notes - Dr. Persis 19


Example Program to print numbers from 10 to 19 using do…while loop

#include <stdio.h> Output


int main ()
{ value of a: 10
int a = 10; value of a: 11
do value of a: 12
{ value of a: 13
printf("value of a: %d\n", a); value of a: 14
a = a + 1; value of a: 15
} value of a: 16
while( a < 20 ); value of a: 17
getch(); value of a: 18
} value of a: 19

27/03/2023 Lecture Notes - Dr. Persis 20


FOR loop - Definition: The for loop in C language is used to Flow Chart
iterate the statements or a part of the program several times.
When we know exactly how many times we want to loop
through a block of code, we can use the for loop instead of
a while loop.
It is frequently used to traverse the data structures like the array
and linked list.

syntax

The syntax of a for loop is −


for ( init; condition; increment )
{
statement(s);
}
27/03/2023 Lecture Notes - Dr. Persis 21
working of FOR loop - for ( init; condition; increment )

•The init step is executed first, and only once. This step allows you to declare and initialize any loop control
variables.

•Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop
does not execute and the flow of control jumps to the next statement just after the 'for' loop.

•After the body of the 'for' loop executes, the flow of control jumps back up to the increment statement. This
statement allows you to update any loop control variables

•The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop,
then increment step, and then again condition). After the condition becomes false, the 'for' loop terminates.

27/03/2023 Lecture Notes - Dr. Persis 22


#include<stdio.h>
#include<conio.h>
Output

void main () value of a: 10


{ value of a: 11
int a; value of a: 12
/* for loop execution */ value of a: 13
value of a: 14
for( a = 10; a < 20; a = a + 1 ) value of a: 15
{ value of a: 16
printf("value of a: %d\n", a); value of a: 17
} value of a: 18
getch(); value of a: 19
}

27/03/2023 Lecture Notes - Dr. Persis 23


#include <stdio.h> Output
#include <conio.h>
Hello World
void main() Hello World
{ Hello World
    int i=0; Hello World
      Hello World
    for (i = 1; i <= 10; i++) Hello World
    { Hello World
        printf( "Hello World\n");    Hello World
    } Hello World
  Hello World
    getch();
}

27/03/2023 Lecture Notes - Dr. Persis 24


Program to Print Upper Case English Alphabets

#include<stdio.h>
#include<conio.h>
Output
void main() ABCDEFGHIJKLMNOPQRSTUVWXYZ
{
char c;
for (c = 'A'; c <= 'Z’; c++)
printf("%c ", c);
getch();
}

You might also like