Intro To Algorithm and Programming 06
Intro To Algorithm and Programming 06
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
}
DR.KALAN 2
C /C++ Programming/ Flow Control
If Statement in C/C++ :
Example:
#include <stdio.h> Output 1
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
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;
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;
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;
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 ;
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