0% found this document useful (0 votes)
15 views23 pages

CHP - 4 - Flow Control - If

Chapter 4 discusses flow control in programming, specifically focusing on decision-making structures such as 'if', 'if-else', and 'if-else if' statements in C programming. It explains the syntax and behavior of these statements, including how boolean expressions are evaluated and the use of nested if statements. The chapter also provides examples to illustrate the concepts and highlights the importance of control flow in programming.

Uploaded by

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

CHP - 4 - Flow Control - If

Chapter 4 discusses flow control in programming, specifically focusing on decision-making structures such as 'if', 'if-else', and 'if-else if' statements in C programming. It explains the syntax and behavior of these statements, including how boolean expressions are evaluated and the use of nested if statements. The chapter also provides examples to illustrate the concepts and highlights the importance of control flow in programming.

Uploaded by

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

PROGRAMMING I

CHAPTER 4.
Flow Control - If

CENGİZ RİVA
Flow Control

• Flow control in programming language is also known as


decision making.
• Decision making structures require that the programmer
specify one or more conditions to be evaluated or tested by the
program.
• if the condition is determined to be true, a statement or
statements underneath the test condition to be executed and
optionally, other statements to be executed if the condition is
determined to be false.
Flow Control

• Following is the general form of


a typical decision making
structure found in most of the
programming languages.
• C programming language
assumes any non-zero and non-
null values as true, and if it is
either zero or null, then it is
assumed as false value.
• C programming language
provides following types of
decision making statements.
Control Flow

• The control-flow of a language specify the order in which


computations are performed.
• C offers following control flow keywords:
• If
• If-Else
• Else-If
• Switch
• While
• For
• Do-While
• Break and Continue
• Goto and labels
if Statement
• An if statement consists of a
boolean expression followed
by one or more statements.
• If the boolean expression
evaluates to true, then the
block of code inside the if
statement will be executed.
• If boolean expression
evaluates to false, then the
first set of code after the end
of the if statement (after the
closing curly brace) will be
executed.
if Statement
• An if statement consists of a boolean expression followed by
one or more statements.
Syntax:
• The syntax of an if statement in C programming language is:

if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true */
}

• C programming language assumes any non-zero and non-null


values as true and if it is either zero or null then it is assumed as
false value.
if Statement
• Example:
#include <stdio.h>
int main ()
{
int a = 10;
/* check the boolean condition using if statement */
if( a < 20 )
{
/* if condition is true then print the following */
printf("a is less than 20\n" );
}
printf("value of a is : %d\n", a);
return 0;
}
if Statement
• C programming language assumes any non-zero and non-null
values as true and if it is either zero or null then it is assumed
as false value.
#include <stdio.h>
int main ()
{
if(5) printf("condition is true\n" );
return 0;
}

#include <stdio.h>
int main ()
{
if(0) printf("condition is true\n");
return 0;
}
if Statement
• There can be more than one expression combined with logical
or arithmetic operators inside the condition.
#include <stdio.h>
void main()
{
int a = 5;
int b = 20;
int c = 15;
if ( a < b && b > c) printf(" 1st AND opr - Condition is true\n");
if ( a > b && b > c ) printf(" 2nd AND opr - Condition is true\n");
if ( a > b || b > c ) printf(" OR opr - Condition is true\n");
if (! (a > b)) printf(" NOT opr - Condition is true\n");
}
if...else Statement
• An if statement can be followed
by an optional else statement,
which executes when the
boolean expression is false.
• If the boolean expression
evaluates to true, then the if
block of code will be executed,
otherwise else block of code
will be executed.
• C programming language
assumes any non-zero and non-
null values as true and if it is
either zero or null then it is
assumed as false value.
if...else Statement
• An if statement can be followed by an optional else
statement, which executes when the boolean expression is
false.
Syntax:
• The syntax of an if...else statement in C programming
language is:
if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true */
}
else
{
/* statement(s) will execute if the boolean expression is false */
}
if...else Statement
• Example:
#include <stdio.h>
int main ()
{
int a = 100;
/* check the boolean condition */
if( a < 20 )
{
/* if condition is true then print the following */
printf("a is less than 20\n" );
}
else
{
/* if condition is false then print the following */
printf("a is not less than 20\n" );
}
printf("value of a is : %d\n", a);
return 0;
}
if...else Statement

• Output of the program will be:


The if...else if...else Statement
• An if statement can be followed by an optional else if...else
statement, which is very useful to test various conditions using
single if...else if statement.
• When using if , else if , else statements there are few points to
keep in mind:
• An if can have zero else if or too many else if's and they
must come before the else.
• Once an else if succeeds, none of the remaining else if's or
else's will be tested.
The if...else if...else Statement
• Syntax:
• The syntax of an if...else if...else statement in C programming
language is:
if(boolean_expression 1)
{
/* Executes when the boolean expression 1 is true */
}
else if( boolean_expression 2)
{
/* Executes when the boolean expression 2 is true */
}
else if( boolean_expression 3)
{
/* Executes when the boolean expression 3 is true */
}
else
{
/* executes when the none of the above condition is true */
}
The if...else if...else Statement
#include <stdio.h>
int main ()
{
int a = 100;
if( a == 10 )
{
printf("Value of a is 10\n" );
}
else if( a == 20 )
{
printf("Value of a is 20\n" );
}
else if( a == 30 )
{
printf("Value of a is 30\n" );
}
else
{
printf("None of the values is matching\n" );
}
printf("Exact value of a is: %d\n", a );
}
The if...else if...else Statement
• Output of the program:

• Let’s change the first condition to true and in between another


one to true:
if( a == 100 ) Line 12
else if( a == 100 ) Line 20
• Output of the program:
So it skips all the other
conditions.
Nested if Statements
• It is always legal in C programming to nest if-else statements,
which means you can use one if or else if statement inside
another if or else if statement(s).
Syntax:
• The syntax for a nested if statement is as follows:

if( boolean_expression 1)
{
/* Executes when the boolean expression 1 is true */
if(boolean_expression 2)
{
/* Executes when the boolean expression 2 is true */
}
}
Nested if Statements
• Example
#include <stdio.h>
int main ()
{
int a = 100;
int b = 200;
if( a == 100 )
{
if( b == 200 )
{
printf("Value of a is 100 and b is 200\n" );
}
}
printf("Exact value of a is : %d\n", a );
printf("Exact value of b is : %d\n", b );
return 0;
}
Nested if Statements
• Output will be:
if Statements

• So far we have compared integer variables with constant


numbers.
• It is also possible to compare variables with characters.
• Note that when comparing with characters we have to use
single quotation marks ''.
• If we want to compare with a string of characters such as
names, it is not possible to use if statement.
• In that case, speacial functions and data types are used.
if Statements
• Example

#include <stdio.h>
int main ()
{
char ch1 = 'a';
char ch2 = 'b';
if( ch1 == 'x')
{
printf("ch1 is a x character\n" );
}
if( ch2 == 'b')
{
printf("ch2 is a b character\n" );
}
printf("Exact character of ch1 : %c\n", ch1 );
printf("Exact character of ch1 : %c\n", ch2 );
return 0;
}
if Statements
• Output will be:

You might also like