Chapter 2 Conrol Folow
Chapter 2 Conrol Folow
Chapter 2 Conrol Folow
Chapter 2
control-flow
MOHAMED HASSAN ABDULLE
HORMUUD UNIVERSITY
EMAIL: JALAQSANE2003@GMAIL.COM
FACEBOOK: MOHAMED HASSAN
Mobile: +252615825152 Or +252615000196
HTTP://HU.EDU.SO/
Course: computer Programming Lecturer : Eng . Ugaska
Outline
1. Introduction
2. If statement
3. If – else statement
4. If – else if statement (nested if statement)
5. Switch statement
6. For loop
7. while loop
8. Do – while loop
9. Exercise
Introduction
The control-flow statements of a language specify the order
in which computations are performed.
We have already met the most common control-flow
constructions in earlier examples;
before we discuss control-flow statements let us study
statements and blocks.
Statements
an expression such as x = 0 or i++ or printf () becomes
{ // beginning
int x = 10 ;
++ x ;
printf("%d", x);
} // ending
A block is syntactically equivalent to a single statement.
Blocks
if, else, while, for all have blocks
Variables can be declared inside any block.
There is no semicolon after the right brace that ends a block.
Example
#include<stdio.h>
Main()
{
int x = 0;
{
int x = 5;
printf("Inside: x = %d\n", x);
}
printf("Outside: x = %d\n", x);
}
Output : inside: x=5
outside: x =0
Flow of Control
Flow of control is the order in which a program
performs actions.
int num ;
printf ( "Enter a number less than 10 " ) ;
scanf ( "%d", &num ) ;
if ( num <= 10 )
printf ( "What an obedient servant you are !" ) ;
IF Statement
The if-else Statement
A branching statement that chooses between two possible
actions.
Syntax
if (Boolean Expression)
Statement_1
else
Statement_2
The if-else Statement
Example
#include<stdio.h>
main()
{
int num;
printf("enter number");
scanf("%d",&num);
if(num<=10)
printf("thanks");
else
printf("mahadsanid");
}
The if-else Statement
Other
Example
#include<stdio.h>
main()
int qty,dis=10;
float rate,total;
scanf("%d%f",&qty,&rate);
if(qty>1000)
dis=10;
#include<stdio.h>
main()
{
int z,y;
int x =10;
if(x>=10)
{
z = 5;
y = 6;
printf("z is = %d\n",z);
printf("y is = %d",y);
}}
If the else part is omitted and the expression after the if is false, no action occurs.
Omitting The Else Part
Syntax
if (Boolean_Expression)
Statement
#include<stdio.h>
main()
{
int z,y;
int x =10;
if(x>=11)
{
z = 5;
y = 6;
printf("z is = %d\n",z);
printf("y is = %d",y);
} }
C Comparison Operators
= Equal to == Balance == 0
= Not equal to != Balance != 0
> Greater than > Balance > 0
> Greater than >= Balance >= 0
Or equal to
< Less than < Balance < 0
< Less than <= Balance <= 0
Or equal to
C Logical Operators
#include<stdio.h>
main()
{
int score;
printf("Enter between 1 to 100\n");
scanf("%d",&score);
if ((score > 0) && (score <= 100))
printf("Welcome to hormuud");
else
printf(“Welcome to computer science ");
}
Compound Boolean Expressions
Boolean expressions can be combined using the “or” (||) operator.
Example
if ((quantity > 5) || (cost < 10))
Syntax
main()
{
int score;
printf("Enter between 1 to 100\n");
scanf("%d",&score);
if ((score > 0) || (score <= 1000))
printf("Welcome to hormuud");
else
printf(“Welcome to mohamed");
}
Compound Boolean Expressions
The NOT operator is often used to reverse the logical value of a single variable, as in the
expression.
if ( ! score )
This is another way of saying
if (score == 0 )
Example
#include<stdio.h>
main()
{
int score =1;
if (!score)
printf("Welcome HORMUUD\n");
else
{
printf("Welcome BCS3");
}
}
Truth Tables
Nested Statements
An if-else statement can contain any sort of statement within
it.
In particular, it can contain another if-else statement.
An if-else may be nested within the “if” part.
else.
int i ;
printf ( "Enter value of i " ) ;
scanf ( "%d", &i ) ;
if ( i = 5 )
printf ( "You entered 5" ) ;
else
printf ( "You entered something other than 5" ) ;
}
common mistake while using the if statement
Another common mistake while using the if statement is to
write a semicolon (;) after the condition, as shown below:
main( )
{
int i ;
printf ( "Enter value of i " ) ;
scanf ( "%d", &i ) ;
if ( i == 5 ) ;
printf ( "You entered 5" ) ;
}
Switch Statement, Cont.
The action associated with a matching case label is
executed.
switch (Controlling_Expression)
{
case Case_Label:
Statement(s);
break;
case Case_Label:
…
default:
…
}
Example of switch
#include<stdio.h>
case 2 :
main()
printf ( "I am in case 2 \n" ) ;
{
int i; break ;
printf("Enter Num between 1 to 3\n"); case 3 :
scanf("%d",&i);
printf ( "I am in case 3 \n" ) ;
switch ( i )
break ;
{
case 1 : default :
}
Loop Statements
A portion of a program that repeats a statement or a group
of statements is called a loop.
syntax
do
Body_Statement
while (Boolean_Expression);
main()
int x=1;
do
x++;
} while ( x<=5) ; }
Infinite Loops
Example
#include<stdio.h>
main()
{
int num=1;
for(num=1;num<=10;num++)
{
printf("number is = %d\n",num);
}}
Choosing a Loop Statement
If you know how many times the loop will be iterated, use a for loop.
If you don’t know how many times the loop will be iterated, but
Questions
?