3. Controle Flow (If-Else)
1. Control Flow :
Execution is Top to bottom ;
review our general code snippet
#include<bits/stdc++.h> // generally contains almost required header files
using namespace std;
⌄int main () {
⌄ freopen("input.txt" , "r", stdin );
⌄ freopen("output.txt","r", stdout);
⌄ /*code*/
⌄ return 0;
}
2. Conditional Statements :
( use to manipulate flow of control )
if( isSatisfied ) {
code
}
the 'code' stament will execute only if isSatisfied have value of true (1)
Switch case :
are faster than if-else ladder
in Switch case it directly goes to the case which matches and execute it without checking other statements :
3. Loops :
1. while
int main () {
....
while( condition ) {
code ...
}
// until the condition is true the code... runs
2. for
int main () {
...
for( initialization stat ; finalize stat/condition ; progress stat ) {
code ...
}
}
3. do-while
do {
code ..
} . while ( condition ) ;
// codes runs first then condition checked , hence at least once code must run
// do-while is an exit control loop;
4. For-Each loop
int main () {
int a[] = {1,2,3,4,5};
for( auto x:a){
cout<<x<<endl;
return 0;
}
// for( auto x:a) // says x is a element in array
// for runs for them one by one x =1 for first case , so on ..
// i.e x = a[0] ... x = a[4] ... one by one for each iteration
Comments
Post a Comment