101903/CO200G
PROGRAMMING IN C
Module II
CONTROL FLOW STATEMENTS
2
MODULE II - SYLLABUS
Program Basics
• Control Flow Statements: If Statement, Switch
Statement, Unconditional Branching using goto
statement, While Loop, Do While Loop, For Loop,
Break and Continue statements. (Simple programs
covering control flow)
Dept of IT, RSET
CO2: Develop readable* C programs with branching and
looping statements, which uses Arithmetic, Logical,
Relational or Bitwise operators.
COURSE OUTCOME
Conditional Statements
1. Why Conditional Statements
2. if
3. if – else
4. Nested if - else
CONTENTS
• In everyday life, we make many decisions
• If it is raining, take an umbrella when you go
out
• If a polygon has 4 sides, it is a quadrilateral
• If some condition is true, do some action –
use conditional statements
Why Conditional Statements
• Used for branching
• A logical test is done, depending on the outcome of
the test, one of the several possible actions are
carried out
• Logical expressions are used to perform the test
• Logical expressions evaluates to true or false
• Also called selection, because a group of statements
is selected from several available groups
Conditional Statements
• To form logical expressions we use
– Four relational operators, <, <=, >, >=
– Two equality operators, ==, !=
– Logical connectives (logical operators), && (AND), ||
(OR) and ! (unary negation operator)
• Logical connectives are used to combine logical
expressions to form a complex expression
Logical Expressions
Conditional Statements
Conditional
Statements
if – else
Nested if –
else
Switch Goto
• Used to express decisions
• Conduct a logical test and perform an action if the
outcome of the test is true
If
if (expression)
{
statement 1;
}
statement x;
Syntax in C
Algorithm
Step no (e.g. 4) : If the condition <condition 1>
Step 4.1 : Then <statements>
Block Executed if the expression
is True
Step 1 : Start
Step 2 : Read the number
Step 3 : Assign the value of number into variable num
Step 4 : If the condition num % 6 is 0 then
Step 4.1 : Then print num is divisible by 6
Step 5 : Stop
Algorithm - Check whether a number is divisible by 6
Program to check if a number divisible by 6
#include <stdio.h>
int main()
{
int num;
printf("Enter a number");
scanf("%d", &num);
if(num%6 == 0)
{
printf("%d is divisible by 6",num);
}
return 0;
}
If you want to execute some code if a condition is
true and another code if a condition is false
If – Else
Syntax
if (expression)
{
statement 1;
}
else
{
statement 2;
}
statement x;
Block Executed if the expression
is True
Block Executed if the expression
is False
Algorithm
Step no (e.g. 4) : If the condition <condition 1>
Step 4.1 : Then <statements>
Step no (e.g. 5) : Otherwise
Step 5.1 : <statements>
Step 1 : Start
Step 2 : Read the numbers
Step 3 : Assign the value of number into variable num
Step 4 : If the condition num % 6 is 0
Step 4.1 : Then print num is divisible by 6
Step 5 : Otherwise
Step 5.1: Print num is not divisible by 6
Step 6 : Stop
Algorithm - Check whether a number is divisible by 6 or not
#include <stdio.h>
int main()
{
int num;
printf("Enter a number");
scanf("%d", &num);
if(num%6 == 0)
printf("%d is divisible by 6",num);
else
printf("%d is not divisible by 6",num);
return 0;
}
Program to check if a number divisible by 6 or not
Step 1 : Start
Step 2 : Read the number
Step 3 : Assign the value of number into variable num
Step 4 : If the condition num % 2 is 0
Step 4.1 : Then print the number num is even
Step 5 : Otherwise
Step 5.1 : Print the number num is odd
Step 6 : Stop
Algorithm - Check whether a number is even or odd
#include <stdio.h>
int main()
{
int num;
printf("Enter the number to check:");
scanf("%d",&num);
if(num%2 == 0)
printf("The number %d is even",num);
else
printf("The number %d is odd",num);
return 0;
}
Program to check if a number is even or odd
• Conditional operator ?: is equivalent to a simple if-
else structure
• It makes use of an expression that is either true or
false
• An appropriate value is selected depending on the
outcome of the expression
• A=(3<4)?(3+4):(8+2)
Conditional Operator
• It is possible to nest if - else statements one within
another
• Nested if – else statements can take several
different forms
• Multi-way decision
Nested If – Else Statements
If – Else If – Else (form 1)
if (expression 1)
{
statement 1;
}
else if (expression 2)
{
statement 2;
}
…
…
else if (expression n)
{
statement n;
}
else
{
statement n+1;
}
statement x;
Syntax
Algorithm
Step no (e.g. 4) : If the condition <condition 1>
Step 4.1 : Then <statements>
Step no (e.g. 5) : Otherwise If the condition <condition 2>
Step 5.1 : <statements>
Step no (e.g. 6) : Otherwise If the condition <condition n>
Step 6.1 : <statements>
Otherwise
Step 7.1 : <statements>
Step 1 : Start
Step 2 : Read the three numbers
Step 3 : Assign the value of first number into variable num1, the
value of second number into variable num2 and the value of third
number into variable num3
Step 4 : If the condition num1 is larger than num2 and num1 is
larger than num3
Step 4.1 : Then print num1 is the largest of three numbers
Step 5 : Otherwise If the condition num2 is larger than num1 and
num2 is larger than num3
Step 5.1 : Then print num2 is the largest of three numbers
Step 6 : Otherwise
Step 6.1 : Print num3 is the largest of three numbers
Step 7 : Stop
Algorithm - Largest of 3 numbers
#include <stdio.h>
int main()
{
int num1, num2, num3;
printf("Enter three numbers");
scanf("%d,%d,%d", &num1, &num2, &num3);
if(num1>num2 && num1>num3)
printf("%d is the largest of the three",num1);
else if(num2>num1 && num2>num3)
printf("%d is the largest of the three",num2);
else
printf("%d is the largest of the three",num3);
return 0;
}
Program to find the largest of three numbers
Algo2: to find the largest of three numbers
1.Start
2. Read the three numbers to be compared, as A, B and C. 3.
3.Check if A is greater than B.
3.1 check if A is greater than C.
3.1.1 Print 'A' as the greatest number.
3.2 otherwise,print 'C' as the greatest number.
3.3 Check if B is greater than C.
3.3.1 If true, print 'B' as the greatest number.
3.4 Otherwise, print 'C' as the greatest number.
4. End
Step 1: Start
Step 2: Read the year
Step 3: Assign the value into variable y
Step 4: If the condition y % 400 is 0
Step 4.1: Then print y is a leap year
Step 5: Otherwise If the condition y % 100 is 0
Step 5.1: Then print y is not a leap year
Step 6: Otherwise If the condition y % 4 is 0
Step 6.1: Then print y is a leap year
Step 7: Otherwise
Step 7.1: print y is not a leap year
Step 8 : Stop
Algorithm - Leap Year or not
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
// leap year if perfectly divisible by 400
if (year % 400 == 0)
{
printf("%d is a leap year.", year);
}
//not a leap year if divisible by 100
//but not divisible by 400
else if (year % 100 == 0)
{
printf("%d is not a leap year.", year);
}
Program to check if a given year is leap year or not
Cont…
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0)
{
printf("%d is a leap year.", year);
}
// all other years are not leap years
else
{
printf("%d is not a leap year.", year);
}
return 0;
}
Nested If – Else Statement (form 2)
if (expression1)
{
if (expression2)
statement 1;
else
{
statement 2;
}
}
else if (expression3)
{
statement 3;
}
else
{
statement 4;
}
Nested If - Else
Step no (e.g. 4) : If the condition <condition 1>
Step 4.1 : If the condition <condition 2>
Step 4.1.1 : <statement 1>
step 4.2: Otherwise
Step 4.2.1 : <statement 2>
Otherwise
Step 5.1 : If the condition <condition 3>
Step 5.1.1 : <statement 3>
step 5.2: Otherwise
Step 5.2.1 : <statement 4>
Step 1 : Start
Step 2 : Read the three numbers
Step 3 : Assign the value of first number into variable num1,
the value of second number into variable num2 and the value
of third number into variable num3
Step 4 : If the condition num1 is larger than num2
Step 4.1: Then if num1 is larger than num3
Step 4.1.1: Print num1 is the largest of three
numbers
Step 4.2: Otherwise
Step 4.2.1: Print num3 is the largest of three
numbers
Algorithm - Largest of 3 numbers
Step 5: Otherwise
Step 5.1 : If num2 is larger than num3
Step 5.1.1: Print num2 is the largest of three
numbers
Step 6 : Otherwise
Step 6.1 : Print num3 is the largest of three
numbers
Step 7 : Stop
Flowchart - largest of three numbers
Program to find the largest of three numbers
#include <stdio.h>
int main()
{
int num1, num2, num3;
printf("Enter three numbers");
scanf("%d,%d,%d",&num1,&num2,&num3);
if(num1>num2)
if(num1>num3)
printf("%d is the largest of the three",num1);
else
printf("%d is the largest of the three",num3);
else
if(num2>num3)
printf("%d is the largest of the three",num2);
else
printf("%d is the largest of the three",num3);
return 0;
}
Some Other Forms of Nested - if
if (expression1)
{
if (expression2)
statement 1;
else
{
statement 2;
}
}
Else clause is associated with the closest preceding
unmatched if
if (expression1)
{
if (expression2)
statement 1;
}
else
{
statement 2;
}
Summary
1. Schaum Series, Gottfried B. S., Tata McGraw Hill,
Programming with C
2. Brian W. Kernighan and Dennis M. Ritchie, Pearson,
C Programming Language
REFERENCES
THANK YOU

Module 2_Conditional Statements.pptx

  • 1.
    101903/CO200G PROGRAMMING IN C ModuleII CONTROL FLOW STATEMENTS
  • 2.
    2 MODULE II -SYLLABUS Program Basics • Control Flow Statements: If Statement, Switch Statement, Unconditional Branching using goto statement, While Loop, Do While Loop, For Loop, Break and Continue statements. (Simple programs covering control flow) Dept of IT, RSET
  • 3.
    CO2: Develop readable*C programs with branching and looping statements, which uses Arithmetic, Logical, Relational or Bitwise operators. COURSE OUTCOME
  • 4.
  • 5.
    1. Why ConditionalStatements 2. if 3. if – else 4. Nested if - else CONTENTS
  • 6.
    • In everydaylife, we make many decisions • If it is raining, take an umbrella when you go out • If a polygon has 4 sides, it is a quadrilateral • If some condition is true, do some action – use conditional statements Why Conditional Statements
  • 7.
    • Used forbranching • A logical test is done, depending on the outcome of the test, one of the several possible actions are carried out • Logical expressions are used to perform the test • Logical expressions evaluates to true or false • Also called selection, because a group of statements is selected from several available groups Conditional Statements
  • 8.
    • To formlogical expressions we use – Four relational operators, <, <=, >, >= – Two equality operators, ==, != – Logical connectives (logical operators), && (AND), || (OR) and ! (unary negation operator) • Logical connectives are used to combine logical expressions to form a complex expression Logical Expressions
  • 9.
    Conditional Statements Conditional Statements if –else Nested if – else Switch Goto
  • 10.
    • Used toexpress decisions • Conduct a logical test and perform an action if the outcome of the test is true If
  • 11.
    if (expression) { statement 1; } statementx; Syntax in C Algorithm Step no (e.g. 4) : If the condition <condition 1> Step 4.1 : Then <statements> Block Executed if the expression is True
  • 12.
    Step 1 :Start Step 2 : Read the number Step 3 : Assign the value of number into variable num Step 4 : If the condition num % 6 is 0 then Step 4.1 : Then print num is divisible by 6 Step 5 : Stop Algorithm - Check whether a number is divisible by 6
  • 13.
    Program to checkif a number divisible by 6 #include <stdio.h> int main() { int num; printf("Enter a number"); scanf("%d", &num); if(num%6 == 0) { printf("%d is divisible by 6",num); } return 0; }
  • 14.
    If you wantto execute some code if a condition is true and another code if a condition is false If – Else
  • 15.
    Syntax if (expression) { statement 1; } else { statement2; } statement x; Block Executed if the expression is True Block Executed if the expression is False
  • 16.
    Algorithm Step no (e.g.4) : If the condition <condition 1> Step 4.1 : Then <statements> Step no (e.g. 5) : Otherwise Step 5.1 : <statements>
  • 17.
    Step 1 :Start Step 2 : Read the numbers Step 3 : Assign the value of number into variable num Step 4 : If the condition num % 6 is 0 Step 4.1 : Then print num is divisible by 6 Step 5 : Otherwise Step 5.1: Print num is not divisible by 6 Step 6 : Stop Algorithm - Check whether a number is divisible by 6 or not
  • 18.
    #include <stdio.h> int main() { intnum; printf("Enter a number"); scanf("%d", &num); if(num%6 == 0) printf("%d is divisible by 6",num); else printf("%d is not divisible by 6",num); return 0; } Program to check if a number divisible by 6 or not
  • 19.
    Step 1 :Start Step 2 : Read the number Step 3 : Assign the value of number into variable num Step 4 : If the condition num % 2 is 0 Step 4.1 : Then print the number num is even Step 5 : Otherwise Step 5.1 : Print the number num is odd Step 6 : Stop Algorithm - Check whether a number is even or odd
  • 20.
    #include <stdio.h> int main() { intnum; printf("Enter the number to check:"); scanf("%d",&num); if(num%2 == 0) printf("The number %d is even",num); else printf("The number %d is odd",num); return 0; } Program to check if a number is even or odd
  • 21.
    • Conditional operator?: is equivalent to a simple if- else structure • It makes use of an expression that is either true or false • An appropriate value is selected depending on the outcome of the expression • A=(3<4)?(3+4):(8+2) Conditional Operator
  • 22.
    • It ispossible to nest if - else statements one within another • Nested if – else statements can take several different forms • Multi-way decision Nested If – Else Statements
  • 23.
    If – ElseIf – Else (form 1)
  • 24.
    if (expression 1) { statement1; } else if (expression 2) { statement 2; } … … else if (expression n) { statement n; } else { statement n+1; } statement x; Syntax
  • 25.
    Algorithm Step no (e.g.4) : If the condition <condition 1> Step 4.1 : Then <statements> Step no (e.g. 5) : Otherwise If the condition <condition 2> Step 5.1 : <statements> Step no (e.g. 6) : Otherwise If the condition <condition n> Step 6.1 : <statements> Otherwise Step 7.1 : <statements>
  • 26.
    Step 1 :Start Step 2 : Read the three numbers Step 3 : Assign the value of first number into variable num1, the value of second number into variable num2 and the value of third number into variable num3 Step 4 : If the condition num1 is larger than num2 and num1 is larger than num3 Step 4.1 : Then print num1 is the largest of three numbers Step 5 : Otherwise If the condition num2 is larger than num1 and num2 is larger than num3 Step 5.1 : Then print num2 is the largest of three numbers Step 6 : Otherwise Step 6.1 : Print num3 is the largest of three numbers Step 7 : Stop Algorithm - Largest of 3 numbers
  • 27.
    #include <stdio.h> int main() { intnum1, num2, num3; printf("Enter three numbers"); scanf("%d,%d,%d", &num1, &num2, &num3); if(num1>num2 && num1>num3) printf("%d is the largest of the three",num1); else if(num2>num1 && num2>num3) printf("%d is the largest of the three",num2); else printf("%d is the largest of the three",num3); return 0; } Program to find the largest of three numbers
  • 28.
    Algo2: to findthe largest of three numbers 1.Start 2. Read the three numbers to be compared, as A, B and C. 3. 3.Check if A is greater than B. 3.1 check if A is greater than C. 3.1.1 Print 'A' as the greatest number. 3.2 otherwise,print 'C' as the greatest number. 3.3 Check if B is greater than C. 3.3.1 If true, print 'B' as the greatest number. 3.4 Otherwise, print 'C' as the greatest number. 4. End
  • 29.
    Step 1: Start Step2: Read the year Step 3: Assign the value into variable y Step 4: If the condition y % 400 is 0 Step 4.1: Then print y is a leap year Step 5: Otherwise If the condition y % 100 is 0 Step 5.1: Then print y is not a leap year Step 6: Otherwise If the condition y % 4 is 0 Step 6.1: Then print y is a leap year Step 7: Otherwise Step 7.1: print y is not a leap year Step 8 : Stop Algorithm - Leap Year or not
  • 30.
    #include <stdio.h> int main(){ int year; printf("Enter a year: "); scanf("%d", &year); // leap year if perfectly divisible by 400 if (year % 400 == 0) { printf("%d is a leap year.", year); } //not a leap year if divisible by 100 //but not divisible by 400 else if (year % 100 == 0) { printf("%d is not a leap year.", year); } Program to check if a given year is leap year or not
  • 31.
    Cont… // leap yearif not divisible by 100 // but divisible by 4 else if (year % 4 == 0) { printf("%d is a leap year.", year); } // all other years are not leap years else { printf("%d is not a leap year.", year); } return 0; }
  • 32.
    Nested If –Else Statement (form 2) if (expression1) { if (expression2) statement 1; else { statement 2; } } else if (expression3) { statement 3; } else { statement 4; }
  • 33.
    Nested If -Else Step no (e.g. 4) : If the condition <condition 1> Step 4.1 : If the condition <condition 2> Step 4.1.1 : <statement 1> step 4.2: Otherwise Step 4.2.1 : <statement 2> Otherwise Step 5.1 : If the condition <condition 3> Step 5.1.1 : <statement 3> step 5.2: Otherwise Step 5.2.1 : <statement 4>
  • 34.
    Step 1 :Start Step 2 : Read the three numbers Step 3 : Assign the value of first number into variable num1, the value of second number into variable num2 and the value of third number into variable num3 Step 4 : If the condition num1 is larger than num2 Step 4.1: Then if num1 is larger than num3 Step 4.1.1: Print num1 is the largest of three numbers Step 4.2: Otherwise Step 4.2.1: Print num3 is the largest of three numbers Algorithm - Largest of 3 numbers
  • 35.
    Step 5: Otherwise Step5.1 : If num2 is larger than num3 Step 5.1.1: Print num2 is the largest of three numbers Step 6 : Otherwise Step 6.1 : Print num3 is the largest of three numbers Step 7 : Stop
  • 36.
    Flowchart - largestof three numbers
  • 37.
    Program to findthe largest of three numbers #include <stdio.h> int main() { int num1, num2, num3; printf("Enter three numbers"); scanf("%d,%d,%d",&num1,&num2,&num3); if(num1>num2) if(num1>num3) printf("%d is the largest of the three",num1); else printf("%d is the largest of the three",num3); else if(num2>num3) printf("%d is the largest of the three",num2); else printf("%d is the largest of the three",num3); return 0; }
  • 38.
    Some Other Formsof Nested - if if (expression1) { if (expression2) statement 1; else { statement 2; } } Else clause is associated with the closest preceding unmatched if if (expression1) { if (expression2) statement 1; } else { statement 2; }
  • 39.
  • 40.
    1. Schaum Series,Gottfried B. S., Tata McGraw Hill, Programming with C 2. Brian W. Kernighan and Dennis M. Ritchie, Pearson, C Programming Language REFERENCES
  • 41.

Editor's Notes

  • #3 School of EECS, WSU