0% found this document useful (0 votes)
9 views

Intro To Algorithm and Programming 06

The document discusses various flow control concepts in C/C++ including if-else statements, switch-case statements, and type casting. It provides examples and explanations of how each concept works.

Uploaded by

okatarinatmo11
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Intro To Algorithm and Programming 06

The document discusses various flow control concepts in C/C++ including if-else statements, switch-case statements, and type casting. It provides examples and explanations of how each concept works.

Uploaded by

okatarinatmo11
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Lecture 06

We will cover these skills:


 C/C++ Flow Control
 If & Else Statement
 Switch Case Statement
 Ternary Operator
 Type Casting in C/C++

REZA KALAN 1
C /C++ Programming/ Flow Control
If Statement in C/C++ :
The if statement in C is a control structure that evaluates a condition.
If the condition is true, it runs the associated code; if false, the code is ignored.
The syntax of the if statement in C programming is:

if (test expression) {
// code
}

How If statement works?


The if statement evaluates the test expression inside the parenthesis ().
 If the test expression is evaluated to true, statements inside the body of if are executed.
 If the test expression is evaluated to false, statements inside the body of if are not executed.

DR.KALAN 2
C /C++ Programming/ Flow Control
If Statement in C/C++ :
Example:
#include <stdio.h> Output 1

int main() { Enter a positive integer number: -3


int number; Entered number is: -3
This is a negative number
printf("Enter a positive integer number: ");
scanf("%d", &number);
printf("\n Entered number is %d", number);
Output 2
// true if number is less than 0
Enter a positive integer number: 5
if (number < 0) {
Entered number is: 5
printf("\nThis is a negative number");
}

return 0;
}

DR.KALAN 3
C /C++ Programming/ Flow Control
If…Else Statement in C/C++ :
The if statement may have an optional else block.
The syntax of the if..else statement is

How Does it work

if (test expression) { If the test expression is evaluated to true,


// run code if test expression is true
}  statements inside the body of if are executed.
 statements inside the body of else are skipped from
else { execution.
// run code if test expression is false If the test expression is evaluated to false,
}
 statements inside the body of else are executed
 statements inside the body of if are skipped from execution.

DR.KALAN 4
C /C++ Programming/ Flow Control
If Statement in C/C++ :
Example: // Check whether an integer is odd or even
#include <iostream> Output 1
using namespace std;
Enter an integer: 11
int main() { 11 is an odd integer
int number;
printf("Enter an integer: ");
cin>>number;

// True if the remainder is 0 Output 2


if (number%2 == 0) {
cout <<number << " is an even integer"; Enter an integer: 4
} 4 is an even integer
else {
cout <<number << " is an odd integer";
}
return 0;
}
DR.KALAN 5
C /C++ Programming/ Flow Control
If…Else Ladder in C/C++ :
Sometimes, a choice has to be made from more
than 2 possibilities.

if (test expression1) {
// statement(s)
The if...else ladder allows you to check between
}
multiple test expressions and execute different else if(test expression2) {
statements. // statement(s)
}
else if (test expression3) {
// statement(s)
}
.
.
The syntax of the if..else statement is else {
// statement(s)
}

DR.KALAN 6
C /C++ Programming/ Flow Control
If Statement in C/C++ : #include <iostream> #include <stdio.h>
using namespace std;
Example:
int main() {
Compare two integer numbers int number1, number2;
cout <<"Enter number1: "; cin>> number1;
cout <<"Enter number2: "; cin>> number2;

Output 1 if(number1 == number2) { //checks if the two integers are equal.


cout<<number1 << "=" <<number2;
Enter number1: 13 //printf("Result: %d = %d",number1,number2);
Enter number2: 17 }
13<17 else if (number1 > number2) {
cout<<number1 << ">" <<number2;
}
Output 2 else { //checks if both test expressions are false
cout<<number1 << "<" <<number2;
Enter number1: 7 }
Enter number2: 7 return 0;
7=7 }

DR.KALAN 7
C /C++ Programming/ Flow Control
Switch ... Case Statement in C/C++ :
The switch statement in C is an alternate to if-else-if ladder statement
which allows us to execute multiple operations for the different possible
values of a single variable called switch variable.
We can define various statements in the multiple cases for the different
values of a single variable.
Syntax of switch...case

switch (expression){
case 1:
// statements
break;
case 2:
// statements
break;
.
default:
// default statements
}

DR.KALAN 8
C /C++ Programming/ Flow Control
Switch ... Case Statement in C/C++ :
How does the switch statement work?
 The expression is evaluated once and compared with the values of each case label.
 If there is a match, the corresponding statements after the matching label are executed.
 For example, if the value of the expression is equal to 2, statements after case 2: are executed until break is encountered.
 If there is no match, the default statements are executed.

Notes:
 If we do not use the break statement, all statements after the matching label are also executed.
 The default clause inside the switch statement is optional.

DR.KALAN 9
C /C++ Programming/ Flow Control
Switch Statement in C/C++ :
Example 1:
#include <stdio.h> case 4:
printf("\n Wednesday");
int main() { break;
int number; case 5:
printf("\n Eenter number between 1 and 7 :\n"); printf("\n Thursday");
scanf("%d", & number); break;
case 6:
switch (number){ case 7:
case 1: printf("\n Weekends");
printf("\n Sunday"); break;
break;
case 2: default:
printf("\n Munday"); printf ("Invalid input");
break; }
case 3: return 0;
printf("\n Tusday"); }

DR.KALAN 10
C /C++ Programming/ Flow Control
Type Casting in C/C++ :
Type conversion is the method to convert one data type into another data type.
Conversion hierarchy
 Small data type change to bigger one

#include <stdio.h>

int main() {
float a = 7.5;

int b = (int) a *2 ; //typecasting float to int

printf("The value of b is: %d", b);

return 0;
}

DR.KALAN 11
C /C++ Programming/ Flow Control
Short Hand If / Else in C/C++ :
The ternary operator, also known as the #include <stdio.h>
conditional operator, is used as shorthand for an int main() {
if...else statement. int number; bool remainder ;

A ternary operator is written with the syntax of a printf("\n Enter a number:\n");


question mark ( ? ) followed by a colon ( : ), as scanf("%d", & number );
demonstrated below.
//(number < 0) ? printf("negative number") : printf("Positive number");

remainder =(number%2) ? 1 : 0;
variable = (condition) ? expressionTrue : expressionFalse;by a ? printf(" remainder is: %d\n", remainder );

if (remainder )
printf("The number is odd");
Output else
Enter a number: printf("The number is even");
4
remainder is: 0 return 0;
The number is even }

DR.KALAN 12
C /C++ Programming/ Flow Control
Switch Statement in C/C++ : Important : Converting one datatype into another is known as type casting
Example 1: A sample calculator
#include <stdio.h> case '/':
if (number2== 0)
int main() { printf("\n Invalid operation, number2 should be > 0 ");
else
int number1, number2; char operand; printf("\n number1/number2 = %f", (float) number1/number2);
break;
printf("\n Enter two numbers and one operator :");
scanf("%d %d %c", & number1,& number2, & operand ); default:
printf ("Invalid input");
switch (operand){ }
case '+': return 0;
printf("\n number1+number2 = %d", number1+number2); }
break;
case '-': Output
printf("\n number1-number2 = %d", number1-number2); Enter two numbers and one operator :
break; 2
case '*': 3
printf("\n number1*number2 = %d", number1*number2); /
break; number1/number2 = 0.666667

DR.KALAN 13

You might also like