C Programming: Switch Statement, Looping and Branching

Asked By 110 points N/A Posted on -
qa-featured

Can anyone help me find the answer to the following questions?

  1. What is "switch statement " in C programming?
  2. What is looping and branching?

Please give me honest suggestions and help me solve my problem.

Thanks.

SHARE
Best Answer by saidur rahman
Best Answer
Best Answer
Answered By 110 points N/A #89058

C Programming: Switch Statement, Looping and Branching

qa-featured

At first, I will give you a heartily thanks for asking this nice question. Here is your solution:

  • "C" is a very interesting programming language. At the same time, it is a very user-friendly programming language for a vast use of user-defined functions and statements; switch statement is one of them.

Let me first clear what the switch statement is. Switch statement is needed when it is necessary to switch a certain statement of my coding. Let me make it clearer. Suppose, you have a cupboard and you have stored your daily necessities in it. When you need a plate or glass you know in which compartment you have kept them. You will then directly open that cupboard and will find the things quite easily. Switch statements do similar tasks.

GENERAL EXPRESSION:

switch(expression)

{

case constant:

statement;

break;

default:

default statement;

break;

}

  • Looping means doing the same jobs more than once what an individual needs. Some well known loops are for loop, while loop etc.

Branching means checking conditions step by step and switching to the appropriate one. Some well known branching statements include if else statement, nested if, etc.

Answered By 0 points N/A #89059

C Programming: Switch Statement, Looping and Branching

qa-featured

I am an expert in C and I want to tell you something new about looping.

"Recurrence" is the main goal, which can be fulfilled by looping. Among various loops, for is the mostly used feature.

The general format of for looping is:

for ( initialization;  condition; increment )

{statement;}

Suppose, initial value is 1 and the condition is do up to 10 and increment is 1; then the expression would be:

for (a=1 ;  a<=10 ; a++)

{sum=sum+a;}

Here, a is the variable while sum is defined in the main coding .

I think you can do well if you practice more and more.

Thanks

Answered By 110 points N/A #89060

C Programming: Switch Statement, Looping and Branching

qa-featured

Thank you both .

Actually I am facing a great problem in solving a task of adding the numbers from 1 to 100; how can I do that; can you help?

Thanks

Answered By 110 points N/A #89062

C Programming: Switch Statement, Looping and Branching

qa-featured

Oh it's easy.

You can use for loop for the solution; here is the coding I provide:

sum=0;

for(a=1:a<=100;a++)

{

sum=sum+a;

}

printf("the answer:  %f ", sum);

Here is the solution you wanted.

Thanks.

Answered By 0 points N/A #89063

C Programming: Switch Statement, Looping and Branching

qa-featured

You can also use the while loop.

Coding:

sum=0;

a=1;

while(a<=100)

{

sum=sum+a;

a++;

}

But I would honestly prefer the  for loop more than the while as for is easier than While.

Thanks

Answered By 110 points N/A #89064

C Programming: Switch Statement, Looping and Branching

qa-featured

Thank you both guys for spending your valuable time for me. I have learned many useful things

from this conversation and am really thankful to you.

I would like to thank, especially Saidur for his large response and for taking my question sincerely.

I would also like to thank Abdul for his technical analysis.

Thanks,

Arif.

Answered By 0 points N/A #89065

C Programming: Switch Statement, Looping and Branching

qa-featured

First, in programming we have this flow control. In flow control we have the so-called choice and loops. Branching occurs in choice, which uses if/else statement. It is called branching since it chooses one branch that meets the condition. More complex branching can be done in switch statement to make it easier. The switch statement has this expression:

selection statement

switch (expression)

case: constant expression

statement executed if the expression is equal to the value of this constant expression

break;

default:

statement executed if the expression does not equal any constant expression

Looping is a sequence of instructions repeated until a certain condition is reached. In C, looping statement used are while, do while and for loops.

Related Questions