DEV Community

Ahmed Gouda
Ahmed Gouda

Posted on • Updated on • Originally published at ahmedgouda.hashnode.dev

Selection and Iteration in C

Selection Statements

Selection statements are used to choose among alternative actions.

if

if( Condition )
{
    Action;
}
Enter fullscreen mode Exit fullscreen mode
if(grade >= 60)
{
    puts("Passed");
}
Enter fullscreen mode Exit fullscreen mode

if .. else

if(Condition)
{
    Action_1;
}
else
{
    Action_2;
}
Enter fullscreen mode Exit fullscreen mode
if(grade >= 60)
{
    puts("Passed");
}
else
{
    puts("Failed");
}
Enter fullscreen mode Exit fullscreen mode

if .. elseif

if(Condition_1)
{
    Action_1;
}
else if(Condition_2)
{
    Action_2;
}
else if(Condition_3)
{
    Action_3;
}
else
{
    Action_4;
}
Enter fullscreen mode Exit fullscreen mode
if(grade >= 90)
{
    puts("A");
}
else if(grade >= 80)
{
    puts("B");
}
else if(grade >= 70)
{
    puts("C");
}
else if(grade >= 60)
{
    puts("D");
}
else
{
    puts("F");
}
Enter fullscreen mode Exit fullscreen mode

switch

switch(Condition)
{
case 1:
    Action_1;
    break;

case 2:
    Action_2;
    break;

case 3:
    Action_3;
    break;

default:
    Action_4;
    break;
}
Enter fullscreen mode Exit fullscreen mode
switch(mark)
{
case 'A':
    puts("Student got excellent grade");
    break;

case 'B':
    puts("Student got very good grade");
    break;

case 'C':
    puts("Student got good degree grade");
    break;

case 'D':
    puts("Student got fair grade");
    break;

default:
    puts("Student should retake the exam");
    break;
}
Enter fullscreen mode Exit fullscreen mode

Conditional Operator

This is the only C ternary operator. It is used in place where if .. else can't.

Condition ? True Action : False Action;
Enter fullscreen mode Exit fullscreen mode
grade >= 60 ? puts("Pass") : puts("Fail");
//or
puts(grade >= 60 ? "Pass" : "Fail");
Enter fullscreen mode Exit fullscreen mode

Iteration Statements

An iteration statement (repetition statement or loop) allows you to specify that an action is to be repeated while some condition remains true.

while

while(Continuation_Condition)
{
    Action;
}
Enter fullscreen mode Exit fullscreen mode
while(grade < 60)
{
    puts("Retake the exam");
}
Enter fullscreen mode Exit fullscreen mode

do .. while

do{

    Action;

}while(Continuation_Condition);
Enter fullscreen mode Exit fullscreen mode
do{
    //do the action once at least before check the condition
    puts("Start the Exam");

}while(grade < 60>);
Enter fullscreen mode Exit fullscreen mode

for

for(Initialization; Continuation_Condition; Increment)
{
    Action;
}
Enter fullscreen mode Exit fullscreen mode
for(int i = 1; i <= 10; i++)
{
    printf("%d ", i);
}
Enter fullscreen mode Exit fullscreen mode

Iteration Types

  1. Counter-Controlled
  2. Sentinel-Controlled

Counter-Controlled Iteration

We know in advance exactly how many times the loop will be executed.

Sentinel-Controlled Iteration

We don't know in advance how many times the loop will be executed. We use sentinel value (flag value) to indicated "End of Data" entry.

Discussion (0)