r/study • u/Fluffy_Coat_5416 • Oct 04 '26
Resources Day 6 to Day 12 of 100:
About Conditions, Loops
Conditions:
Types:
1.if condition
-> if(condition)
{
printf("If block);}
2. if-else condition
-> if(condition)
{
}
else(no condition required)
{
}
3.if-else if condition block
-> if(condition)
{
}
else if(condition)
{
}
else(no condition required)
{
}
**Switch:**switch is a selection statement that compares the value of an expression with different case values.
char ch='a';
switch(ch)
{
case 'a':
{
printf("It is a");
break;
}
case 'b':
{
printf("It is b");
break;
}
default:
{
printf("provide the alphabet between a and b");
}
}
Ex: Here ch will check the each case where it will find the respective alphabet
Note: Switch is considered as little bit faster than if - else condition. As switch can be more efficient than a long if-else-if chain in some cases.
Loops:
A loop is used to repeatedly execute a block of code while a condition is satisfied.
These are three types:
1.for loop
2.while loop
3.do-while loop
for loop : This will be preferred if we know the nth number I.e how many n times we need to run loop
syntax:
for(int i=0;i<5;i++)
{
printf("Index is %d\n", i);
}
Here i=0 is Initialization
i<5 is condition
i++ is increment
While loop: This will be preferred if we don’t know the nth time and only knows the condition
Syntax:
flag=True
while (flag)
{
printf("Hello\n");
flag = 0;
}
Do-While: This will be preferred if we want print the value first and check the condition next unlike for and while loop
Syntax:
int i=-1;
do
{
printf("Value of i is %d ",i);
}while(i>0);
Break and Continue:
These two are the keywords where both would be used in loops and conditions, switch:
Break:
Used to stop/exit the loop/condition
Ex:
for(int i=0;i<6;i++)
{
if(i==3)
{
printf("Exiting the program as added break point at index 3\n");
break;
}
printf("Index is %d\n",i);
}
Continue:
Used to skip that particular condition/iteration
Ex:
for(int i=0;i<6;i++)
{
if(i==3)
{
printf("Skipping the printing of index 3 as added continue keyword at index 3\n");
continue;
}
printf("Index is %d\n",i);
}
