Você está na página 1de 5

BIL-200 TUTORIAL FOR LAB-3 Group A&B&C

OBJECTIVE: In this lecture, flow of control tools will be discussed.


Relational, Equality and Logic Operators: To express the conditions in
C programming, relational, equality and logic operators are used. These operators
return binary bits which indicates that whether the comparison is true or not. If
the comparison is true, it returns 1, otherwise it returns 0. The “and” and/or “or”
operators are used to check the multiple conditions. “And” (&&) operator combines
two conditions, and results true if both conditions are true. If one of the condition
is false, then the result is false. “Or”(||) operator combines two conditions, and
results true if only one of the condition is true. If both conditions are false, then
the result is false. Some of the relational and equality operators are listed above;

Expression Type Operator Expression in C Condition Returned Value


Equality Equal Value1 ==Value2 500==500 TRUE (1)
Equality Not Equal Value1 ! =Value2 500!=650 TRUE (1)
Relational Greater or Equal Value1 >=Value2 356 >= 100 TRUE (1)
Relational Greater Value1 >Value2 356 > 100 TRUE (1)
Relational Less Value1 <Value2 356 < 400 TRUE (1)
Relational Less or Equal Value1 <=Value2 356 <= 500 TRUE (1)
Expression Type Condition Return Value
Logic (500 == 500)&&(756 < 400) FALSE (0)
Logic (456 > 1220)||(236 >= 236) TRUE (1)
Logic (500! = 650)&&(321 > 211) TRUE (1)
Logic (356 >= 100)||(200 == 200) TRUE(1)
Flow of Control:
“If ”: “If” statement is used in conditional cases. If an(some) expression(s) is (are)
wanted to execute by depending on a desired condition(s), “If” structure is used.
The expression which is desired to be executed is written after the “If (condition)”
statement. If the condition, written in the “If” statement, is satisfied, then the
desired statement is executed. Otherwise, program skips the following expression.

#include <stdio.h>
int main() {
int a=0,b=0;
char c;
printf("Assign a character:");
scanf("%c",&c);
if (c>101)
{
a=10;
b=10;
}
printf("Summation of a and b is %d",a+b);
}

“If-Else”: “If-Else” statement is analogous to the “if” statement. In this struc-


ture, there exist two statements (or statements block) and each of them depends on

1
BIL-200 TUTORIAL FOR LAB-3 Group A&B&C

the returning value of the condition written in the “If (condition)” statement. The
first expression is written after the “If(condition)” statement, and if the condition
is satisfied then this expression is executed and the program then by-pass the ex-
pression written in the “Else” statement. If the condition fails, then the expression
in the else statement is executed.
What may be the outputs of the following program?
Don’t write the code to find the exact solution

#include <stdio.h>
int main() {
int a,b;
a=50;
b=100;
if (a+b>150)
{ printf("\n Summation is greater than 150" );
printf("\n that’s all" );
}
else
{ printf("\n Summation is less than (or equal ) 150");
printf("\n that’s all" );
}
}

“Else If ” : “Else-If” structure contains more than one condition. If execution


of every statement in the program depends on conditions than this structure is
preferred. Execution of statements in the “Else-If” structure, depends on whether
the related condition written in the previous else statement is true or not. If
condition is satisfied, then the program executes the desired statement and skips
all other statements to the end of the structure. In this structure, for each different
condition, different statements are given. Let us consider the following program;

#include<stdio.h>
int main(){
int a;
printf("Input an Integer:\n");
scanf("%d",&a);
if(a%2==0 && a<0)
{printf("%d is even and less than zero",a); }
else if(a%2!=0 && a<0)
{printf("%d is odd and less than zero",a); }
else if(a%2==0 && a>0)
{printf("%d is even and strictly positive",a); }
else if(a%2!=0 && a>0)
{printf("%d is odd and strictly positive",a);}
else
printf("You Entered Zero");
}

2
BIL-200 TUTORIAL FOR LAB-3 Group A&B&C

‘’Switch” Statement: The switch statement is the generalization of the if-else


statement. A proper flow control can be achieved utilizing this function. Let us
consider the following example;

#include <stdio.h>
int main() {
int a=0,b=0;
char c;
printf("This program increases the values of a and b depends on the choice\n");
printf("If users assigns ‘a’ then a is increased 1\n");
printf("If users assigns ‘A’ then a is increased 2\n");
printf("If users assigns ‘b’ then b is increased 1\n");
printf("If users assigns ‘B’ then b is increased 2\n");
printf("If users assign ‘c’ or ’C’, then both a and b are increased 1\n");
printf("If users assigns ‘E’ or ’e’, the program terminates\n");
printf("\n\nAssign a character:");
scanf("%c",&c);
switch(c)
{
case ’a’:
++a;
break;
case ’A’:
a=a+2;
break;
case ’b’:
++b;
break;
case ’B’:
b=b+2;
break;
case ’c’:
++a;
++b;
break;
case ’C’:
++a;
++b;
break;
case ’E’:
printf("The program terminates ");
break;
case ’e’:
printf("The program terminates ");
break;
}
printf("a=%d b=%d\n",a,b);
}

3
BIL-200 TUTORIAL FOR LAB-3 Group A&B&C

’While’ loop: This structure is another conditional loop. In this loop, there exists
a condition to initialize the while loop. If the initialization condition fails, then
program skips the while loop and executes the next following statements of the
while loop. If the condition is satisfied, the program enters the loop and it executes
all the statements continuously in the loop up to condition fails. Simple “while”
examples are listed below; Examine the following programs;

#include <stdio.h>
int main() {
int i,sum;
i=0;
sum=0;
while (i<=10)
{ sum=sum+i;
i++; }
printf("Summation 1+2+...+10 equals %d\n",sum);}

Home Exercise 1: This examples also provides you to be experienced with usage
of “while” and “if”. Write a program that asks to user assign a value to n and then the
following output is resulted at the screen. If n = 10, then the output is following:
|10 − 0| = 10
|9 − 1| = 8
|8 − 2| = 6
. . .
|.. − ..| = ..
|1 − 9| = 8
|0 − 10| = 0
Do not use the abs() command. The | · | can be printed by using the printf function.
Home Exercise 2: Write a program to compute the roots of a second order equation
ax2 + bx + c. The program repeatedly asks the user to assign a value to the variables a,
b, and c. Then, it calculates the roots of the second order equation.
Usage of getchar and putchar The functions getchar and putchar are macros defined
in stdio.h. getchar reads a character from the keyboard and putchar prints a character
to the screen. In a memory, a char is stored in 1 byte, and an int is stored typically in 2
bits or 4 bits (according to compiler). Let us consider the following example.
#include<stdio.h>
int main() {
char x=’a’;
int a=0;
while (x!=’e’)
{
a=a+1;
x=getchar();
}
putchar(x);
puts("Bye Bye \n");
printf("a is %d ",a);
}

4
Project 2:  You will write a calculation program that recieves two parameters from the user. 

               First parameter(SEL_PAR) identifies the type of the calculation. It should be an  integer and 
can take the values from 1 to 4. 

    Second parameter(X) should also be a positive integer which is used in calculation. 

i. If SEL_PAR  is equal to 1, program should calculate the factorial of X (X!) and print the 
result. 
ii. If SEL_PAR is equal to 2, program should calculate the summation: ∑  and print 
the result. 
iii. If SEL_PAR is equal to 3, program should give the Xth element of the Fibonacci series. 

               For example: If X is 7, 

                The first seven elements of the Fibonacci series are 1, 1, 2, 3, 5, 8, 13. 

  So the result should be 13. 

iv. If SEL_PAR is equal to 4, program should calculate XX . (Do not use math library! 
Calculate by using for or while cycle.) 

   

Você também pode gostar