question archive Define and provide the syntax of the following

Define and provide the syntax of the following

Subject:Computer SciencePrice:2.86 Bought11

Define and provide the syntax of the following. Provide an example.

1. if statement

2. switch statement

3. for loop

4. while loop

5. do while loop

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

1.If statements are use to control the process of the program by using conditions together with the else statements, which also function like if statements. If and else if statements uses logical operator symbols such as (>)less than, (<)greater than,(=) equal to, (!=)not equal to, or the combination of any two statements to compare the values of the variables and execute commands if the conditions are met. Else if statements are used to specify another condition if the first condition is not satisfied while else statements are use if none of the condition in if and else if statements are satisfied or true.

Syntax:

if (condition){

//block of code to be run if the condition is true

}

else if (condition) {

//block of code to be run if the condition is true

}

else{

//block of code to be run if no condition is satisfied

}

2.Switch statements are use to execute a group of command among with many other options. It compares the value of a variable or expression to the list of the options, and if the variable is equal to the option, it executes the command within that option. It also uses the default option to execute a command if no option is equal to the variable. The list of option in the switch statements are called case.

Syntax:

switch( expression or variable){

case value1:

//command to be executed if the expression/variable is equal to the value of the case

break;

 

case value2:

//command to be executed if the expression/variable is equal to the value of the case

break;

 

case value3:

//command to be executed if the expression/variable is equal to the value of the case

break;

default:

//command to be executed if no value in the list cases is equal to the expression/variable/

}

3.For Loops are used to repeat a group of code in a certain number, for example, if you want to print your name 100 times, instead of typing it, you can use for loop to print it 100 times. It uses initial expression to set the starting value or initialize the variable , second expression to set the limit of the loop, and update expression to update the value of the initial expression.

Syntax:

for (initial expression; second expression; update expression){

//block of code to run

}

4.While loops are almost the same as for loop, except it uses a single expression to run a block of code repeatedly. Until the expression is met, the code will be run repeatedly

Syntax:

while (expression){

//block of code to be executed

}

5. Do - while loops are just the same as for and while loops, except that it executes the "do " block of codes first before testing if the expression in the "while " is met. The do block of codes are simply the codes you want to run if the expression in the while is true.

Syntax

do {

//block of code to be run

}

while (expression)