CENG 103 – Computer Programming I
Week 4
Dr. Akif Hacinecipoglu
Flow Control
Control Structures
Sequential Conditional Iterative
if for
if ... else while
switch do ... while
Flow Control
Conditional (Selection structure)
• if Structure
if ( booleanExpression )
{
true-block ;
}
Flow Control
Conditional (Selection structure)
• if ... else Structure
if ( booleanExpression ) {
true-block ;
} else {
false-block ;
}
Flow Control
Conditional (Selection structure)
• if ... else Structure (nested)
if ( booleanExpr-1 ) {
block-1 ;
} else if ( booleanExpr-2 ) {
block-2 ;
} else if ( booleanExpr-3 ) {
block-3 ;
} else if ( booleanExpr-4 ) {
......
} else {
elseBlock ;
}
Flow Control
Conditional (Selection structure)
• if ... else Structure (nested inside)
if ( booleanExpr-1 ) if ( booleanExpr-1 )
{ {
block-1;
}
block-1;
else }
{ else if ( booleanExpr-2 )
if ( booleanExpr-2 ) {
{ block-2;
block-2; }
}
else else
{ {
elseBlock; elseBlock;
} }
}
Flow Control
Conditional (Selection structure)
• Use of braces.
if ( booleanExpr-1 )
{
statement-1;
statement-2;
}
if ( booleanExpr-1 )
statement-1;
statement-2; // Not evaluated inside if block! Common mistake.
Flow Control
Conditional (Selection structure)
• if Structure. Use of braces.
• You may omit braces if you have one statement only.
• Still it is a good practice to use braces even with one statement.
int a = 5; int a = 5;
if (a > 10) if (a > 10)
a++; {
else a++;
a--; }
else
{
a--;
}
Flow Control
Conditional (Selection structure)
• switch Structure
switch ( selector ) {
case value-1:
block-1;
break;
case value-2:
block-2;
break;
case value-n:
block-n;
break;
default:
default-block;
}
Flow Control
Conditional (Selection structure)
A switch statement allows a variable to be tested for equality against a list of values.
• You can use either an int or char variable as the
switch ( selector ) {
case-selector. case value-1:
block-1;
• You can have any number of case statements break;
case value-2:
within a switch.
block-2;
break;
• Each case is followed by the value to be compared case value-n:
to and a colon. block-n;
break;
• The constant-expression for a case must be the default:
default-block;
same data type as the variable in the switch }
Flow Control
Conditional (Selection structure)
A switch statement allows a variable to be tested for equality against a list of values.
• When the variable being switched on is equal to a
switch ( selector ) {
case, the statements following that case will case value-1:
execute until a break statement is reached. block-1;
break;
case value-2:
• When a break statement is reached, the switch
block-2;
terminates, and the flow of control jumps to the break;
case value-n:
next line following the switch statement. block-n;
break;
• Not every case needs to contain a break. If no default:
break appears, the flow of control will fall through default-block;
}
to subsequent cases until a break is reached.
Flow Control
Conditional (Selection structure)
A switch statement allows a variable to be tested for equality against a list of values.
• A switch statement can have an optional default
switch ( selector ) {
case. case value-1:
block-1;
• The default case can be used for performing a break;
case value-2:
task when none of the cases is true.
block-2;
break;
• break is optional in the default case. case value-n:
block-n;
break;
default:
default-block;
}
Flow Control
#include <stdio.h>
int main(void) {
char operator;
double n1, n2;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf",&n1, &n2);
switch(operator)
{
case '+':
printf("%.1lf + %.1lf = %.1lf",n1, n2, n1+n2);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf",n1, n2, n1-n2);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf",n1, n2, n1*n2);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf",n1, n2, n1/n2);
break;
default:
printf("Error! operator is not correct");
}
return 0;
}
Flow Control
char type, revisited
• Character type is one-byte data type stored in memory.
• Every character has a basic integer encoding from 0 to 127.
• There are also unicode characters.
char value1 = 'A';
char value2 = 'a';
int value3 = value1;
Flow Control
char type, revisited
• Character type is one-byte data type stored in memory.
• Every character has a basic integer encoding from 0 to 127.
• There are also unicode characters.
#include <stdio.h>
int main(void) {
char value1 = 'A';
char value2 = 'a';
int value3 = value1;
int value4 = value2;
printf("%d\n", value3); // prints out 65
printf("%d", value4); // prints out 97
return 0;
}
ASCII Table
American Standard Code for
Information Interchange is a character
encoding standard for electronic
communication.
Flow Control
#include <stdio.h>
int main(void) {
char operator;
double n1, n2;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf",&n1, &n2);
switch(operator)
{
case 43:
printf("%.1lf + %.1lf = %.1lf",n1, n2, n1+n2);
break;
case 45:
printf("%.1lf - %.1lf = %.1lf",n1, n2, n1-n2);
break;
case 42:
printf("%.1lf * %.1lf = %.1lf",n1, n2, n1*n2);
break;
case 47:
printf("%.1lf / %.1lf = %.1lf",n1, n2, n1/n2);
break;
default:
printf("Error! operator is not correct");
}
return 0;
}
Flow Control
#include <stdio.h>
int main (void) {
char grade = 'B';
switch(grade) {
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
case 'C' :
printf("Well done\n" );
break;
case 'D' :
printf("You passed\n" );
break;
case 'F' :
printf("Better try again\n" );
break;
default :
printf("Invalid grade\n" );
}
printf("Your grade is %c\n", grade );
return 0;
}
Flow Control
Conditional (Selection structure)
• Conditional operator: A conditional operator is a ternary (3-operand) operator, in the
form of booleanExpr ? trueExpr : falseExpr. Depending on the booleanExpr, it evaluates
and returns the value of trueExpr or falseExpr.
booleanExpr ? trueExpr : falseExpr
printf("%s\n", (grade >= 50) ? "PASS" : "FAIL");
// print either "PASS" or "FAIL"
max = (a > b) ? a : b; // RHS returns a or b
abs = (a > 0) ? a : -a; // RHS returns a or -a
Sample Questions
What will be the output of the following C program?
#include <stdio.h>
İnt main(void) {
double x, y;
x = 7;
x = x / 2;
y = x + x / 2;
printf("%.2f %.2f", x, y);
return 0;
}
a) 3.00 3.00 b) 3.00 4.50 c) 3.50 3.50 d) 3.50 5.25 e) 4.50 5.25
Sample Questions
What will be the output of the following C program?
#include <stdio.h>
int main(void) {
printf("%c, %d, %c, %d", 'a', 'a', 97, 97);
return 0;
}
a) a, a, 97, 97 b) 97, a, 97, a c) 97, 97, a, a d) a, 97, a, 97 e) a, 97, 97, a
Sample Questions
What will be the output of the following C program?
#include <stdio.h>
int main(void) {
double pi = 22/7;
printf("%3.2f", pi);
return 0;
}
a) 3.142857 b) 3.142 c) 3.14 d) 03.14 e) 3.00
Sample Questions
What will be the output of the following C program?
#include <stdio.h>
int main(void) {
int x = 5;
int y = 3;
y += 5-y+x++;
x = y%x;
printf("%d", x);
return 0;
}
a) 3 b) 4 c) 6 d) 2 e) 5
Sample Questions
What will be the output of the following C program?
#include <stdio.h>
int main(void) {
int b;
int a = 3, c = 5;
b = 12+a--/++c-(--a);
printf("%d", b);
return 0;
}
a) 8 b) 9 c) 10 d) 11 e) 12
Sample Questions
What will be the output of the following C program?
#include <stdio.h>
int main(void) {
int b;
int a = 6, c = 5;
b = 12+a--/++c-(--a);
printf("%d", b);
return 0;
}
a) 8 b) 9 c) 10 d) 11 e) 12